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