2 .\" Title: CREATE DATABASE
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "CREATE DATABASE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 CREATE_DATABASE \- create a new database
35 CREATE DATABASE \fIname\fR
36 [ WITH ] [ OWNER [=] \fIuser_name\fR ]
37 [ TEMPLATE [=] \fItemplate\fR ]
38 [ ENCODING [=] \fIencoding\fR ]
39 [ STRATEGY [=] \fIstrategy\fR ]
40 [ LOCALE [=] \fIlocale\fR ]
41 [ LC_COLLATE [=] \fIlc_collate\fR ]
42 [ LC_CTYPE [=] \fIlc_ctype\fR ]
43 [ BUILTIN_LOCALE [=] \fIbuiltin_locale\fR ]
44 [ ICU_LOCALE [=] \fIicu_locale\fR ]
45 [ ICU_RULES [=] \fIicu_rules\fR ]
46 [ LOCALE_PROVIDER [=] \fIlocale_provider\fR ]
47 [ COLLATION_VERSION = \fIcollation_version\fR ]
48 [ TABLESPACE [=] \fItablespace_name\fR ]
49 [ ALLOW_CONNECTIONS [=] \fIallowconn\fR ]
50 [ CONNECTION LIMIT [=] \fIconnlimit\fR ]
51 [ IS_TEMPLATE [=] \fIistemplate\fR ]
61 To create a database, you must be a superuser or have the special
64 CREATE ROLE (\fBCREATE_ROLE\fR(7))\&.
66 By default, the new database will be created by cloning the standard system database
67 template1\&. A different template can be specified by writing
68 TEMPLATE \fIname\fR\&. In particular, by writing
69 TEMPLATE template0, you can create a pristine database (one where no user\-defined objects exist and where the system objects have not been altered) containing only the standard objects predefined by your version of
70 PostgreSQL\&. This is useful if you wish to avoid copying any installation\-local objects that might have been added to
76 The name of a database to create\&.
81 The role name of the user who will own the new database, or
83 to use the default (namely, the user executing the command)\&. To create a database owned by another role, you must be able to
90 The name of the template from which to create the new database, or
92 to use the default template (template1)\&.
97 Character set encoding to use in the new database\&. Specify a string constant (e\&.g\&.,
98 \*(AqSQL_ASCII\*(Aq), or an integer encoding number, or
100 to use the default encoding (namely, the encoding of the template database)\&. The character sets supported by the
102 server are described in
103 Section\ \&23.3.1\&. See below for additional restrictions\&.
108 Strategy to be used in creating the new database\&. If the
110 strategy is used, the database will be copied block by block and each block will be separately written to the write\-ahead log\&. This is the most efficient strategy in cases where the template database is small, and therefore it is the default\&. The older
112 strategy is also available\&. This strategy writes a small record to the write\-ahead log for each tablespace used by the target database\&. Each such record represents copying an entire directory to a new location at the filesystem level\&. While this does reduce the write\-ahead log volume substantially, especially if the template database is large, it also forces the system to perform a checkpoint both before and after the creation of the new database\&. In some situations, this may have a noticeable negative impact on overall system performance\&. The
114 strategy is affected by the
121 Sets the default collation order and character classification in the new database\&. Collation affects the sort order applied to strings, e\&.g\&., in queries with
122 ORDER BY, as well as the order used in indexes on text columns\&. Character classification affects the categorization of characters, e\&.g\&., lower, upper, and digit\&. Also sets the associated aspects of the operating system environment,
125 LC_CTYPE\&. The default is the same setting as the template database\&. See
126 Section\ \&23.2.2.3.1
128 Section\ \&23.2.2.3.2
131 Can be overridden by setting
134 \fIbuiltin_locale\fR, or
139 \fIlocale_provider\fR
145 must be specified and set to either
154 .nr an-no-space-flag 1
161 The other locale settings
166 are not fixed per database and are not set by this command\&. If you want to make them the default for a specific database, you can use
167 ALTER DATABASE \&.\&.\&. SET\&.
176 in the database server\*(Aqs operating system environment\&. The default is the setting of
178 if specified, otherwise the same setting as the template database\&. See below for additional restrictions\&.
181 \fIlocale_provider\fR
183 libc, also sets the default collation order to use in the new database, overriding the setting
191 in the database server\*(Aqs operating system environment\&. The default is the setting of
193 if specified, otherwise the same setting as the template database\&. See below for additional restrictions\&.
196 \fIlocale_provider\fR
198 libc, also sets the default character classification to use in the new database, overriding the setting
204 Specifies the builtin provider locale for the database default collation order and character classification, overriding the setting
208 builtin\&. The default is the setting of
210 if specified; otherwise the same setting as the template database\&.
212 The locales available for the
223 Specifies the ICU locale (see
224 Section\ \&23.2.2.3.2) for the database default collation order and character classification, overriding the setting
227 must be ICU\&. The default is the setting of
229 if specified; otherwise the same setting as the template database\&.
234 Specifies additional collation rules to customize the behavior of the default collation of this database\&. This is supported for ICU only\&. See
239 \fIlocale_provider\fR
241 Specifies the provider to use for the default collation in this database\&. Possible values are
244 (if the server was built with ICU support) or
245 libc\&. By default, the provider is the same as that of the
246 \fItemplate\fR\&. See
251 \fIcollation_version\fR
253 Specifies the collation version string to store with the database\&. Normally, this should be omitted, which will cause the version to be computed from the actual version of the database collation as provided by the operating system\&. This option is intended to be used by
255 for copying the version from an existing installation\&.
258 ALTER DATABASE (\fBALTER_DATABASE\fR(7))
259 for how to handle database collation version mismatches\&.
262 \fItablespace_name\fR
264 The name of the tablespace that will be associated with the new database, or
266 to use the template database\*(Aqs tablespace\&. This tablespace will be the default tablespace used for objects created in this database\&. See
267 CREATE TABLESPACE (\fBCREATE_TABLESPACE\fR(7))
268 for more information\&.
273 If false then no one can connect to this database\&. The default is true, allowing connections (except as restricted by other mechanisms, such as
274 GRANT/REVOKE CONNECT)\&.
279 How many concurrent connections can be made to this database\&. \-1 (the default) means no limit\&.
284 If true, then this database can be cloned by any user with
286 privileges; if false (the default), then only superusers or the owner of the database can clone it\&.
291 The object identifier to be used for the new database\&. If this parameter is not specified,
293 will choose a suitable OID automatically\&. This parameter is primarily intended for internal use by
296 can specify a value less than 16384\&.
299 Optional parameters can be written in any order, not only the order illustrated above\&.
302 \fBCREATE DATABASE\fR
303 cannot be executed inside a transaction block\&.
305 Errors along the line of
306 \(lqcould not initialize database directory\(rq
307 are most likely related to insufficient permissions on the data directory, a full disk, or other file system problems\&.
311 to remove a database\&.
315 is a wrapper program around this command, provided for convenience\&.
317 Database\-level configuration parameters (set via
318 \fBALTER DATABASE\fR) and database\-level permissions (set via
319 \fBGRANT\fR) are not copied from the template database\&.
321 Although it is possible to copy a database other than
323 by specifying its name as the template, this is not (yet) intended as a general\-purpose
324 \(lq\fBCOPY DATABASE\fR\(rq
325 facility\&. The principal limitation is that no other sessions can be connected to the template database while it is being copied\&.
326 \fBCREATE DATABASE\fR
327 will fail if any other connection exists when it starts; otherwise, new connections to the template database are locked out until
328 \fBCREATE DATABASE\fR
331 for more information\&.
333 The character set encoding specified for the new database must be compatible with the chosen locale settings (LC_COLLATE
335 LC_CTYPE)\&. If the locale is
338 POSIX), then all encodings are allowed, but for other locale settings there is only one encoding that will work properly\&. (On Windows, however, UTF\-8 encoding can be used with any locale\&.)
339 \fBCREATE DATABASE\fR
340 will allow superusers to specify
342 encoding regardless of the locale settings, but this choice is deprecated and may result in misbehavior of character\-string functions if data that is not encoding\-compatible with the locale is stored in the database\&.
344 The encoding and locale settings must match those of the template database, except when
346 is used as template\&. This is because other databases might contain data that does not match the specified encoding, or might contain indexes whose sort ordering is affected by
349 LC_CTYPE\&. Copying such data would result in a database that is corrupt according to the new settings\&.
350 template0, however, is known to not contain any data or indexes that would be affected\&.
352 There is currently no option to use a database locale with nondeterministic comparisons (see
353 \fBCREATE COLLATION\fR
354 for an explanation)\&. If this is needed, then per\-column collations would need to be used\&.
358 option is only enforced approximately; if two new sessions start at about the same time when just one connection
360 remains for the database, it is possible that both will fail\&. Also, the limit is not enforced against superusers or background worker processes\&.
363 To create a new database:
369 CREATE DATABASE lusiadas;
379 with a default tablespace of
386 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
394 with a different locale:
400 CREATE DATABASE music
401 LOCALE \*(Aqsv_SE\&.utf8\*(Aq
410 clause is required if the specified locale is different from the one in
411 template1\&. (If it is not, then specifying the locale explicitly is redundant\&.)
415 with a different locale and a different character set encoding:
421 CREATE DATABASE music2
422 LOCALE \*(Aqsv_SE\&.iso885915\*(Aq
430 The specified locale and encoding settings must match, or an error will be reported\&.
432 Note that locale names are specific to the operating system, so that the above commands might not work in the same way everywhere\&.
436 \fBCREATE DATABASE\fR
437 statement in the SQL standard\&. Databases are equivalent to catalogs, whose creation is implementation\-defined\&.
439 ALTER DATABASE (\fBALTER_DATABASE\fR(7)), DROP DATABASE (\fBDROP_DATABASE\fR(7))