4 CREATE DATABASE — create a new database
9 [ WITH ] [ OWNER [=] user_name ]
10 [ TEMPLATE [=] template ]
11 [ ENCODING [=] encoding ]
12 [ STRATEGY [=] strategy ]
14 [ LC_COLLATE [=] lc_collate ]
15 [ LC_CTYPE [=] lc_ctype ]
16 [ BUILTIN_LOCALE [=] builtin_locale ]
17 [ ICU_LOCALE [=] icu_locale ]
18 [ ICU_RULES [=] icu_rules ]
19 [ LOCALE_PROVIDER [=] locale_provider ]
20 [ COLLATION_VERSION = collation_version ]
21 [ TABLESPACE [=] tablespace_name ]
22 [ ALLOW_CONNECTIONS [=] allowconn ]
23 [ CONNECTION LIMIT [=] connlimit ]
24 [ IS_TEMPLATE [=] istemplate ]
29 CREATE DATABASE creates a new PostgreSQL database.
31 To create a database, you must be a superuser or have the special
32 CREATEDB privilege. See CREATE ROLE.
34 By default, the new database will be created by cloning the standard
35 system database template1. A different template can be specified by
36 writing TEMPLATE name. In particular, by writing TEMPLATE template0,
37 you can create a pristine database (one where no user-defined objects
38 exist and where the system objects have not been altered) containing
39 only the standard objects predefined by your version of PostgreSQL.
40 This is useful if you wish to avoid copying any installation-local
41 objects that might have been added to template1.
46 The name of a database to create.
49 The role name of the user who will own the new database, or
50 DEFAULT to use the default (namely, the user executing the
51 command). To create a database owned by another role, you must
52 be able to SET ROLE to that role.
55 The name of the template from which to create the new database,
56 or DEFAULT to use the default template (template1).
59 Character set encoding to use in the new database. Specify a
60 string constant (e.g., 'SQL_ASCII'), or an integer encoding
61 number, or DEFAULT to use the default encoding (namely, the
62 encoding of the template database). The character sets supported
63 by the PostgreSQL server are described in Section 23.3.1. See
64 below for additional restrictions.
67 Strategy to be used in creating the new database. If the WAL_LOG
68 strategy is used, the database will be copied block by block and
69 each block will be separately written to the write-ahead log.
70 This is the most efficient strategy in cases where the template
71 database is small, and therefore it is the default. The older
72 FILE_COPY strategy is also available. This strategy writes a
73 small record to the write-ahead log for each tablespace used by
74 the target database. Each such record represents copying an
75 entire directory to a new location at the filesystem level.
76 While this does reduce the write-ahead log volume substantially,
77 especially if the template database is large, it also forces the
78 system to perform a checkpoint both before and after the
79 creation of the new database. In some situations, this may have
80 a noticeable negative impact on overall system performance. The
81 FILE_COPY strategy is affected by the file_copy_method setting.
84 Sets the default collation order and character classification in
85 the new database. Collation affects the sort order applied to
86 strings, e.g., in queries with ORDER BY, as well as the order
87 used in indexes on text columns. Character classification
88 affects the categorization of characters, e.g., lower, upper,
89 and digit. Also sets the associated aspects of the operating
90 system environment, LC_COLLATE and LC_CTYPE. The default is the
91 same setting as the template database. See Section 23.2.2.3.1
92 and Section 23.2.2.3.2 for details.
94 Can be overridden by setting lc_collate, lc_ctype,
95 builtin_locale, or icu_locale individually.
97 If locale_provider is builtin, then locale or builtin_locale
98 must be specified and set to either C, C.UTF-8, or
103 The other locale settings lc_messages, lc_monetary, lc_numeric,
104 and lc_time are not fixed per database and are not set by this
105 command. If you want to make them the default for a specific
106 database, you can use ALTER DATABASE ... SET.
109 Sets LC_COLLATE in the database server's operating system
110 environment. The default is the setting of locale if specified,
111 otherwise the same setting as the template database. See below
112 for additional restrictions.
114 If locale_provider is libc, also sets the default collation
115 order to use in the new database, overriding the setting locale.
118 Sets LC_CTYPE in the database server's operating system
119 environment. The default is the setting of locale if specified,
120 otherwise the same setting as the template database. See below
121 for additional restrictions.
123 If locale_provider is libc, also sets the default character
124 classification to use in the new database, overriding the
128 Specifies the builtin provider locale for the database default
129 collation order and character classification, overriding the
130 setting locale. The locale provider must be builtin. The default
131 is the setting of locale if specified; otherwise the same
132 setting as the template database.
134 The locales available for the builtin provider are C, C.UTF-8
138 Specifies the ICU locale (see Section 23.2.2.3.2) for the
139 database default collation order and character classification,
140 overriding the setting locale. The locale provider must be ICU.
141 The default is the setting of locale if specified; otherwise the
142 same setting as the template database.
145 Specifies additional collation rules to customize the behavior
146 of the default collation of this database. This is supported for
147 ICU only. See Section 23.2.3.4 for details.
150 Specifies the provider to use for the default collation in this
151 database. Possible values are builtin, icu (if the server was
152 built with ICU support) or libc. By default, the provider is the
153 same as that of the template. See Section 23.1.4 for details.
156 Specifies the collation version string to store with the
157 database. Normally, this should be omitted, which will cause the
158 version to be computed from the actual version of the database
159 collation as provided by the operating system. This option is
160 intended to be used by pg_upgrade for copying the version from
161 an existing installation.
163 See also ALTER DATABASE for how to handle database collation
167 The name of the tablespace that will be associated with the new
168 database, or DEFAULT to use the template database's tablespace.
169 This tablespace will be the default tablespace used for objects
170 created in this database. See CREATE TABLESPACE for more
174 If false then no one can connect to this database. The default
175 is true, allowing connections (except as restricted by other
176 mechanisms, such as GRANT/REVOKE CONNECT).
179 How many concurrent connections can be made to this database. -1
180 (the default) means no limit.
183 If true, then this database can be cloned by any user with
184 CREATEDB privileges; if false (the default), then only
185 superusers or the owner of the database can clone it.
188 The object identifier to be used for the new database. If this
189 parameter is not specified, PostgreSQL will choose a suitable
190 OID automatically. This parameter is primarily intended for
191 internal use by pg_upgrade, and only pg_upgrade can specify a
192 value less than 16384.
194 Optional parameters can be written in any order, not only the order
199 CREATE DATABASE cannot be executed inside a transaction block.
201 Errors along the line of “could not initialize database directory” are
202 most likely related to insufficient permissions on the data directory,
203 a full disk, or other file system problems.
205 Use DROP DATABASE to remove a database.
207 The program createdb is a wrapper program around this command, provided
210 Database-level configuration parameters (set via ALTER DATABASE) and
211 database-level permissions (set via GRANT) are not copied from the
214 Although it is possible to copy a database other than template1 by
215 specifying its name as the template, this is not (yet) intended as a
216 general-purpose “COPY DATABASE” facility. The principal limitation is
217 that no other sessions can be connected to the template database while
218 it is being copied. CREATE DATABASE will fail if any other connection
219 exists when it starts; otherwise, new connections to the template
220 database are locked out until CREATE DATABASE completes. See
221 Section 22.3 for more information.
223 The character set encoding specified for the new database must be
224 compatible with the chosen locale settings (LC_COLLATE and LC_CTYPE).
225 If the locale is C (or equivalently POSIX), then all encodings are
226 allowed, but for other locale settings there is only one encoding that
227 will work properly. (On Windows, however, UTF-8 encoding can be used
228 with any locale.) CREATE DATABASE will allow superusers to specify
229 SQL_ASCII encoding regardless of the locale settings, but this choice
230 is deprecated and may result in misbehavior of character-string
231 functions if data that is not encoding-compatible with the locale is
232 stored in the database.
234 The encoding and locale settings must match those of the template
235 database, except when template0 is used as template. This is because
236 other databases might contain data that does not match the specified
237 encoding, or might contain indexes whose sort ordering is affected by
238 LC_COLLATE and LC_CTYPE. Copying such data would result in a database
239 that is corrupt according to the new settings. template0, however, is
240 known to not contain any data or indexes that would be affected.
242 There is currently no option to use a database locale with
243 nondeterministic comparisons (see CREATE COLLATION for an explanation).
244 If this is needed, then per-column collations would need to be used.
246 The CONNECTION LIMIT option is only enforced approximately; if two new
247 sessions start at about the same time when just one connection “slot”
248 remains for the database, it is possible that both will fail. Also, the
249 limit is not enforced against superusers or background worker
254 To create a new database:
255 CREATE DATABASE lusiadas;
257 To create a database sales owned by user salesapp with a default
258 tablespace of salesspace:
259 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
261 To create a database music with a different locale:
262 CREATE DATABASE music
266 In this example, the TEMPLATE template0 clause is required if the
267 specified locale is different from the one in template1. (If it is not,
268 then specifying the locale explicitly is redundant.)
270 To create a database music2 with a different locale and a different
271 character set encoding:
272 CREATE DATABASE music2
273 LOCALE 'sv_SE.iso885915'
277 The specified locale and encoding settings must match, or an error will
280 Note that locale names are specific to the operating system, so that
281 the above commands might not work in the same way everywhere.
285 There is no CREATE DATABASE statement in the SQL standard. Databases
286 are equivalent to catalogs, whose creation is implementation-defined.
290 ALTER DATABASE, DROP DATABASE