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