1CREATE DATABASE(7)               SQL Commands               CREATE DATABASE(7)
2
3
4

NAME

6       CREATE DATABASE - create a new database
7
8

SYNOPSIS

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

DESCRIPTION

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       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

PARAMETERS

35       name   The name of a database to create.
36
37       dbowner
38              The role name of the user who will  own  the  new  database,  or
39              DEFAULT  to use the default (namely, the user executing the com‐
40              mand). To create a database owned by another role, you must be a
41              direct or indirect member of that role, or be a superuser.
42
43       template
44              The  name of the template from which to create the new database,
45              or DEFAULT to use the default template (template1).
46
47       encoding
48              Character set encoding to use in the  new  database.  Specify  a
49              string constant (e.g., 'SQL_ASCII'), or an integer encoding num‐
50              ber, or DEFAULT to use the default encoding (namely, the  encod‐
51              ing  of  the template database). The character sets supported by
52              the PostgreSQL server are described in in the documentation. See
53              below for additional restrictions.
54
55       lc_collate
56              Collation  order  (LC_COLLATE) to use in the new database.  This
57              affects the sort order applied to strings, e.g. in queries  with
58              ORDER  BY, as well as the order used in indexes on text columns.
59              The default is to use the collation order of the template  data‐
60              base.  See below for additional restrictions.
61
62       lc_ctype
63              Character  classification (LC_CTYPE) to use in the new database.
64              This affects the categorization of characters, e.g. lower, upper
65              and digit. The default is to use the character classification of
66              the template database. See below for additional restrictions.
67
68       tablespace
69              The name of the tablespace that will be associated with the  new
70              database,  or DEFAULT to use the template database's tablespace.
71              This tablespace will be the default tablespace used for  objects
72              created   in   this   database.   See  CREATE  TABLESPACE  [cre‐
73              ate_tablespace(7)] for more information.
74
75       connlimit
76              How many concurrent connections can be made to this database. -1
77              (the default) means no limit.
78
79       Optional  parameters  can  be  written in any order, not only the order
80       illustrated above.
81

NOTES

83       CREATE DATABASE cannot be executed inside a transaction block.
84
85       Errors along the line of ``could not  initialize  database  directory''
86       are  most likely related to insufficient permissions on the data direc‐
87       tory, a full disk, or other file system problems.
88
89       Use DROP DATABASE [drop_database(7)] to remove a database.
90
91       The program createdb [createdb(1)] is a  wrapper  program  around  this
92       command, provided for convenience.
93
94       Although  it  is  possible  to  copy a database other than template1 by
95       specifying its name as the template, this is not (yet)  intended  as  a
96       general-purpose  ``COPY  DATABASE'' facility.  The principal limitation
97       is that no other sessions can be connected  to  the  template  database
98       while  it  is being copied. CREATE DATABASE will fail if any other con‐
99       nection exists when it starts; otherwise, new connections to  the  tem‐
100       plate  database are locked out until CREATE DATABASE completes.  See in
101       the documentation for more information.
102
103       The character set encoding specified for the new database must be  com‐
104       patible  with  the chosen locale settings (LC_COLLATE and LC_CTYPE). If
105       the locale is  C  (or  equivalently  POSIX),  then  all  encodings  are
106       allowed,  but for other locale settings there is only one encoding that
107       will work properly.  (On Windows, however, UTF-8 encoding can  be  used
108       with  any  locale.)   CREATE  DATABASE will allow superusers to specify
109       SQL_ASCII encoding regardless of the locale settings, but  this  choice
110       is  deprecated  and may result in misbehavior of character-string func‐
111       tions if data that is not encoding-compatible with the locale is stored
112       in the database.
113
114       The encoding and locale settings must match those of the template data‐
115       base, except when template0 is used as template. This is because  other
116       databases  might  contain data that does not match the specified encod‐
117       ing, or might contain  indexes  whose  sort  ordering  is  affected  by
118       LC_COLLATE  and  LC_CTYPE. Copying such data would result in a database
119       that is corrupt according to the new settings.  template0, however,  is
120       known to not contain any data or indexes that would be affected.
121
122       The  CONNECTION LIMIT option is only enforced approximately; if two new
123       sessions start at about the same time when just one connection ``slot''
124       remains for the database, it is possible that both will fail. Also, the
125       limit is not enforced against superusers.
126

EXAMPLES

128       To create a new database:
129
130       CREATE DATABASE lusiadas;
131
132
133       To create a database sales  owned  by  user  salesapp  with  a  default
134       tablespace of salesspace:
135
136       CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
137
138
139       To create a database music which supports the ISO-8859-1 character set:
140
141       CREATE DATABASE music ENCODING 'LATIN1' TEMPLATE template0;
142
143       In  this  example, the TEMPLATE template0 clause would only be required
144       if template1's encoding is not ISO-8859-1.  Note that changing encoding
145       might require selecting new LC_COLLATE and LC_CTYPE settings as well.
146

COMPATIBILITY

148       There  is  no  CREATE DATABASE statement in the SQL standard. Databases
149       are equivalent to catalogs, whose creation is implementation-defined.
150

SEE ALSO

152       ALTER DATABASE [alter_database(7)], DROP DATABASE [drop_database(7)]
153
154
155
156SQL - Language Statements         2014-02-17                CREATE DATABASE(7)
Impressum