1CREATE DATABASE(7) PostgreSQL 15.4 Documentation CREATE DATABASE(7)
2
3
4
6 CREATE_DATABASE - create a new database
7
9 CREATE DATABASE name
10 [ WITH ] [ OWNER [=] user_name ]
11 [ TEMPLATE [=] template ]
12 [ ENCODING [=] encoding ]
13 [ STRATEGY [=] strategy ] ]
14 [ LOCALE [=] locale ]
15 [ LC_COLLATE [=] lc_collate ]
16 [ LC_CTYPE [=] lc_ctype ]
17 [ ICU_LOCALE [=] icu_locale ]
18 [ LOCALE_PROVIDER [=] locale_provider ]
19 [ COLLATION_VERSION = collation_version ]
20 [ TABLESPACE [=] tablespace_name ]
21 [ ALLOW_CONNECTIONS [=] allowconn ]
22 [ CONNECTION LIMIT [=] connlimit ]
23 [ IS_TEMPLATE [=] istemplate ]
24 [ OID [=] oid ]
25
27 CREATE DATABASE creates a new PostgreSQL database.
28
29 To create a database, you must be a superuser or have the special
30 CREATEDB privilege. See CREATE ROLE (CREATE_ROLE(7)).
31
32 By default, the new database will be created by cloning the standard
33 system database template1. A different template can be specified by
34 writing TEMPLATE name. In particular, by writing TEMPLATE template0,
35 you can create a pristine database (one where no user-defined objects
36 exist and where the system objects have not been altered) containing
37 only the standard objects predefined by your version of PostgreSQL.
38 This is useful if you wish to avoid copying any installation-local
39 objects that might have been added to template1.
40
42 name
43 The name of a database to create.
44
45 user_name
46 The role name of the user who will own the new database, or DEFAULT
47 to use the default (namely, the user executing the command). To
48 create a database owned by another role, you must be a direct or
49 indirect member of that role, or be a superuser.
50
51 template
52 The name of the template from which to create the new database, or
53 DEFAULT to use the default template (template1).
54
55 encoding
56 Character set encoding to use in the new database. Specify a string
57 constant (e.g., 'SQL_ASCII'), or an integer encoding number, or
58 DEFAULT to use the default encoding (namely, the encoding of the
59 template database). The character sets supported by the PostgreSQL
60 server are described in Section 24.3.1. See below for additional
61 restrictions.
62
63 strategy
64 Strategy to be used in creating the new database. If the WAL_LOG
65 strategy is used, the database will be copied block by block and
66 each block will be separately written to the write-ahead log. This
67 is the most efficient strategy in cases where the template database
68 is small, and therefore it is the default. The older FILE_COPY
69 strategy is also available. This strategy writes a small record to
70 the write-ahead log for each tablespace used by the target
71 database. Each such record represents copying an entire directory
72 to a new location at the filesystem level. While this does reduce
73 the write-ahead log volume substantially, especially if the
74 template database is large, it also forces the system to perform a
75 checkpoint both before and after the creation of the new database.
76 In some situations, this may have a noticeable negative impact on
77 overall system performance.
78
79 locale
80 This is a shortcut for setting LC_COLLATE and LC_CTYPE at once.
81
82 Tip
83 The other locale settings lc_messages, lc_monetary, lc_numeric,
84 and lc_time are not fixed per database and are not set by this
85 command. If you want to make them the default for a specific
86 database, you can use ALTER DATABASE ... SET.
87
88 lc_collate
89 Collation order (LC_COLLATE) to use in the new database. This
90 affects the sort order applied to strings, e.g., in queries with
91 ORDER BY, as well as the order used in indexes on text columns. The
92 default is to use the collation order of the template database. See
93 below for additional restrictions.
94
95 lc_ctype
96 Character classification (LC_CTYPE) to use in the new database.
97 This affects the categorization of characters, e.g., lower, upper
98 and digit. The default is to use the character classification of
99 the template database. See below for additional restrictions.
100
101 icu_locale
102 Specifies the ICU locale ID if the ICU locale provider is used.
103
104 locale_provider
105 Specifies the provider to use for the default collation in this
106 database. Possible values are: icu, libc. libc is the default. The
107 available choices depend on the operating system and build options.
108
109 collation_version
110 Specifies the collation version string to store with the database.
111 Normally, this should be omitted, which will cause the version to
112 be computed from the actual version of the database collation as
113 provided by the operating system. This option is intended to be
114 used by pg_upgrade for copying the version from an existing
115 installation.
116
117 See also ALTER DATABASE (ALTER_DATABASE(7)) for how to handle
118 database collation version mismatches.
119
120 tablespace_name
121 The name of the tablespace that will be associated with the new
122 database, or DEFAULT to use the template database's tablespace.
123 This tablespace will be the default tablespace used for objects
124 created in this database. See CREATE TABLESPACE
125 (CREATE_TABLESPACE(7)) for more information.
126
127 allowconn
128 If false then no one can connect to this database. The default is
129 true, allowing connections (except as restricted by other
130 mechanisms, such as GRANT/REVOKE CONNECT).
131
132 connlimit
133 How many concurrent connections can be made to this database. -1
134 (the default) means no limit.
135
136 istemplate
137 If true, then this database can be cloned by any user with CREATEDB
138 privileges; if false (the default), then only superusers or the
139 owner of the database can clone it.
140
141 oid
142 The object identifier to be used for the new database. If this
143 parameter is not specified, PostgreSQL will choose a suitable OID
144 automatically. This parameter is primarily intended for internal
145 use by pg_upgrade, and only pg_upgrade can specify a value less
146 than 16384.
147
148 Optional parameters can be written in any order, not only the order
149 illustrated above.
150
152 CREATE DATABASE cannot be executed inside a transaction block.
153
154 Errors along the line of “could not initialize database directory” are
155 most likely related to insufficient permissions on the data directory,
156 a full disk, or other file system problems.
157
158 Use DROP DATABASE to remove a database.
159
160 The program createdb(1) is a wrapper program around this command,
161 provided for convenience.
162
163 Database-level configuration parameters (set via ALTER DATABASE) and
164 database-level permissions (set via GRANT) are not copied from the
165 template database.
166
167 Although it is possible to copy a database other than template1 by
168 specifying its name as the template, this is not (yet) intended as a
169 general-purpose “COPY DATABASE” facility. The principal limitation is
170 that no other sessions can be connected to the template database while
171 it is being copied. CREATE DATABASE will fail if any other connection
172 exists when it starts; otherwise, new connections to the template
173 database are locked out until CREATE DATABASE completes. See
174 Section 23.3 for more information.
175
176 The character set encoding specified for the new database must be
177 compatible with the chosen locale settings (LC_COLLATE and LC_CTYPE).
178 If the locale is C (or equivalently POSIX), then all encodings are
179 allowed, but for other locale settings there is only one encoding that
180 will work properly. (On Windows, however, UTF-8 encoding can be used
181 with any locale.) CREATE DATABASE will allow superusers to specify
182 SQL_ASCII encoding regardless of the locale settings, but this choice
183 is deprecated and may result in misbehavior of character-string
184 functions if data that is not encoding-compatible with the locale is
185 stored in the database.
186
187 The encoding and locale settings must match those of the template
188 database, except when template0 is used as template. This is because
189 other databases might contain data that does not match the specified
190 encoding, or might contain indexes whose sort ordering is affected by
191 LC_COLLATE and LC_CTYPE. Copying such data would result in a database
192 that is corrupt according to the new settings. template0, however, is
193 known to not contain any data or indexes that would be affected.
194
195 There is currently no option to use a database locale with
196 nondeterministic comparisons (see CREATE COLLATION for an explanation).
197 If this is needed, then per-column collations would need to be used.
198
199 The CONNECTION LIMIT option is only enforced approximately; if two new
200 sessions start at about the same time when just one connection “slot”
201 remains for the database, it is possible that both will fail. Also, the
202 limit is not enforced against superusers or background worker
203 processes.
204
206 To create a new database:
207
208 CREATE DATABASE lusiadas;
209
210 To create a database sales owned by user salesapp with a default
211 tablespace of salesspace:
212
213 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
214
215 To create a database music with a different locale:
216
217 CREATE DATABASE music
218 LOCALE 'sv_SE.utf8'
219 TEMPLATE template0;
220
221 In this example, the TEMPLATE template0 clause is required if the
222 specified locale is different from the one in template1. (If it is not,
223 then specifying the locale explicitly is redundant.)
224
225 To create a database music2 with a different locale and a different
226 character set encoding:
227
228 CREATE DATABASE music2
229 LOCALE 'sv_SE.iso885915'
230 ENCODING LATIN9
231 TEMPLATE template0;
232
233 The specified locale and encoding settings must match, or an error will
234 be reported.
235
236 Note that locale names are specific to the operating system, so that
237 the above commands might not work in the same way everywhere.
238
240 There is no CREATE DATABASE statement in the SQL standard. Databases
241 are equivalent to catalogs, whose creation is implementation-defined.
242
244 ALTER DATABASE (ALTER_DATABASE(7)), DROP DATABASE (DROP_DATABASE(7))
245
246
247
248PostgreSQL 15.4 2023 CREATE DATABASE(7)