| Server IP : 172.67.134.114 / Your IP : 104.23.197.123 Web Server : Apache/2.4.37 System : Linux almalinux.duckdns.org 4.18.0-553.111.1.el8_10.x86_64 #1 SMP Sun Mar 8 20:06:07 EDT 2026 x86_64 User : ricodeal ( 1046) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/doc/postgresql-docs/html/ |
Upload File : |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CREATE DATABASE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="[email protected]" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-createconversion.html" title="CREATE CONVERSION" /><link rel="next" href="sql-createdomain.html" title="CREATE DOMAIN" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CREATE DATABASE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-createconversion.html" title="CREATE CONVERSION">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 10.23 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-createdomain.html" title="CREATE DOMAIN">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-CREATEDATABASE"><div class="titlepage"></div><a id="id-1.9.3.58.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">CREATE DATABASE</span></h2><p>CREATE DATABASE — create a new database</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">CREATE DATABASE <em class="replaceable"><code>name</code></em>
[ WITH ] [ OWNER [=] <em class="replaceable"><code>user_name</code></em> ]
[ TEMPLATE [=] <em class="replaceable"><code>template</code></em> ]
[ ENCODING [=] <em class="replaceable"><code>encoding</code></em> ]
[ LC_COLLATE [=] <em class="replaceable"><code>lc_collate</code></em> ]
[ LC_CTYPE [=] <em class="replaceable"><code>lc_ctype</code></em> ]
[ TABLESPACE [=] <em class="replaceable"><code>tablespace_name</code></em> ]
[ ALLOW_CONNECTIONS [=] <em class="replaceable"><code>allowconn</code></em> ]
[ CONNECTION LIMIT [=] <em class="replaceable"><code>connlimit</code></em> ]
[ IS_TEMPLATE [=] <em class="replaceable"><code>istemplate</code></em> ]</pre></div><div class="refsect1" id="id-1.9.3.58.5"><h2>Description</h2><p> <code class="command">CREATE DATABASE</code> creates a new
<span class="productname">PostgreSQL</span> database.
</p><p> To create a database, you must be a superuser or have the special
<code class="literal">CREATEDB</code> privilege.
See <a class="xref" href="sql-createuser.html" title="CREATE USER"><span class="refentrytitle">CREATE USER</span></a>.
</p><p> By default, the new database will be created by cloning the standard
system database <code class="literal">template1</code>. A different template can be
specified by writing <code class="literal">TEMPLATE
<em class="replaceable"><code>name</code></em></code>. In particular,
by writing <code class="literal">TEMPLATE template0</code>, you can create a virgin
database containing only the standard objects predefined by your
version of <span class="productname">PostgreSQL</span>. This is useful
if you wish to avoid copying
any installation-local objects that might have been added to
<code class="literal">template1</code>.
</p></div><div class="refsect1" id="id-1.9.3.58.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p> The name of a database to create.
</p></dd><dt><span class="term"><em class="replaceable"><code>user_name</code></em></span></dt><dd><p> The role name of the user who will own the new database,
or <code class="literal">DEFAULT</code> 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.
</p></dd><dt><span class="term"><em class="replaceable"><code>template</code></em></span></dt><dd><p> The name of the template from which to create the new database,
or <code class="literal">DEFAULT</code> to use the default template
(<code class="literal">template1</code>).
</p></dd><dt><span class="term"><em class="replaceable"><code>encoding</code></em></span></dt><dd><p> Character set encoding to use in the new database. Specify
a string constant (e.g., <code class="literal">'SQL_ASCII'</code>),
or an integer encoding number, or <code class="literal">DEFAULT</code>
to use the default encoding (namely, the encoding of the
template database). The character sets supported by the
<span class="productname">PostgreSQL</span> server are described in
<a class="xref" href="multibyte.html#MULTIBYTE-CHARSET-SUPPORTED" title="23.3.1. Supported Character Sets">Section 23.3.1</a>. See below for
additional restrictions.
</p></dd><dt><span class="term"><em class="replaceable"><code>lc_collate</code></em></span></dt><dd><p> Collation order (<code class="literal">LC_COLLATE</code>) 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.
</p></dd><dt><span class="term"><em class="replaceable"><code>lc_ctype</code></em></span></dt><dd><p> Character classification (<code class="literal">LC_CTYPE</code>) 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.
</p></dd><dt><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span></dt><dd><p> The name of the tablespace that will be associated with the
new database, or <code class="literal">DEFAULT</code> to use the
template database's tablespace. This
tablespace will be the default tablespace used for objects
created in this database. See
<a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>
for more information.
</p></dd><dt><span class="term"><em class="replaceable"><code>allowconn</code></em></span></dt><dd><p> If false then no one can connect to this database. The default is
true, allowing connections (except as restricted by other mechanisms,
such as <code class="literal">GRANT</code>/<code class="literal">REVOKE CONNECT</code>).
</p></dd><dt><span class="term"><em class="replaceable"><code>connlimit</code></em></span></dt><dd><p> How many concurrent connections can be made
to this database. -1 (the default) means no limit.
</p></dd><dt><span class="term"><em class="replaceable"><code>istemplate</code></em></span></dt><dd><p> If true, then this database can be cloned by any user with <code class="literal">CREATEDB</code>
privileges; if false (the default), then only superusers or the owner
of the database can clone it.
</p></dd></dl></div><p> Optional parameters can be written in any order, not only the order
illustrated above.
</p></div><div class="refsect1" id="id-1.9.3.58.7"><h2>Notes</h2><p> <code class="command">CREATE DATABASE</code> cannot be executed inside a transaction
block.
</p><p> Errors along the line of <span class="quote">“<span class="quote">could not initialize database directory</span>”</span>
are most likely related to insufficient permissions on the data
directory, a full disk, or other file system problems.
</p><p> Use <a class="xref" href="sql-dropdatabase.html" title="DROP DATABASE"><span class="refentrytitle">DROP DATABASE</span></a> to remove a database.
</p><p> The program <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> is a
wrapper program around this command, provided for convenience.
</p><p> Database-level configuration parameters (set via <a class="xref" href="sql-alterdatabase.html" title="ALTER DATABASE"><span class="refentrytitle">ALTER DATABASE</span></a>) and database-level permissions (set via
<a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a>) are not copied from the template database.
</p><p> Although it is possible to copy a database other than <code class="literal">template1</code>
by specifying its name as the template, this is not (yet) intended as
a general-purpose <span class="quote">“<span class="quote"><code class="command">COPY DATABASE</code></span>”</span> facility.
The principal limitation is that no other sessions can be connected to
the template database while it is being copied. <code class="command">CREATE
DATABASE</code> will fail if any other connection exists when it starts;
otherwise, new connections to the template database are locked out
until <code class="command">CREATE DATABASE</code> completes.
See <a class="xref" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Section 22.3</a> for more information.
</p><p> The character set encoding specified for the new database must be
compatible with the chosen locale settings (<code class="literal">LC_COLLATE</code> and
<code class="literal">LC_CTYPE</code>). If the locale is <code class="literal">C</code> (or equivalently
<code class="literal">POSIX</code>), 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.)
<code class="command">CREATE DATABASE</code> will allow superusers to specify
<code class="literal">SQL_ASCII</code> 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.
</p><p> The encoding and locale settings must match those of the template database,
except when <code class="literal">template0</code> 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
<code class="literal">LC_COLLATE</code> and <code class="literal">LC_CTYPE</code>. Copying such data would
result in a database that is corrupt according to the new settings.
<code class="literal">template0</code>, however, is known to not contain any data or
indexes that would be affected.
</p><p> The <code class="literal">CONNECTION LIMIT</code> option is only enforced approximately;
if two new sessions start at about the same time when just one
connection <span class="quote">“<span class="quote">slot</span>”</span> remains for the database, it is possible that
both will fail. Also, the limit is not enforced against superusers or
background worker processes.
</p></div><div class="refsect1" id="id-1.9.3.58.8"><h2>Examples</h2><p> To create a new database:
</p><pre class="programlisting">CREATE DATABASE lusiadas;</pre><p>
</p><p> To create a database <code class="literal">sales</code> owned by user <code class="literal">salesapp</code>
with a default tablespace of <code class="literal">salesspace</code>:
</p><pre class="programlisting">CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;</pre><p>
</p><p> To create a database <code class="literal">music</code> with a different locale:
</p><pre class="programlisting">CREATE DATABASE music
LC_COLLATE 'sv_SE.utf8' LC_CTYPE 'sv_SE.utf8'
TEMPLATE template0;</pre><p>
In this example, the <code class="literal">TEMPLATE template0</code> clause is required if
the specified locale is different from the one in <code class="literal">template1</code>.
(If it is not, then specifying the locale explicitly is redundant.)
</p><p> To create a database <code class="literal">music2</code> with a different locale and a
different character set encoding:
</p><pre class="programlisting">CREATE DATABASE music2
LC_COLLATE 'sv_SE.iso885915' LC_CTYPE 'sv_SE.iso885915'
ENCODING LATIN9
TEMPLATE template0;</pre><p>
The specified locale and encoding settings must match, or an error will be
reported.
</p><p> Note that locale names are specific to the operating system, so that the
above commands might not work in the same way everywhere.
</p></div><div class="refsect1" id="id-1.9.3.58.9"><h2>Compatibility</h2><p> There is no <code class="command">CREATE DATABASE</code> statement in the SQL
standard. Databases are equivalent to catalogs, whose creation is
implementation-defined.
</p></div><div class="refsect1" id="id-1.9.3.58.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-alterdatabase.html" title="ALTER DATABASE"><span class="refentrytitle">ALTER DATABASE</span></a>, <a class="xref" href="sql-dropdatabase.html" title="DROP DATABASE"><span class="refentrytitle">DROP DATABASE</span></a></span></div></div><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-createconversion.html" title="CREATE CONVERSION">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createdomain.html" title="CREATE DOMAIN">Next</a></td></tr><tr><td width="40%" align="left" valign="top">CREATE CONVERSION </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 10.23 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE DOMAIN</td></tr></table></div></body></html>