postgresql/doc/src/sgml/ref/create_database.sgml

257 lines
7.9 KiB
Plaintext
Raw Normal View History

<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/create_database.sgml,v 1.38 2004/03/23 02:47:35 neilc Exp $
PostgreSQL documentation
-->
<refentry id="SQL-CREATEDATABASE">
<refmeta>
<refentrytitle id="sql-createdatabase-title">CREATE DATABASE</refentrytitle>
<refmiscinfo>SQL - Language Statements</refmiscinfo>
</refmeta>
2003-04-22 12:08:08 +02:00
<refnamediv>
2003-04-22 12:08:08 +02:00
<refname>CREATE DATABASE</refname>
<refpurpose>create a new database</refpurpose>
</refnamediv>
2003-04-22 12:08:08 +02:00
2003-08-31 19:32:24 +02:00
<indexterm zone="sql-createdatabase">
<primary>CREATE DATABASE</primary>
</indexterm>
<refsynopsisdiv>
2003-04-22 12:08:08 +02:00
<synopsis>
CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
[ [ WITH ] [ OWNER [=] <replaceable class="parameter">dbowner</replaceable> ]
[ LOCATION [=] '<replaceable class="parameter">dbpath</replaceable>' ]
[ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
[ ENCODING [=] <replaceable class="parameter">encoding</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.
See <xref linkend="SQL-CREATEUSER" endterm="SQL-CREATEUSER-title">.
</para>
2003-04-22 12:08:08 +02:00
<para>
Normally, the creator becomes the owner of the new database.
Superusers can create databases owned by other users using the
<literal>OWNER</> clause. They can even create databases owned by
users with no special privileges. Non-superusers with <literal>CREATEDB</>
privilege can only create databases owned by themselves.
</para>
<para>
An alternative location can be specified in order to,
for example, store the database on a different disk.
The path must have been prepared with the
<xref linkend="APP-INITLOCATION" endterm="APP-INITLOCATION-title">
command.
</para>
<para>
If the path name does not contain a slash, it is interpreted
as an environment variable name, which must be known to the
server process. This way the database administrator can
exercise control over locations in which databases can be created.
(A customary choice is, e.g., <envar>PGDATA2</envar>.)
If the server is compiled with <literal>ALLOW_ABSOLUTE_DBPATHS</literal>
(not so by default), absolute path names, as identified by
a leading slash
(e.g., <filename>/usr/local/pgsql/data</filename>),
are allowed as well.
2003-09-12 02:12:47 +02:00
In either case, the final path name must be absolute and must not
contain any single quotes.
2003-04-22 12:08:08 +02:00
</para>
<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
any installation-local objects that may have been added to
<literal>template1</>.
</para>
<para>
The optional encoding parameter allows selection of the database
encoding. When not specified, it defaults to the encoding used by
the selected template database.
</para>
</refsect1>
<refsect1>
2003-09-12 02:12:47 +02:00
<title>Parameters</title>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">name</replaceable></term>
<listitem>
<para>
The name of a database to create.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">dbowner</replaceable></term>
<listitem>
<para>
2003-04-22 12:08:08 +02:00
The name of the database user who will own the new database,
or <literal>DEFAULT</literal> to use the default (namely, the
user executing the command).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">dbpath</replaceable></term>
<listitem>
<para>
2002-01-20 23:19:57 +01:00
An alternate file-system location in which to store the new database,
specified as a string literal;
or <literal>DEFAULT</literal> to use the default location.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">template</replaceable></term>
<listitem>
<para>
2003-04-22 12:08:08 +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>).
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">encoding</replaceable></term>
<listitem>
<para>
Character set encoding to use in the new database. Specify
2003-04-22 12:08:08 +02:00
a string constant (e.g., <literal>'SQL_ASCII'</literal>),
or an integer encoding number, or <literal>DEFAULT</literal>
to use the default encoding. The character sets supported by the
<productname>PostgreSQL</productname> server are described in
<xref linkend="multibyte-charset-supported">.
</para>
</listitem>
</varlistentry>
</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>
2003-09-12 02:12:47 +02:00
<para>
<command>CREATE DATABASE</> cannot be executed inside a transaction
block.
</para>
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
directory, a full disk, or other file system problems. When using an
alternate location, the user under
which the database server is running must have access to the location.
</para>
1998-05-13 07:34:00 +02:00
<para>
Use <xref linkend="SQL-DROPDATABASE" endterm="SQL-DROPDATABASE-title"> to remove a database.
</para>
<para>
2003-04-22 12:08:08 +02:00
The program <xref linkend="APP-CREATEDB" endterm="APP-CREATEDB-title"> is a
wrapper program around this command, provided for convenience.
</para>
2003-04-22 12:08:08 +02:00
<para>
There are security issues involved with using alternate database
2003-09-12 02:12:47 +02:00
locations specified with absolute path names; this is why the feature
is not enabled by default. See <xref
2003-04-22 12:08:08 +02:00
linkend="manage-ag-alternate-locs"> for more information.
</para>
<para>
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.
We recommend that databases used as templates be treated as read-only.
2003-04-22 12:08:08 +02:00
See <xref linkend="manage-ag-templatedbs"> for more information.
</para>
</refsect1>
2003-04-22 12:08:08 +02:00
<refsect1>
<title>Examples</title>
<para>
1998-05-13 07:34:00 +02:00
To create a new database:
2003-04-22 12:08:08 +02:00
<programlisting>
CREATE DATABASE lusiadas;
</programlisting>
</para>
<para>
2003-04-22 12:08:08 +02:00
To create a new database in an alternate area
<filename>~/private_db</filename>, execute the following from the
shell:
<programlisting>
mkdir private_db
initlocation ~/private_db
</programlisting>
Then execute the following from within a
<application>psql</application> session:
<programlisting>
CREATE DATABASE elsewhere WITH LOCATION '/home/olly/private_db';
</programlisting>
</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>
</refentry>
1998-05-13 07:34:00 +02:00
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
1998-05-13 07:34:00 +02:00
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
-->