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