2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/create_database.sgml,v 1.36 2003/09/12 00:12:47 tgl Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-CREATEDATABASE">
8 <refentrytitle id="sql-createdatabase-title">CREATE DATABASE</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
13 <refname>CREATE DATABASE</refname>
14 <refpurpose>create a new database</refpurpose>
17 <indexterm zone="sql-createdatabase">
18 <primary>CREATE DATABASE</primary>
23 CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
24 [ [ WITH ] [ OWNER [=] <replaceable class="parameter">dbowner</replaceable> ]
25 [ LOCATION [=] '<replaceable class="parameter">dbpath</replaceable>' ]
26 [ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
27 [ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ] ]
32 <title>Description</title>
35 <command>CREATE DATABASE</command> creates a new
36 <productname>PostgreSQL</productname> database.
40 To create a database, you must be a superuser or have the special
41 <literal>CREATEDB</> privilege.
42 See <xref linkend="SQL-CREATEUSER" endterm="SQL-CREATEUSER-title">.
46 Normally, the creator becomes the owner of the new database.
47 Superusers can create databases owned by other users using the
48 <literal>OWNER</> clause. They can even create databases owned by
49 users with no special privileges. Non-superusers with <literal>CREATEDB</>
50 privilege can only create databases owned by themselves.
54 An alternative location can be specified in order to,
55 for example, store the database on a different disk.
56 The path must have been prepared with the
57 <xref linkend="APP-INITLOCATION" endterm="APP-INITLOCATION-title">
62 If the path name does not contain a slash, it is interpreted
63 as an environment variable name, which must be known to the
64 server process. This way the database administrator can
65 exercise control over locations in which databases can be created.
66 (A customary choice is, e.g., <envar>PGDATA2</envar>.)
67 If the server is compiled with <literal>ALLOW_ABSOLUTE_DBPATHS</literal>
68 (not so by default), absolute path names, as identified by
70 (e.g., <filename>/usr/local/pgsql/data</filename>),
72 In either case, the final path name must be absolute and must not
73 contain any single quotes.
77 By default, the new database will be created by cloning the standard
78 system database <literal>template1</>. A different template can be
79 specified by writing <literal>TEMPLATE
80 <replaceable class="parameter">name</replaceable></literal>. In particular,
81 by writing <literal>TEMPLATE template0</>, you can create a virgin
82 database containing only the standard objects predefined by your
83 version of <productname>PostgreSQL</productname>. This is useful
84 if you wish to avoid copying
85 any installation-local objects that may have been added to
86 <literal>template1</>.
90 The optional encoding parameter allows selection of the database
91 encoding. When not specified, it defaults to the encoding used by
92 the selected template database.
97 <title>Parameters</title>
101 <term><replaceable class="parameter">name</replaceable></term>
104 The name of a database to create.
109 <term><replaceable class="parameter">dbowner</replaceable></term>
112 The name of the database user who will own the new database,
113 or <literal>DEFAULT</literal> to use the default (namely, the
114 user executing the command).
119 <term><replaceable class="parameter">dbpath</replaceable></term>
122 An alternate file-system location in which to store the new database,
123 specified as a string literal;
124 or <literal>DEFAULT</literal> to use the default location.
129 <term><replaceable class="parameter">template</replaceable></term>
132 The name of the template from which to create the new database,
133 or <literal>DEFAULT</literal> to use the default template
134 (<literal>template1</literal>).
139 <term><replaceable class="parameter">encoding</replaceable></term>
142 Character set encoding to use in the new database. Specify
143 a string constant (e.g., <literal>'SQL_ASCII'</literal>),
144 or an integer encoding number, or <literal>DEFAULT</literal>
145 to use the default encoding.
152 Optional parameters can be written in any order, not only the order
161 <command>CREATE DATABASE</> cannot be executed inside a transaction
166 Errors along the line of <quote>could not initialize database directory</>
167 are most likely related to insufficient permissions on the data
168 directory, a full disk, or other file system problems. When using an
169 alternate location, the user under
170 which the database server is running must have access to the location.
174 Use <xref linkend="SQL-DROPDATABASE" endterm="SQL-DROPDATABASE-title"> to remove a database.
178 The program <xref linkend="APP-CREATEDB" endterm="APP-CREATEDB-title"> is a
179 wrapper program around this command, provided for convenience.
183 There are security issues involved with using alternate database
184 locations specified with absolute path names; this is why the feature
185 is not enabled by default. See <xref
186 linkend="manage-ag-alternate-locs"> for more information.
190 Although it is possible to copy a database other than <literal>template1</>
191 by specifying its name as the template, this is not (yet) intended as
192 a general-purpose <quote><command>COPY DATABASE</command></quote> facility.
193 We recommend that databases used as templates be treated as read-only.
194 See <xref linkend="manage-ag-templatedbs"> for more information.
199 <title>Examples</title>
202 To create a new database:
205 CREATE DATABASE lusiadas;
210 To create a new database in an alternate area
211 <filename>~/private_db</filename>, execute the following from the
216 initlocation ~/private_db
219 Then execute the following from within a
220 <application>psql</application> session:
223 CREATE DATABASE elsewhere WITH LOCATION '/home/olly/private_db';
229 <title>Compatibility</title>
232 There is no <command>CREATE DATABASE</command> statement in the SQL
233 standard. Databases are equivalent to catalogs, whose creation is
234 implementation-defined.
239 <!-- Keep this comment at the end of the file
244 sgml-minimize-attributes:nil
245 sgml-always-quote-attributes:t
248 sgml-parent-document:nil
249 sgml-default-dtd-file:"../reference.ced"
250 sgml-exposed-tags:nil
251 sgml-local-catalogs:"/usr/lib/sgml/catalog"
252 sgml-local-ecat-files:nil