-<REFENTRY ID="SQL-CREATEDATABASE">
- <REFMETA>
- <REFENTRYTITLE>
- CREATE DATABASE
- </REFENTRYTITLE>
- <REFMISCINFO>SQL - Language Statements</REFMISCINFO>
- </REFMETA>
- <REFNAMEDIV>
- <REFNAME>
- CREATE DATABASE
- </REFNAME>
- <REFPURPOSE>
- Creates a new database
- </REFPURPOSE>
- <REFSYNOPSISDIV>
- <REFSYNOPSISDIVINFO>
- <DATE>1998-04-15</DATE>
- </REFSYNOPSISDIVINFO>
- <SYNOPSIS>
-CREATE DATABASE <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE> [ WITH LOCATION = '<replaceable class="parameter">dbpath</replaceable>' ]
- </SYNOPSIS>
-
- <REFSECT2 ID="R2-SQL-CREATEDATABASE-1">
- <REFSECT2INFO>
- <DATE>1998-04-15</DATE>
- </REFSECT2INFO>
- <TITLE>
- Inputs
- </TITLE>
- <PARA>
- <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">dbpath</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- An alternate location can be specified as either an
- environment variable known to the backend server
- (e.g. '<envar>PGDATA2</envar>') or as an absolute path name
- (e.g. '<filename>/usr/local/pgsql/data</filename>').
- In either case, the location must be pre-configured
- by <command>initlocation</command>.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- </VARIABLELIST>
- </REFSECT2>
-
- <REFSECT2 ID="R2-SQL-CREATEDATABASE-2">
- <REFSECT2INFO>
- <DATE>1998-04-15</DATE>
- </REFSECT2INFO>
- <TITLE>
- Outputs
- </TITLE>
- <PARA>
- </PARA>
- <VARIABLELIST>
- <VARLISTENTRY>
- <TERM>
- <replaceable>status</replaceable>
- </TERM>
- <LISTITEM>
- <PARA>
- <VARIABLELIST>
- <VARLISTENTRY>
- <TERM>
- <ReturnValue>CREATEDB</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- Message returned if the command completes successfully.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <ReturnValue>WARN: createdb: database "<replaceable class="parameter">name</replaceable>" already exists.</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
- This occurs if <replaceable class="parameter">database</replaceable> specified already exists.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- <VARLISTENTRY>
- <TERM>
- <ReturnValue>ERROR: Unable to create database directory <replaceable class="parameter">directory</replaceable>
-</ReturnValue>
- </TERM>
- <LISTITEM>
- <PARA>
-There was a problem with creating the required directory; this operation will
- need permissions for the <literal>postgres</literal> user on the specified location.
- </PARA>
- </LISTITEM>
- </VARLISTENTRY>
- </variablelist>
- </LISTITEM>
- </VARLISTENTRY>
- </VARIABLELIST>
- </REFSECT2>
- </REFSYNOPSISDIV>
+<!--
+doc/src/sgml/ref/create_database.sgml
+PostgreSQL documentation
+-->
+
+<refentry id="SQL-CREATEDATABASE">
+ <indexterm zone="sql-createdatabase">
+ <primary>CREATE DATABASE</primary>
+ </indexterm>
+
+ <refmeta>
+ <refentrytitle>CREATE DATABASE</refentrytitle>
+ <manvolnum>7</manvolnum>
+ <refmiscinfo>SQL - Language Statements</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+ <refname>CREATE DATABASE</refname>
+ <refpurpose>create a new database</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+<synopsis>
+CREATE DATABASE <replaceable class="PARAMETER">name</replaceable>
+ [ [ WITH ] [ OWNER [=] <replaceable class="parameter">user_name</replaceable> ]
+ [ TEMPLATE [=] <replaceable class="parameter">template</replaceable> ]
+ [ ENCODING [=] <replaceable class="parameter">encoding</replaceable> ]
+ [ LC_COLLATE [=] <replaceable class="parameter">lc_collate</replaceable> ]
+ [ LC_CTYPE [=] <replaceable class="parameter">lc_ctype</replaceable> ]
+ [ TABLESPACE [=] <replaceable class="parameter">tablespace_name</replaceable> ]
+ [ IS_TEMPLATE [=] <replaceable class="parameter">istemplate</replaceable> ]
+ [ ALLOW_CONNECTIONS [=] <replaceable class="parameter">allowconn</replaceable> ]
+ [ CONNECTION LIMIT [=] <replaceable class="parameter">connlimit</replaceable> ] ]
+</synopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+ <title>Description</title>
+
+ <para>
+ <command>CREATE DATABASE</command> creates a new
+ <productname>PostgreSQL</productname> database.
+ </para>
+
+ <para>
+ To create a database, you must be a superuser or have the special
+ <literal>CREATEDB</> privilege.
+ See <xref linkend="SQL-CREATEUSER">.
+ </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 might have been added to
+ <literal>template1</>.
+ </para>
+ </refsect1>
+
+ <refsect1>
+ <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">user_name</replaceable></term>
+ <listitem>
+ <para>
+ The role name of the user who will own the new database,
+ or <literal>DEFAULT</literal> to use the default (namely, the
+ 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.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">template</replaceable></term>
+ <listitem>
+ <para>
+ 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
+ a string constant (e.g., <literal>'SQL_ASCII'</literal>),
+ or an integer encoding number, or <literal>DEFAULT</literal>
+ to use the default encoding (namely, the encoding of the
+ template database). The character sets supported by the
+ <productname>PostgreSQL</productname> server are described in
+ <xref linkend="multibyte-charset-supported">. See below for
+ additional restrictions.
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><replaceable class="parameter">lc_collate</replaceable></term>
+ <listitem>
+ <para>
+ Collation order (<literal>LC_COLLATE</>) to use in the new database.
+ This affects the sort order applied to strings, e.g. in queries with
+ 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>
+ <term><replaceable class="parameter">lc_ctype</replaceable></term>
+ <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>
+ <varlistentry>
+ <term><replaceable class="parameter">tablespace_name</replaceable></term>
+ <listitem>
+ <para>
+ 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
+ <xref linkend="sql-createtablespace">
+ for more information.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <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>
- <REFSECT1 ID="R1-SQL-CREATEDATABASE-1">
- <REFSECT1INFO>
- <DATE>1998-04-15</DATE>
- </REFSECT1INFO>
- <TITLE>
- Description
- </TITLE>
- <PARA>
- <command>CREATE DATABASE</command> creates a new Postgres database.
- The creator becomes the administrator of the new database.
- </PARA>
-
- <REFSECT2 ID="R2-SQL-CREATEDATABASE-3">
- <REFSECT2INFO>
- <DATE>1998-04-15</DATE>
- </REFSECT2INFO>
- <TITLE>
- Notes
- </TITLE>
- <PARA>
- <command>CREATE DATABASE</command> is a <productname>Postgres</productname>
- language extension.
- </PARA>
- <para>
- Use <command>DROP DATABASE</command> to remove a database.
- </para>
- </REFSECT2>
-
- <REFSECT1 ID="R1-SQL-CREATEDATABASE-2">
- <TITLE>
- Usage
- </TITLE>
- <PARA>
- To create a new database:
- </PARA>
- <ProgramListing>
-<prompt>olly=></prompt> <userinput>create database lusiadas;</userinput>
- </ProgramListing>
- <PARA>
- To create a new database in an alternate area <filename>~/private_db</filename>:
- </PARA>
- <ProgramListing>
-<prompt>$</prompt> <userinput>mkdir private_db</userinput>
-<prompt>$</prompt> <userinput>initlocation ~/private_db</userinput>
-<computeroutput>Creating Postgres database system directory /home/olly/private_db/base</computeroutput>
-
-<prompt>$</prompt> <userinput>psql olly</userinput>
-<computeroutput>Welcome to the POSTGRESQL interactive sql monitor:
- Please read the file COPYRIGHT for copyright terms of POSTGRESQL
-
- type \? for help on slash commands
- type \q to quit
- type \g or terminate with semicolon to execute query
- You are currently connected to the database: template1
-
-<prompt>olly=></prompt></computeroutput> <userinput>create database elsewhere with location = '/home/olly/private_db';</userinput>
- <computeroutput>CREATEDB</computeroutput>
- </ProgramListing>
- </REFSECT1>
+ <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>
- <REFSECT1 ID="R1-SQL-CREATEDATABASE-3">
- <TITLE>
- Bugs
- </TITLE>
- <PARA>
- There are security and data integrity issues
- involved with using alternate database locations
- specified with absolute path names, and by default
-only an environment variable known to the backend may be
-specified for an alternate location.
- See the Administrator's Guide for more information.
- </PARA>
+ <varlistentry>
+ <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>
+ </variablelist>
+
+ <para>
+ Optional parameters can be written in any order, not only the order
+ illustrated above.
+ </para>
</refsect1>
-<!--
-comment from Olly; response from Thomas...
- <comment>
- initlocation does not create a PG_VERSION file in the specified location.
- How will Postgres handle the situation if it is upgraded to an
- incompatible database version?
- </comment>
-Hmm. This isn't an issue since the upgrade would do a dump/reload from the main database area also.
-Not sure if the dump/reload would guarantee that the alternate data area gets refreshed though...
--->
-
- <REFSECT1 ID="R1-SQL-CREATEDATABASE-4">
- <TITLE>
- Compatibility
- </TITLE>
- <PARA>
-
- <REFSECT2 ID="R2-SQL-CREATEDATABASE-4">
- <REFSECT2INFO>
- <DATE>1998-04-15</DATE>
- </REFSECT2INFO>
- <TITLE>
- SQL92
- </TITLE>
- <PARA>
- There is no <command>CREATE DATABASE</command> statement in SQL92.
- </PARA>
+ <refsect1>
+ <title>Notes</title>
+
+ <para>
+ <command>CREATE DATABASE</> cannot be executed inside a transaction
+ block.
+ </para>
+
+ <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.
+ </para>
+
<para>
- The equivalent command in standard SQL is <command>CREATE SCHEMA</command>.
+ Use <xref linkend="SQL-DROPDATABASE"> to remove a database.
</para>
- </refsect2>
+
+ <para>
+ The program <xref linkend="APP-CREATEDB"> is a
+ wrapper program around this command, provided for convenience.
+ </para>
+
+ <para>
+ Database-level configuration parameters (set via <xref
+ linkend="sql-alterdatabase">) are not copied from the template
+ database.
+ </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
+ a general-purpose <quote><command>COPY DATABASE</command></quote> facility.
+ 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.
+ See <xref linkend="manage-ag-templatedbs"> for more information.
+ </para>
+
+ <para>
+ The character set encoding specified for the new database must be
+ compatible with the chosen locale settings (<literal>LC_COLLATE</> and
+ <literal>LC_CTYPE</>). If the locale is <literal>C</> (or equivalently
+ <literal>POSIX</>), then all encodings are allowed, but for other
+ locale settings there is only one encoding that will work properly.
+ (On Windows, however, UTF-8 encoding can be used with any locale.)
+ <command>CREATE DATABASE</> will allow superusers to specify
+ <literal>SQL_ASCII</> encoding regardless of the locale settings,
+ 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>
+
+ <para>
+ 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.
+ </para>
+
+ <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>
</refsect1>
-</REFENTRY>
-
-
-<!-- Keep this comment at the end of the file
-Local variables:
-mode: sgml
-sgml-omittag:t
-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:
--->
+
+ <refsect1>
+ <title>Examples</title>
+
+ <para>
+ To create a new database:
+
+<programlisting>
+CREATE DATABASE lusiadas;
+</programlisting>
+ </para>
+
+ <para>
+ To create a database <literal>sales</> owned by user <literal>salesapp</>
+ with a default tablespace of <literal>salesspace</>:
+
+<programlisting>
+CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
+</programlisting>
+ </para>
+
+ <para>
+ To create a database <literal>music</> which supports the ISO-8859-1
+ character set:
+
+<programlisting>
+CREATE DATABASE music ENCODING 'LATIN1' TEMPLATE template0;
+</programlisting>
+
+ 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.
+ </para>
+ </refsect1>
+
+ <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>
+ </refsect1>
+
+ <refsect1>
+ <title>See Also</title>
+
+ <simplelist type="inline">
+ <member><xref linkend="sql-alterdatabase"></member>
+ <member><xref linkend="sql-dropdatabase"></member>
+ </simplelist>
+ </refsect1>
+
+</refentry>