2 doc/src/sgml/ref/create_extension.sgml
3 PostgreSQL documentation
6 <refentry id=
"sql-createextension">
7 <indexterm zone=
"sql-createextension">
8 <primary>CREATE EXTENSION
</primary>
12 <refentrytitle>CREATE EXTENSION
</refentrytitle>
13 <manvolnum>7</manvolnum>
14 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
18 <refname>CREATE EXTENSION
</refname>
19 <refpurpose>install an extension
</refpurpose>
24 CREATE EXTENSION [ IF NOT EXISTS ]
<replaceable class=
"parameter">extension_name
</replaceable>
25 [ WITH ] [ SCHEMA
<replaceable class=
"parameter">schema_name
</replaceable> ]
26 [ VERSION
<replaceable class=
"parameter">version
</replaceable> ]
32 <title>Description
</title>
35 <command>CREATE EXTENSION
</command> loads a new extension into the current
36 database. There must not be an extension of the same name already loaded.
40 Loading an extension essentially amounts to running the extension's script
41 file. The script will typically create new
<acronym>SQL
</acronym> objects such as
42 functions, data types, operators and index support methods.
43 <command>CREATE EXTENSION
</command> additionally records the identities
44 of all the created objects, so that they can be dropped again if
45 <command>DROP EXTENSION
</command> is issued.
49 The user who runs
<command>CREATE EXTENSION
</command> becomes the
50 owner of the extension for purposes of later privilege checks, and
51 normally also becomes the owner of any objects created by the
56 Loading an extension ordinarily requires the same privileges that would
57 be required to create its component objects. For many extensions this
58 means superuser privileges are needed.
59 However, if the extension is marked
<firstterm>trusted
</firstterm> in
60 its control file, then it can be installed by any user who has
61 <literal>CREATE
</literal> privilege on the current database.
62 In this case the extension object itself will be owned by the calling
63 user, but the contained objects will be owned by the bootstrap superuser
64 (unless the extension's script explicitly assigns them to the calling
65 user). This configuration gives the calling user the right to drop the
66 extension, but not to modify individual objects within it.
72 <title>Parameters
</title>
76 <term><literal>IF NOT EXISTS
</literal></term>
79 Do not throw an error if an extension with the same name already
80 exists. A notice is issued in this case. Note that there is no
81 guarantee that the existing extension is anything like the one that
82 would have been created from the currently-available script file.
88 <term><replaceable class=
"parameter">extension_name
</replaceable></term>
91 The name of the extension to be
92 installed.
<productname>PostgreSQL
</productname> will create the
93 extension using details from the file
94 <literal>SHAREDIR/extension/
</literal><replaceable class=
"parameter">extension_name
</replaceable><literal>.control
</literal>.
100 <term><replaceable class=
"parameter">schema_name
</replaceable></term>
103 The name of the schema in which to install the extension's
104 objects, given that the extension allows its contents to be
105 relocated. The named schema must already exist.
106 If not specified, and the extension's control file does not specify a
107 schema either, the current default object creation schema is used.
111 If the extension specifies a
<literal>schema
</literal> parameter in its
112 control file, then that schema cannot be overridden with
113 a
<literal>SCHEMA
</literal> clause. Normally, an error will be raised if
114 a
<literal>SCHEMA
</literal> clause is given and it conflicts with the
115 extension's
<literal>schema
</literal> parameter. However, if
116 the
<literal>CASCADE
</literal> clause is also given,
117 then
<replaceable class=
"parameter">schema_name
</replaceable> is
118 ignored when it conflicts. The
119 given
<replaceable class=
"parameter">schema_name
</replaceable> will be
120 used for installation of any needed extensions that do not
121 specify
<literal>schema
</literal> in their control files.
125 Remember that the extension itself is not considered to be within any
126 schema: extensions have unqualified names that must be unique
127 database-wide. But objects belonging to the extension can be within
134 <term><replaceable class=
"parameter">version
</replaceable></term>
137 The version of the extension to install. This can be written as
138 either an identifier or a string literal. The default version is
139 whatever is specified in the extension's control file.
145 <term><literal>CASCADE
</literal></term>
148 Automatically install any extensions that this extension depends on
149 that are not already installed. Their dependencies are likewise
150 automatically installed, recursively. The
<literal>SCHEMA
</literal> clause,
151 if given, applies to all extensions that get installed this way.
152 Other options of the statement are not applied to
153 automatically-installed extensions; in particular, their default
154 versions are always selected.
165 Before you can use
<command>CREATE EXTENSION
</command> to load an extension
166 into a database, the extension's supporting files must be installed.
167 Information about installing the extensions supplied with
168 <productname>PostgreSQL
</productname> can be found in
169 <link linkend=
"contrib">Additional Supplied Modules
</link>.
173 The extensions currently available for loading can be identified from the
174 <link linkend=
"view-pg-available-extensions"><structname>pg_available_extensions
</structname></link>
176 <link linkend=
"view-pg-available-extension-versions"><structname>pg_available_extension_versions
</structname></link>
182 Installing an extension as superuser requires trusting that the
183 extension's author wrote the extension installation script in a secure
184 fashion. It is not terribly difficult for a malicious user to create
185 trojan-horse objects that will compromise later execution of a
186 carelessly-written extension script, allowing that user to acquire
187 superuser privileges. However, trojan-horse objects are only hazardous
188 if they are in the
<varname>search_path
</varname> during script
189 execution, meaning that they are in the extension's installation target
190 schema or in the schema of some extension it depends on. Therefore, a
191 good rule of thumb when dealing with extensions whose scripts have not
192 been carefully vetted is to install them only into schemas for which
193 CREATE privilege has not been and will not be granted to any untrusted
194 users. Likewise for any extensions they depend on.
198 The extensions supplied with
<productname>PostgreSQL
</productname> are
199 believed to be secure against installation-time attacks of this sort,
200 except for a few that depend on other extensions. As stated in the
201 documentation for those extensions, they should be installed into secure
202 schemas, or installed into the same schemas as the extensions they
208 For information about writing new extensions, see
209 <xref linkend=
"extend-extensions"/>.
214 <title>Examples
</title>
217 Install the
<link linkend=
"hstore">hstore
</link> extension into the
218 current database, placing its objects in schema
<literal>addons
</literal>:
220 CREATE EXTENSION hstore SCHEMA addons;
222 Another way to accomplish the same thing:
224 SET search_path = addons;
225 CREATE EXTENSION hstore;
226 </programlisting></para>
230 <title>Compatibility
</title>
233 <command>CREATE EXTENSION
</command> is a
<productname>PostgreSQL
</productname>
239 <title>See Also
</title>
241 <simplelist type=
"inline">
242 <member><xref linkend=
"sql-alterextension"/></member>
243 <member><xref linkend=
"sql-dropextension"/></member>