1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2010-09-20 22:08:53 +02:00
|
|
|
doc/src/sgml/ref/create_database.sgml
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refentry id="SQL-CREATEDATABASE">
|
2014-02-24 03:25:35 +01:00
|
|
|
<indexterm zone="sql-createdatabase">
|
|
|
|
<primary>CREATE DATABASE</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmeta>
|
2010-04-03 09:23:02 +02:00
|
|
|
<refentrytitle>CREATE DATABASE</refentrytitle>
|
2008-11-14 11:22:48 +01:00
|
|
|
<manvolnum>7</manvolnum>
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refnamediv>
|
2003-04-22 12:08:08 +02:00
|
|
|
<refname>CREATE DATABASE</refname>
|
|
|
|
<refpurpose>create a new database</refpurpose>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refnamediv>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refsynopsisdiv>
|
2003-04-22 12:08:08 +02:00
|
|
|
<synopsis>
|
2000-11-14 19:37:49 +01:00
|
|
|
CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
|
2009-09-19 12:23:27 +02:00
|
|
|
[ [ WITH ] [ OWNER [=] <replaceable class="parameter">user_name</replaceable> ]
|
2002-02-25 04:37:14 +01:00
|
|
|
[ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
|
2004-06-18 08:14:31 +02:00
|
|
|
[ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ]
|
2009-04-06 10:42:53 +02:00
|
|
|
[ LC_COLLATE [=] <replaceable class="parameter">lc_collate</replaceable> ]
|
|
|
|
[ LC_CTYPE [=] <replaceable class="parameter">lc_ctype</replaceable> ]
|
2012-06-22 00:06:14 +02:00
|
|
|
[ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
|
2014-07-02 02:10:38 +02:00
|
|
|
[ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
|
|
|
|
[ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
|
2005-07-31 19:19:22 +02:00
|
|
|
[ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ] ]
|
2003-04-22 12:08:08 +02:00
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>CREATE DATABASE</command> creates a new
|
|
|
|
<productname>PostgreSQL</productname> database.
|
|
|
|
</para>
|
|
|
|
|
2003-09-12 02:12:47 +02:00
|
|
|
<para>
|
|
|
|
To create a database, you must be a superuser or have the special
|
|
|
|
<literal>CREATEDB</> privilege.
|
2010-04-03 09:23:02 +02:00
|
|
|
See <xref linkend="SQL-CREATEUSER">.
|
2003-09-12 02:12:47 +02:00
|
|
|
</para>
|
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<para>
|
|
|
|
By default, the new database will be created by cloning the standard
|
|
|
|
system database <literal>template1</>. A different template can be
|
|
|
|
specified by writing <literal>TEMPLATE
|
|
|
|
<replaceable class="parameter">name</replaceable></literal>. In particular,
|
|
|
|
by writing <literal>TEMPLATE template0</>, you can create a virgin
|
|
|
|
database containing only the standard objects predefined by your
|
|
|
|
version of <productname>PostgreSQL</productname>. This is useful
|
|
|
|
if you wish to avoid copying
|
Update reference documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 00:26:05 +01:00
|
|
|
any installation-local objects that might have been added to
|
2004-06-18 08:14:31 +02:00
|
|
|
<literal>template1</>.
|
2003-04-22 12:08:08 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
2003-09-12 02:12:47 +02:00
|
|
|
<title>Parameters</title>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-18 08:14:31 +02:00
|
|
|
The name of a database to create.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2002-02-24 21:20:21 +01:00
|
|
|
<varlistentry>
|
2012-03-11 21:56:26 +01:00
|
|
|
<term><replaceable class="parameter">user_name</replaceable></term>
|
2002-02-24 21:20:21 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2012-10-04 19:41:01 +02:00
|
|
|
The role name of the user who will own the new database,
|
2004-06-18 08:14:31 +02:00
|
|
|
or <literal>DEFAULT</literal> to use the default (namely, the
|
2012-10-04 19:41:01 +02:00
|
|
|
user executing the command). To create a database owned by another
|
|
|
|
role, you must be a direct or indirect member of that role,
|
|
|
|
or be a superuser.
|
2002-02-24 21:20:21 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
1999-07-06 19:16:42 +02:00
|
|
|
<varlistentry>
|
2004-06-18 08:14:31 +02:00
|
|
|
<term><replaceable class="parameter">template</replaceable></term>
|
1999-07-06 19:16:42 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-18 08:14:31 +02:00
|
|
|
The name of the template from which to create the new database,
|
|
|
|
or <literal>DEFAULT</literal> to use the default template
|
|
|
|
(<literal>template1</literal>).
|
2000-11-14 19:37:49 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2004-06-18 08:14:31 +02:00
|
|
|
<term><replaceable class="parameter">encoding</replaceable></term>
|
2000-11-14 19:37:49 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-06-18 08:14:31 +02:00
|
|
|
Character set encoding to use in the new database. Specify
|
|
|
|
a string constant (e.g., <literal>'SQL_ASCII'</literal>),
|
|
|
|
or an integer encoding number, or <literal>DEFAULT</literal>
|
2006-05-04 18:07:29 +02:00
|
|
|
to use the default encoding (namely, the encoding of the
|
|
|
|
template database). The character sets supported by the
|
2004-06-18 08:14:31 +02:00
|
|
|
<productname>PostgreSQL</productname> server are described in
|
2007-09-29 00:25:49 +02:00
|
|
|
<xref linkend="multibyte-charset-supported">. See below for
|
|
|
|
additional restrictions.
|
2000-11-14 19:37:49 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2008-09-23 11:20:39 +02:00
|
|
|
<varlistentry>
|
2009-05-06 18:15:21 +02:00
|
|
|
<term><replaceable class="parameter">lc_collate</replaceable></term>
|
2008-09-23 11:20:39 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Collation order (<literal>LC_COLLATE</>) to use in the new database.
|
2009-05-06 18:15:21 +02:00
|
|
|
This affects the sort order applied to strings, e.g. in queries with
|
2008-09-23 11:20:39 +02:00
|
|
|
ORDER BY, as well as the order used in indexes on text columns.
|
|
|
|
The default is to use the collation order of the template database.
|
|
|
|
See below for additional restrictions.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2009-05-06 18:15:21 +02:00
|
|
|
<term><replaceable class="parameter">lc_ctype</replaceable></term>
|
2008-09-23 11:20:39 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Character classification (<literal>LC_CTYPE</>) to use in the new
|
|
|
|
database. This affects the categorization of characters, e.g. lower,
|
|
|
|
upper and digit. The default is to use the character classification of
|
|
|
|
the template database. See below for additional restrictions.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2000-11-14 19:37:49 +01:00
|
|
|
<varlistentry>
|
2012-06-22 00:06:14 +02:00
|
|
|
<term><replaceable class="parameter">tablespace_name</replaceable></term>
|
2000-11-14 19:37:49 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-10-29 05:17:22 +02:00
|
|
|
The name of the tablespace that will be associated with the
|
|
|
|
new database, or <literal>DEFAULT</literal> to use the
|
|
|
|
template database's tablespace. This
|
|
|
|
tablespace will be the default tablespace used for objects
|
|
|
|
created in this database. See
|
2010-04-03 09:23:02 +02:00
|
|
|
<xref linkend="sql-createtablespace">
|
2004-06-18 08:14:31 +02:00
|
|
|
for more information.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2005-07-31 19:19:22 +02:00
|
|
|
|
|
|
|
<varlistentry>
|
2014-07-02 02:10:38 +02:00
|
|
|
<term><replaceable class="parameter">istemplate</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If true, then this database can be cloned by any user with CREATEDB
|
|
|
|
privileges; if false (the default), then only superusers or the owner
|
|
|
|
of the database can clone it.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">allowconn</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If false then no one can connect to this database. The default is
|
|
|
|
true, allowing connections (except as restricted by other mechanisms,
|
|
|
|
such as <literal>GRANT</>/<literal>REVOKE CONNECT</>).
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
2005-07-31 19:19:22 +02:00
|
|
|
<term><replaceable class="parameter">connlimit</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
How many concurrent connections can be made
|
|
|
|
to this database. -1 (the default) means no limit.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
1999-07-06 19:16:42 +02:00
|
|
|
</variablelist>
|
2003-04-22 12:08:08 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Optional parameters can be written in any order, not only the order
|
|
|
|
illustrated above.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
2003-09-12 02:12:47 +02:00
|
|
|
<title>Notes</title>
|
1999-12-12 06:15:10 +01:00
|
|
|
|
2003-09-12 02:12:47 +02:00
|
|
|
<para>
|
|
|
|
<command>CREATE DATABASE</> cannot be executed inside a transaction
|
|
|
|
block.
|
|
|
|
</para>
|
2000-11-14 19:37:49 +01:00
|
|
|
|
2003-09-12 02:12:47 +02:00
|
|
|
<para>
|
|
|
|
Errors along the line of <quote>could not initialize database directory</>
|
|
|
|
are most likely related to insufficient permissions on the data
|
2004-06-18 08:14:31 +02:00
|
|
|
directory, a full disk, or other file system problems.
|
2003-09-12 02:12:47 +02:00
|
|
|
</para>
|
2000-11-14 19:37:49 +01:00
|
|
|
|
1998-05-13 07:34:00 +02:00
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
Use <xref linkend="SQL-DROPDATABASE"> to remove a database.
|
1999-12-12 06:15:10 +01:00
|
|
|
</para>
|
1999-07-22 17:09:15 +02:00
|
|
|
|
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
The program <xref linkend="APP-CREATEDB"> is a
|
2003-04-22 12:08:08 +02:00
|
|
|
wrapper program around this command, provided for convenience.
|
1999-07-22 17:09:15 +02:00
|
|
|
</para>
|
|
|
|
|
2014-04-19 21:26:49 +02:00
|
|
|
<para>
|
|
|
|
Database-level configuration parameters (set via <xref
|
|
|
|
linkend="sql-alterdatabase">) are not copied from the template
|
|
|
|
database.
|
|
|
|
</para>
|
|
|
|
|
2000-11-14 19:37:49 +01:00
|
|
|
<para>
|
2001-11-18 01:38:00 +01:00
|
|
|
Although it is possible to copy a database other than <literal>template1</>
|
|
|
|
by specifying its name as the template, this is not (yet) intended as
|
2003-04-22 12:08:08 +02:00
|
|
|
a general-purpose <quote><command>COPY DATABASE</command></quote> facility.
|
2006-05-04 18:07:29 +02:00
|
|
|
The principal limitation is that no other sessions can be connected to
|
|
|
|
the template database while it is being copied. <command>CREATE
|
|
|
|
DATABASE</> will fail if any other connection exists when it starts;
|
|
|
|
otherwise, new connections to the template database are locked out
|
|
|
|
until <command>CREATE DATABASE</> completes.
|
2003-04-22 12:08:08 +02:00
|
|
|
See <xref linkend="manage-ag-templatedbs"> for more information.
|
2001-08-26 18:56:03 +02:00
|
|
|
</para>
|
2005-07-31 19:19:22 +02:00
|
|
|
|
2007-09-29 00:25:49 +02:00
|
|
|
<para>
|
2008-09-23 11:20:39 +02:00
|
|
|
The character set encoding specified for the new database must be
|
2009-05-06 18:15:21 +02:00
|
|
|
compatible with the chosen locale settings (<literal>LC_COLLATE</> and
|
|
|
|
<literal>LC_CTYPE</>). If the locale is <literal>C</> (or equivalently
|
2007-09-29 00:25:49 +02:00
|
|
|
<literal>POSIX</>), then all encodings are allowed, but for other
|
2008-09-23 11:20:39 +02:00
|
|
|
locale settings there is only one encoding that will work properly.
|
2009-05-06 18:15:21 +02:00
|
|
|
(On Windows, however, UTF-8 encoding can be used with any locale.)
|
2007-09-29 00:25:49 +02:00
|
|
|
<command>CREATE DATABASE</> will allow superusers to specify
|
2009-05-06 18:15:21 +02:00
|
|
|
<literal>SQL_ASCII</> encoding regardless of the locale settings,
|
2007-09-29 00:25:49 +02:00
|
|
|
but this choice is deprecated and may result in misbehavior of
|
|
|
|
character-string functions if data that is not encoding-compatible
|
|
|
|
with the locale is stored in the database.
|
|
|
|
</para>
|
|
|
|
|
2008-09-23 11:20:39 +02:00
|
|
|
<para>
|
2009-05-06 18:15:21 +02:00
|
|
|
The encoding and locale settings must match those of the template database,
|
|
|
|
except when <literal>template0</> is used as template. This is because
|
|
|
|
other databases might contain data that does not match the specified
|
|
|
|
encoding, or might contain indexes whose sort ordering is affected by
|
|
|
|
<literal>LC_COLLATE</> and <literal>LC_CTYPE</>. Copying such data would
|
|
|
|
result in a database that is corrupt according to the new settings.
|
|
|
|
<literal>template0</literal>, however, is known to not contain any data or
|
|
|
|
indexes that would be affected.
|
2008-09-23 11:20:39 +02:00
|
|
|
</para>
|
|
|
|
|
2005-07-31 19:19:22 +02:00
|
|
|
<para>
|
|
|
|
The <literal>CONNECTION LIMIT</> option is only enforced approximately;
|
|
|
|
if two new sessions start at about the same time when just one
|
|
|
|
connection <quote>slot</> remains for the database, it is possible that
|
|
|
|
both will fail. Also, the limit is not enforced against superusers.
|
|
|
|
</para>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
1998-05-13 07:34:00 +02:00
|
|
|
To create a new database:
|
1999-07-06 19:16:42 +02:00
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<programlisting>
|
|
|
|
CREATE DATABASE lusiadas;
|
2004-07-12 03:22:53 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-10-22 16:12:41 +02:00
|
|
|
To create a database <literal>sales</> owned by user <literal>salesapp</>
|
2004-07-12 03:22:53 +02:00
|
|
|
with a default tablespace of <literal>salesspace</>:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2009-05-06 18:15:21 +02:00
|
|
|
To create a database <literal>music</> which supports the ISO-8859-1
|
2004-07-12 03:22:53 +02:00
|
|
|
character set:
|
|
|
|
|
|
|
|
<programlisting>
|
2009-05-06 18:15:21 +02:00
|
|
|
CREATE DATABASE music ENCODING 'LATIN1' TEMPLATE template0;
|
2003-04-22 12:08:08 +02:00
|
|
|
</programlisting>
|
2009-05-06 18:15:21 +02:00
|
|
|
|
|
|
|
In this example, the <literal>TEMPLATE template0</> clause would only
|
|
|
|
be required if <literal>template1</>'s encoding is not ISO-8859-1.
|
|
|
|
Note that changing encoding might require selecting new
|
|
|
|
<literal>LC_COLLATE</> and <literal>LC_CTYPE</> settings as well.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2003-04-22 12:08:08 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
There is no <command>CREATE DATABASE</command> statement in the SQL
|
|
|
|
standard. Databases are equivalent to catalogs, whose creation is
|
|
|
|
implementation-defined.
|
|
|
|
</para>
|
1998-05-13 07:34:00 +02:00
|
|
|
</refsect1>
|
2006-05-04 18:07:29 +02:00
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2010-04-03 09:23:02 +02:00
|
|
|
<member><xref linkend="sql-alterdatabase"></member>
|
|
|
|
<member><xref linkend="sql-dropdatabase"></member>
|
2006-05-04 18:07:29 +02:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
</refentry>
|