1CREATE DATABASE(7) PostgreSQL 13.3 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 [ LOCALE [=] locale ]
14 [ LC_COLLATE [=] lc_collate ]
15 [ LC_CTYPE [=] lc_ctype ]
16 [ TABLESPACE [=] tablespace_name ]
17 [ ALLOW_CONNECTIONS [=] allowconn ]
18 [ CONNECTION LIMIT [=] connlimit ]
19 [ IS_TEMPLATE [=] istemplate ] ]
20
22 CREATE DATABASE creates a new PostgreSQL database.
23
24 To create a database, you must be a superuser or have the special
25 CREATEDB privilege. See CREATE ROLE (CREATE_ROLE(7)).
26
27 By default, the new database will be created by cloning the standard
28 system database template1. A different template can be specified by
29 writing TEMPLATE name. In particular, by writing TEMPLATE template0,
30 you can create a pristine database (one where no user-defined objects
31 exist and where the system objects have not been altered) containing
32 only the standard objects predefined by your version of PostgreSQL.
33 This is useful if you wish to avoid copying any installation-local
34 objects that might have been added to template1.
35
37 name
38 The name of a database to create.
39
40 user_name
41 The role name of the user who will own the new database, or DEFAULT
42 to use the default (namely, the user executing the command). To
43 create a database owned by another role, you must be a direct or
44 indirect member of that role, or be a superuser.
45
46 template
47 The name of the template from which to create the new database, or
48 DEFAULT to use the default template (template1).
49
50 encoding
51 Character set encoding to use in the new database. Specify a string
52 constant (e.g., 'SQL_ASCII'), or an integer encoding number, or
53 DEFAULT to use the default encoding (namely, the encoding of the
54 template database). The character sets supported by the PostgreSQL
55 server are described in Section 23.3.1. See below for additional
56 restrictions.
57
58 locale
59 This is a shortcut for setting LC_COLLATE and LC_CTYPE at once. If
60 you specify this, you cannot specify either of those parameters.
61
62 Tip
63 The other locale settings lc_messages, lc_monetary, lc_numeric,
64 and lc_time are not fixed per database and are not set by this
65 command. If you want to make them the default for a specific
66 database, you can use ALTER DATABASE ... SET.
67
68 lc_collate
69 Collation order (LC_COLLATE) to use in the new database. This
70 affects the sort order applied to strings, e.g., in queries with
71 ORDER BY, as well as the order used in indexes on text columns. The
72 default is to use the collation order of the template database. See
73 below for additional restrictions.
74
75 lc_ctype
76 Character classification (LC_CTYPE) to use in the new database.
77 This affects the categorization of characters, e.g., lower, upper
78 and digit. The default is to use the character classification of
79 the template database. See below for additional restrictions.
80
81 tablespace_name
82 The name of the tablespace that will be associated with the new
83 database, or DEFAULT to use the template database's tablespace.
84 This tablespace will be the default tablespace used for objects
85 created in this database. See CREATE TABLESPACE
86 (CREATE_TABLESPACE(7)) for more information.
87
88 allowconn
89 If false then no one can connect to this database. The default is
90 true, allowing connections (except as restricted by other
91 mechanisms, such as GRANT/REVOKE CONNECT).
92
93 connlimit
94 How many concurrent connections can be made to this database. -1
95 (the default) means no limit.
96
97 istemplate
98 If true, then this database can be cloned by any user with CREATEDB
99 privileges; if false (the default), then only superusers or the
100 owner of the database can clone it.
101
102 Optional parameters can be written in any order, not only the order
103 illustrated above.
104
106 CREATE DATABASE cannot be executed inside a transaction block.
107
108 Errors along the line of “could not initialize database directory” are
109 most likely related to insufficient permissions on the data directory,
110 a full disk, or other file system problems.
111
112 Use DROP DATABASE (DROP_DATABASE(7)) to remove a database.
113
114 The program createdb(1) is a wrapper program around this command,
115 provided for convenience.
116
117 Database-level configuration parameters (set via ALTER DATABASE
118 (ALTER_DATABASE(7))) and database-level permissions (set via GRANT(7))
119 are not copied from the template database.
120
121 Although it is possible to copy a database other than template1 by
122 specifying its name as the template, this is not (yet) intended as a
123 general-purpose “COPY DATABASE” facility. The principal limitation is
124 that no other sessions can be connected to the template database while
125 it is being copied. CREATE DATABASE will fail if any other connection
126 exists when it starts; otherwise, new connections to the template
127 database are locked out until CREATE DATABASE completes. See
128 Section 22.3 for more information.
129
130 The character set encoding specified for the new database must be
131 compatible with the chosen locale settings (LC_COLLATE and LC_CTYPE).
132 If the locale is C (or equivalently POSIX), then all encodings are
133 allowed, but for other locale settings there is only one encoding that
134 will work properly. (On Windows, however, UTF-8 encoding can be used
135 with any locale.) CREATE DATABASE will allow superusers to specify
136 SQL_ASCII encoding regardless of the locale settings, but this choice
137 is deprecated and may result in misbehavior of character-string
138 functions if data that is not encoding-compatible with the locale is
139 stored in the database.
140
141 The encoding and locale settings must match those of the template
142 database, except when template0 is used as template. This is because
143 other databases might contain data that does not match the specified
144 encoding, or might contain indexes whose sort ordering is affected by
145 LC_COLLATE and LC_CTYPE. Copying such data would result in a database
146 that is corrupt according to the new settings. template0, however, is
147 known to not contain any data or indexes that would be affected.
148
149 The CONNECTION LIMIT option is only enforced approximately; if two new
150 sessions start at about the same time when just one connection “slot”
151 remains for the database, it is possible that both will fail. Also, the
152 limit is not enforced against superusers or background worker
153 processes.
154
156 To create a new database:
157
158 CREATE DATABASE lusiadas;
159
160 To create a database sales owned by user salesapp with a default
161 tablespace of salesspace:
162
163 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
164
165 To create a database music with a different locale:
166
167 CREATE DATABASE music
168 LOCALE 'sv_SE.utf8'
169 TEMPLATE template0;
170
171 In this example, the TEMPLATE template0 clause is required if the
172 specified locale is different from the one in template1. (If it is not,
173 then specifying the locale explicitly is redundant.)
174
175 To create a database music2 with a different locale and a different
176 character set encoding:
177
178 CREATE DATABASE music2
179 LOCALE 'sv_SE.iso885915'
180 ENCODING LATIN9
181 TEMPLATE template0;
182
183 The specified locale and encoding settings must match, or an error will
184 be reported.
185
186 Note that locale names are specific to the operating system, so that
187 the above commands might not work in the same way everywhere.
188
190 There is no CREATE DATABASE statement in the SQL standard. Databases
191 are equivalent to catalogs, whose creation is implementation-defined.
192
194 ALTER DATABASE (ALTER_DATABASE(7)), DROP DATABASE (DROP_DATABASE(7))
195
196
197
198PostgreSQL 13.3 2021 CREATE DATABASE(7)