2 doc/src/sgml/ref/create_database.sgml
3 PostgreSQL documentation
6 <refentry id=
"sql-createdatabase">
7 <indexterm zone=
"sql-createdatabase">
8 <primary>CREATE DATABASE
</primary>
12 <refentrytitle>CREATE DATABASE
</refentrytitle>
13 <manvolnum>7</manvolnum>
14 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
18 <refname>CREATE DATABASE
</refname>
19 <refpurpose>create a new database
</refpurpose>
24 CREATE DATABASE
<replaceable class=
"parameter">name
</replaceable>
25 [ WITH ] [ OWNER [=]
<replaceable class=
"parameter">user_name
</replaceable> ]
26 [ TEMPLATE [=]
<replaceable class=
"parameter">template
</replaceable> ]
27 [ ENCODING [=]
<replaceable class=
"parameter">encoding
</replaceable> ]
28 [ STRATEGY [=]
<replaceable class=
"parameter">strategy
</replaceable> ]
29 [ LOCALE [=]
<replaceable class=
"parameter">locale
</replaceable> ]
30 [ LC_COLLATE [=]
<replaceable class=
"parameter">lc_collate
</replaceable> ]
31 [ LC_CTYPE [=]
<replaceable class=
"parameter">lc_ctype
</replaceable> ]
32 [ BUILTIN_LOCALE [=]
<replaceable class=
"parameter">builtin_locale
</replaceable> ]
33 [ ICU_LOCALE [=]
<replaceable class=
"parameter">icu_locale
</replaceable> ]
34 [ ICU_RULES [=]
<replaceable class=
"parameter">icu_rules
</replaceable> ]
35 [ LOCALE_PROVIDER [=]
<replaceable class=
"parameter">locale_provider
</replaceable> ]
36 [ COLLATION_VERSION =
<replaceable>collation_version
</replaceable> ]
37 [ TABLESPACE [=]
<replaceable class=
"parameter">tablespace_name
</replaceable> ]
38 [ ALLOW_CONNECTIONS [=]
<replaceable class=
"parameter">allowconn
</replaceable> ]
39 [ CONNECTION LIMIT [=]
<replaceable class=
"parameter">connlimit
</replaceable> ]
40 [ IS_TEMPLATE [=]
<replaceable class=
"parameter">istemplate
</replaceable> ]
41 [ OID [=]
<replaceable class=
"parameter">oid
</replaceable> ]
46 <title>Description
</title>
49 <command>CREATE DATABASE
</command> creates a new
50 <productname>PostgreSQL
</productname> database.
54 To create a database, you must be a superuser or have the special
55 <literal>CREATEDB
</literal> privilege.
56 See
<xref linkend=
"sql-createrole"/>.
60 By default, the new database will be created by cloning the standard
61 system database
<literal>template1
</literal>. A different template can be
62 specified by writing
<literal>TEMPLATE
63 <replaceable class=
"parameter">name
</replaceable></literal>. In particular,
64 by writing
<literal>TEMPLATE template0
</literal>, you can create a pristine
65 database (one where no user-defined objects exist and where the system
66 objects have not been altered)
67 containing only the standard objects predefined by your
68 version of
<productname>PostgreSQL
</productname>. This is useful
69 if you wish to avoid copying
70 any installation-local objects that might have been added to
71 <literal>template1
</literal>.
76 <title>Parameters
</title>
79 <varlistentry id=
"create-database-name">
80 <term><replaceable class=
"parameter">name
</replaceable></term>
83 The name of a database to create.
87 <varlistentry id=
"create-database-user-name">
88 <term><replaceable class=
"parameter">user_name
</replaceable></term>
91 The role name of the user who will own the new database,
92 or
<literal>DEFAULT
</literal> to use the default (namely, the
93 user executing the command). To create a database owned by another
94 role, you must be able to
<literal>SET ROLE
</literal> to that
99 <varlistentry id=
"create-database-template">
100 <term><replaceable class=
"parameter">template
</replaceable></term>
103 The name of the template from which to create the new database,
104 or
<literal>DEFAULT
</literal> to use the default template
105 (
<literal>template1
</literal>).
109 <varlistentry id=
"create-database-encoding">
110 <term><replaceable class=
"parameter">encoding
</replaceable></term>
113 Character set encoding to use in the new database. Specify
114 a string constant (e.g.,
<literal>'SQL_ASCII'
</literal>),
115 or an integer encoding number, or
<literal>DEFAULT
</literal>
116 to use the default encoding (namely, the encoding of the
117 template database). The character sets supported by the
118 <productname>PostgreSQL
</productname> server are described in
119 <xref linkend=
"multibyte-charset-supported"/>. See below for
120 additional restrictions.
124 <varlistentry id=
"create-database-strategy" xreflabel=
"CREATE DATABASE STRATEGY">
125 <term><replaceable class=
"parameter">strategy
</replaceable></term>
128 Strategy to be used in creating the new database. If
129 the
<literal>WAL_LOG
</literal> strategy is used, the database will be
130 copied block by block and each block will be separately written
131 to the write-ahead log. This is the most efficient strategy in
132 cases where the template database is small, and therefore it is the
133 default. The older
<literal>FILE_COPY
</literal> strategy is also
134 available. This strategy writes a small record to the write-ahead log
135 for each tablespace used by the target database. Each such record
136 represents copying an entire directory to a new location at the
137 filesystem level. While this does reduce the write-ahead
138 log volume substantially, especially if the template database is large,
139 it also forces the system to perform a checkpoint both before and
140 after the creation of the new database. In some situations, this may
141 have a noticeable negative impact on overall system performance.
145 <varlistentry id=
"create-database-locale">
146 <term><replaceable class=
"parameter">locale
</replaceable></term>
149 Sets the default collation order and character classification in the
150 new database. Collation affects the sort order applied to strings,
151 e.g., in queries with
<literal>ORDER BY
</literal>, as well as the order used in indexes
152 on text columns. Character classification affects the categorization
153 of characters, e.g., lower, upper, and digit. Also sets the
154 associated aspects of the operating system environment,
155 <literal>LC_COLLATE
</literal> and
<literal>LC_CTYPE
</literal>. The
156 default is the same setting as the template database. See
<xref
157 linkend=
"collation-managing-create-libc"/> and
<xref
158 linkend=
"collation-managing-create-icu"/> for details.
161 Can be overridden by setting
<xref
162 linkend=
"create-database-lc-collate"/>,
<xref
163 linkend=
"create-database-lc-ctype"/>,
<xref
164 linkend=
"create-database-builtin-locale"/>, or
<xref
165 linkend=
"create-database-icu-locale"/> individually.
168 If
<xref linkend=
"create-database-locale-provider"/> is
169 <literal>builtin
</literal>, then
<replaceable>locale
</replaceable> or
170 <replaceable>builtin_locale
</replaceable> must be specified and set to
171 either
<literal>C
</literal>,
<literal>C.UTF-
8</literal>, or
172 <literal>PG_UNICODE_FAST
</literal>.
176 The other locale settings
<xref linkend=
"guc-lc-messages"/>,
<xref
177 linkend=
"guc-lc-monetary"/>,
<xref linkend=
"guc-lc-numeric"/>, and
178 <xref linkend=
"guc-lc-time"/> are not fixed per database and are not
179 set by this command. If you want to make them the default for a
180 specific database, you can use
<literal>ALTER DATABASE
186 <varlistentry id=
"create-database-lc-collate">
187 <term><replaceable class=
"parameter">lc_collate
</replaceable></term>
190 Sets
<literal>LC_COLLATE
</literal> in the database server's operating
191 system environment. The default is the setting of
<xref
192 linkend=
"create-database-locale"/> if specified, otherwise the same
193 setting as the template database. See below for additional
197 If
<xref linkend=
"create-database-locale-provider"/> is
198 <literal>libc
</literal>, also sets the default collation order to use
199 in the new database, overriding the setting
<xref
200 linkend=
"create-database-locale"/>.
204 <varlistentry id=
"create-database-lc-ctype">
205 <term><replaceable class=
"parameter">lc_ctype
</replaceable></term>
208 Sets
<literal>LC_CTYPE
</literal> in the database server's operating
209 system environment. The default is the setting of
<xref
210 linkend=
"create-database-locale"/> if specified, otherwise the same
211 setting as the template database. See below for additional
215 If
<xref linkend=
"create-database-locale-provider"/> is
216 <literal>libc
</literal>, also sets the default character
217 classification to use in the new database, overriding the setting
218 <xref linkend=
"create-database-locale"/>.
223 <varlistentry id=
"create-database-builtin-locale">
224 <term><replaceable class=
"parameter">builtin_locale
</replaceable></term>
227 Specifies the builtin provider locale for the database default
228 collation order and character classification, overriding the setting
229 <xref linkend=
"create-database-locale"/>. The
<link
230 linkend=
"create-database-locale-provider">locale provider
</link> must
231 be
<literal>builtin
</literal>. The default is the setting of
<xref
232 linkend=
"create-database-locale"/> if specified; otherwise the same
233 setting as the template database.
236 The locales available for the
<literal>builtin
</literal> provider are
237 <literal>C
</literal>,
<literal>C.UTF-
8</literal> and
238 <literal>PG_UNICODE_FAST
</literal>.
243 <varlistentry id=
"create-database-icu-locale">
244 <term><replaceable class=
"parameter">icu_locale
</replaceable></term>
247 Specifies the ICU locale (see
<xref
248 linkend=
"collation-managing-create-icu"/>) for the database default
249 collation order and character classification, overriding the setting
250 <xref linkend=
"create-database-locale"/>. The
<link
251 linkend=
"create-database-locale-provider">locale provider
</link> must be ICU. The default
252 is the setting of
<xref linkend=
"create-database-locale"/> if
253 specified; otherwise the same setting as the template database.
258 <varlistentry id=
"create-database-icu-rules">
259 <term><replaceable class=
"parameter">icu_rules
</replaceable></term>
262 Specifies additional collation rules to customize the behavior of the
263 default collation of this database. This is supported for ICU only.
264 See
<xref linkend=
"icu-tailoring-rules"/> for details.
269 <varlistentry id=
"create-database-locale-provider">
270 <term><replaceable>locale_provider
</replaceable></term>
274 Specifies the provider to use for the default collation in this
275 database. Possible values are
<literal>builtin
</literal>,
276 <literal>icu
</literal><indexterm><primary>ICU
</primary></indexterm>
277 (if the server was built with ICU support) or
<literal>libc
</literal>.
278 By default, the provider is the same as that of the
<xref
279 linkend=
"create-database-template"/>. See
<xref
280 linkend=
"locale-providers"/> for details.
285 <varlistentry id=
"create-database-collation-version">
286 <term><replaceable>collation_version
</replaceable></term>
290 Specifies the collation version string to store with the database.
291 Normally, this should be omitted, which will cause the version to be
292 computed from the actual version of the database collation as provided
293 by the operating system. This option is intended to be used by
294 <command>pg_upgrade
</command> for copying the version from an existing
299 See also
<xref linkend=
"sql-alterdatabase"/> for how to handle
300 database collation version mismatches.
304 <varlistentry id=
"create-database-tablespace-name">
305 <term><replaceable class=
"parameter">tablespace_name
</replaceable></term>
308 The name of the tablespace that will be associated with the
309 new database, or
<literal>DEFAULT
</literal> to use the
310 template database's tablespace. This
311 tablespace will be the default tablespace used for objects
312 created in this database. See
313 <xref linkend=
"sql-createtablespace"/>
314 for more information.
319 <varlistentry id=
"create-database-allowconn">
320 <term><replaceable class=
"parameter">allowconn
</replaceable></term>
323 If false then no one can connect to this database. The default is
324 true, allowing connections (except as restricted by other mechanisms,
325 such as
<literal>GRANT
</literal>/
<literal>REVOKE CONNECT
</literal>).
330 <varlistentry id=
"create-database-connlimit">
331 <term><replaceable class=
"parameter">connlimit
</replaceable></term>
334 How many concurrent connections can be made
335 to this database. -
1 (the default) means no limit.
340 <varlistentry id=
"create-database-istemplate">
341 <term><replaceable class=
"parameter">istemplate
</replaceable></term>
344 If true, then this database can be cloned by any user with
<literal>CREATEDB
</literal>
345 privileges; if false (the default), then only superusers or the owner
346 of the database can clone it.
351 <varlistentry id=
"create-database-oid">
352 <term><replaceable class=
"parameter">oid
</replaceable></term>
355 The object identifier to be used for the new database. If this
356 parameter is not specified,
<productname>PostgreSQL
</productname>
357 will choose a suitable OID automatically. This parameter is primarily
358 intended for internal use by
<application>pg_upgrade
</application>,
359 and only
<application>pg_upgrade
</application> can specify a value
368 Optional parameters can be written in any order, not only the order
377 <command>CREATE DATABASE
</command> cannot be executed inside a transaction
382 Errors along the line of
<quote>could not initialize database directory
</quote>
383 are most likely related to insufficient permissions on the data
384 directory, a full disk, or other file system problems.
388 Use
<link linkend=
"sql-dropdatabase"><command>DROP DATABASE
</command></link> to remove a database.
392 The program
<xref linkend=
"app-createdb"/> is a
393 wrapper program around this command, provided for convenience.
397 Database-level configuration parameters (set via
<link
398 linkend=
"sql-alterdatabase"><command>ALTER DATABASE
</command></link>) and database-level permissions (set via
399 <link linkend=
"sql-grant"><command>GRANT
</command></link>) are not copied from the template database.
403 Although it is possible to copy a database other than
<literal>template1
</literal>
404 by specifying its name as the template, this is not (yet) intended as
405 a general-purpose
<quote><command>COPY DATABASE
</command></quote> facility.
406 The principal limitation is that no other sessions can be connected to
407 the template database while it is being copied.
<command>CREATE
408 DATABASE
</command> will fail if any other connection exists when it starts;
409 otherwise, new connections to the template database are locked out
410 until
<command>CREATE DATABASE
</command> completes.
411 See
<xref linkend=
"manage-ag-templatedbs"/> for more information.
415 The character set encoding specified for the new database must be
416 compatible with the chosen locale settings (
<literal>LC_COLLATE
</literal> and
417 <literal>LC_CTYPE
</literal>). If the locale is
<literal>C
</literal> (or equivalently
418 <literal>POSIX
</literal>), then all encodings are allowed, but for other
419 locale settings there is only one encoding that will work properly.
420 (On Windows, however, UTF-
8 encoding can be used with any locale.)
421 <command>CREATE DATABASE
</command> will allow superusers to specify
422 <literal>SQL_ASCII
</literal> encoding regardless of the locale settings,
423 but this choice is deprecated and may result in misbehavior of
424 character-string functions if data that is not encoding-compatible
425 with the locale is stored in the database.
429 The encoding and locale settings must match those of the template database,
430 except when
<literal>template0
</literal> is used as template. This is because
431 other databases might contain data that does not match the specified
432 encoding, or might contain indexes whose sort ordering is affected by
433 <literal>LC_COLLATE
</literal> and
<literal>LC_CTYPE
</literal>. Copying such data would
434 result in a database that is corrupt according to the new settings.
435 <literal>template0
</literal>, however, is known to not contain any data or
436 indexes that would be affected.
440 There is currently no option to use a database locale with nondeterministic
441 comparisons (see
<link linkend=
"sql-createcollation"><command>CREATE
442 COLLATION
</command></link> for an explanation). If this is needed, then
443 per-column collations would need to be used.
447 The
<literal>CONNECTION LIMIT
</literal> option is only enforced approximately;
448 if two new sessions start at about the same time when just one
449 connection
<quote>slot
</quote> remains for the database, it is possible that
450 both will fail. Also, the limit is not enforced against superusers or
451 background worker processes.
456 <title>Examples
</title>
459 To create a new database:
462 CREATE DATABASE lusiadas;
467 To create a database
<literal>sales
</literal> owned by user
<literal>salesapp
</literal>
468 with a default tablespace of
<literal>salesspace
</literal>:
471 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
476 To create a database
<literal>music
</literal> with a different locale:
478 CREATE DATABASE music
482 In this example, the
<literal>TEMPLATE template0
</literal> clause is required if
483 the specified locale is different from the one in
<literal>template1
</literal>.
484 (If it is not, then specifying the locale explicitly is redundant.)
488 To create a database
<literal>music2
</literal> with a different locale and a
489 different character set encoding:
491 CREATE DATABASE music2
492 LOCALE 'sv_SE.iso885915'
496 The specified locale and encoding settings must match, or an error will be
501 Note that locale names are specific to the operating system, so that the
502 above commands might not work in the same way everywhere.
507 <title>Compatibility
</title>
510 There is no
<command>CREATE DATABASE
</command> statement in the SQL
511 standard. Databases are equivalent to catalogs, whose creation is
512 implementation-defined.
517 <title>See Also
</title>
519 <simplelist type=
"inline">
520 <member><xref linkend=
"sql-alterdatabase"/></member>
521 <member><xref linkend=
"sql-dropdatabase"/></member>