1CREATE DATABASE(7) PostgreSQL 11.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))) are not copied from the template database.
107
108 Although it is possible to copy a database other than template1 by
109 specifying its name as the template, this is not (yet) intended as a
110 general-purpose “COPY DATABASE” facility. The principal limitation is
111 that no other sessions can be connected to the template database while
112 it is being copied. CREATE DATABASE will fail if any other connection
113 exists when it starts; otherwise, new connections to the template
114 database are locked out until CREATE DATABASE completes. See
115 Section 22.3 for more information.
116
117 The character set encoding specified for the new database must be
118 compatible with the chosen locale settings (LC_COLLATE and LC_CTYPE).
119 If the locale is C (or equivalently POSIX), then all encodings are
120 allowed, but for other locale settings there is only one encoding that
121 will work properly. (On Windows, however, UTF-8 encoding can be used
122 with any locale.) CREATE DATABASE will allow superusers to specify
123 SQL_ASCII encoding regardless of the locale settings, but this choice
124 is deprecated and may result in misbehavior of character-string
125 functions if data that is not encoding-compatible with the locale is
126 stored in the database.
127
128 The encoding and locale settings must match those of the template
129 database, except when template0 is used as template. This is because
130 other databases might contain data that does not match the specified
131 encoding, or might contain indexes whose sort ordering is affected by
132 LC_COLLATE and LC_CTYPE. Copying such data would result in a database
133 that is corrupt according to the new settings. template0, however, is
134 known to not contain any data or indexes that would be affected.
135
136 The CONNECTION LIMIT option is only enforced approximately; if two new
137 sessions start at about the same time when just one connection “slot”
138 remains for the database, it is possible that both will fail. Also, the
139 limit is not enforced against superusers or background worker
140 processes.
141
143 To create a new database:
144
145 CREATE DATABASE lusiadas;
146
147 To create a database sales owned by user salesapp with a default
148 tablespace of salesspace:
149
150 CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
151
152 To create a database music with a different locale:
153
154 CREATE DATABASE music
155 LC_COLLATE 'sv_SE.utf8' LC_CTYPE 'sv_SE.utf8'
156 TEMPLATE template0;
157
158 In this example, the TEMPLATE template0 clause is required if the
159 specified locale is different from the one in template1. (If it is not,
160 then specifying the locale explicitly is redundant.)
161
162 To create a database music2 with a different locale and a different
163 character set encoding:
164
165 CREATE DATABASE music2
166 LC_COLLATE 'sv_SE.iso885915' LC_CTYPE 'sv_SE.iso885915'
167 ENCODING LATIN9
168 TEMPLATE template0;
169
170 The specified locale and encoding settings must match, or an error will
171 be reported.
172
173 Note that locale names are specific to the operating system, so that
174 the above commands might not work in the same way everywhere.
175
177 There is no CREATE DATABASE statement in the SQL standard. Databases
178 are equivalent to catalogs, whose creation is implementation-defined.
179
181 ALTER DATABASE (ALTER_DATABASE(7)), DROP DATABASE (DROP_DATABASE(7))
182
183
184
185PostgreSQL 11.6 2019 CREATE DATABASE(7)