Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sql-createdatabase.html265 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!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="pgsql-docs@lists.postgresql.org" /><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 id="docContent" class="container-fluid col-10"><div 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 16.3 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 /></div><div class="refentry" id="SQL-CREATEDATABASE"><div class="titlepage"></div><a id="id-1.9.3.61.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">3CREATE DATABASE <em class="replaceable"><code>name</code></em>4    [ WITH ] [ OWNER [=] <em class="replaceable"><code>user_name</code></em> ]5           [ TEMPLATE [=] <em class="replaceable"><code>template</code></em> ]6           [ ENCODING [=] <em class="replaceable"><code>encoding</code></em> ]7           [ STRATEGY [=] <em class="replaceable"><code>strategy</code></em> ]8           [ LOCALE [=] <em class="replaceable"><code>locale</code></em> ]9           [ LC_COLLATE [=] <em class="replaceable"><code>lc_collate</code></em> ]10           [ LC_CTYPE [=] <em class="replaceable"><code>lc_ctype</code></em> ]11           [ ICU_LOCALE [=] <em class="replaceable"><code>icu_locale</code></em> ]12           [ ICU_RULES [=] <em class="replaceable"><code>icu_rules</code></em> ]13           [ LOCALE_PROVIDER [=] <em class="replaceable"><code>locale_provider</code></em> ]14           [ COLLATION_VERSION = <em class="replaceable"><code>collation_version</code></em> ]15           [ TABLESPACE [=] <em class="replaceable"><code>tablespace_name</code></em> ]16           [ ALLOW_CONNECTIONS [=] <em class="replaceable"><code>allowconn</code></em> ]17           [ CONNECTION LIMIT [=] <em class="replaceable"><code>connlimit</code></em> ]18           [ IS_TEMPLATE [=] <em class="replaceable"><code>istemplate</code></em> ]19           [ OID [=] <em class="replaceable"><code>oid</code></em> ]20</pre></div><div class="refsect1" id="id-1.9.3.61.5"><h2>Description</h2><p>21   <code class="command">CREATE DATABASE</code> creates a new22   <span class="productname">PostgreSQL</span> database.23  </p><p>24   To create a database, you must be a superuser or have the special25   <code class="literal">CREATEDB</code> privilege.26   See <a class="xref" href="sql-createrole.html" title="CREATE ROLE"><span class="refentrytitle">CREATE ROLE</span></a>.27  </p><p>28   By default, the new database will be created by cloning the standard29   system database <code class="literal">template1</code>.  A different template can be30   specified by writing <code class="literal">TEMPLATE31   <em class="replaceable"><code>name</code></em></code>.  In particular,32   by writing <code class="literal">TEMPLATE template0</code>, you can create a pristine33   database (one where no user-defined objects exist and where the system34   objects have not been altered)35   containing only the standard objects predefined by your36   version of <span class="productname">PostgreSQL</span>.  This is useful37   if you wish to avoid copying38   any installation-local objects that might have been added to39   <code class="literal">template1</code>.40  </p></div><div class="refsect1" id="id-1.9.3.61.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt id="CREATE-DATABASE-NAME"><span class="term"><em class="replaceable"><code>name</code></em></span> <a href="#CREATE-DATABASE-NAME" class="id_link">#</a></dt><dd><p>41        The name of a database to create.42       </p></dd><dt id="CREATE-DATABASE-USER-NAME"><span class="term"><em class="replaceable"><code>user_name</code></em></span> <a href="#CREATE-DATABASE-USER-NAME" class="id_link">#</a></dt><dd><p>43        The role name of the user who will own the new database,44        or <code class="literal">DEFAULT</code> to use the default (namely, the45        user executing the command).  To create a database owned by another46        role, you must be able to <code class="literal">SET ROLE</code> to that47        role.48       </p></dd><dt id="CREATE-DATABASE-TEMPLATE"><span class="term"><em class="replaceable"><code>template</code></em></span> <a href="#CREATE-DATABASE-TEMPLATE" class="id_link">#</a></dt><dd><p>49        The name of the template from which to create the new database,50        or <code class="literal">DEFAULT</code> to use the default template51        (<code class="literal">template1</code>).52       </p></dd><dt id="CREATE-DATABASE-ENCODING"><span class="term"><em class="replaceable"><code>encoding</code></em></span> <a href="#CREATE-DATABASE-ENCODING" class="id_link">#</a></dt><dd><p>53        Character set encoding to use in the new database.  Specify54        a string constant (e.g., <code class="literal">'SQL_ASCII'</code>),55        or an integer encoding number, or <code class="literal">DEFAULT</code>56        to use the default encoding (namely, the encoding of the57        template database). The character sets supported by the58        <span class="productname">PostgreSQL</span> server are described in59        <a class="xref" href="multibyte.html#MULTIBYTE-CHARSET-SUPPORTED" title="24.3.1. Supported Character Sets">Section 24.3.1</a>. See below for60        additional restrictions.61       </p></dd><dt id="CREATE-DATABASE-STRATEGY"><span class="term"><em class="replaceable"><code>strategy</code></em></span> <a href="#CREATE-DATABASE-STRATEGY" class="id_link">#</a></dt><dd><p>62        Strategy to be used in creating the new database.  If63        the <code class="literal">WAL_LOG</code> strategy is used, the database will be64        copied block by block and each block will be separately written65        to the write-ahead log. This is the most efficient strategy in66        cases where the template database is small, and therefore it is the67        default. The older <code class="literal">FILE_COPY</code> strategy is also68        available. This strategy writes a small record to the write-ahead log69        for each tablespace used by the target database. Each such record70        represents copying an entire directory to a new location at the71        filesystem level. While this does reduce the write-ahead72        log volume substantially, especially if the template database is large,73        it also forces the system to perform a checkpoint both before and74        after the creation of the new database. In some situations, this may75        have a noticeable negative impact on overall system performance.76       </p></dd><dt id="CREATE-DATABASE-LOCALE"><span class="term"><em class="replaceable"><code>locale</code></em></span> <a href="#CREATE-DATABASE-LOCALE" class="id_link">#</a></dt><dd><p>77        Sets the default collation order and character classification in the78        new database.  Collation affects the sort order applied to strings,79        e.g., in queries with <code class="literal">ORDER BY</code>, as well as the order used in indexes80        on text columns.  Character classification affects the categorization81        of characters, e.g., lower, upper, and digit.  Also sets the82        associated aspects of the operating system environment,83        <code class="literal">LC_COLLATE</code> and <code class="literal">LC_CTYPE</code>.  The84        default is the same setting as the template database.  See <a class="xref" href="collation.html#COLLATION-MANAGING-CREATE-LIBC" title="24.2.2.3.1. libc Collations">Section 24.2.2.3.1</a> and <a class="xref" href="collation.html#COLLATION-MANAGING-CREATE-ICU" title="24.2.2.3.2. ICU Collations">Section 24.2.2.3.2</a> for details.85       </p><p>86        Can be overridden by setting <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LC-COLLATE"><em class="replaceable"><code>lc_collate</code></em></a>, <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LC-CTYPE"><em class="replaceable"><code>lc_ctype</code></em></a>, or <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-ICU-LOCALE"><em class="replaceable"><code>icu_locale</code></em></a> individually.87       </p><div class="tip"><h3 class="title">Tip</h3><p>88         The other locale settings <a class="xref" href="runtime-config-client.html#GUC-LC-MESSAGES">lc_messages</a>, <a class="xref" href="runtime-config-client.html#GUC-LC-MONETARY">lc_monetary</a>, <a class="xref" href="runtime-config-client.html#GUC-LC-NUMERIC">lc_numeric</a>, and89         <a class="xref" href="runtime-config-client.html#GUC-LC-TIME">lc_time</a> are not fixed per database and are not90         set by this command.  If you want to make them the default for a91         specific database, you can use <code class="literal">ALTER DATABASE92         ... SET</code>.93        </p></div></dd><dt id="CREATE-DATABASE-LC-COLLATE"><span class="term"><em class="replaceable"><code>lc_collate</code></em></span> <a href="#CREATE-DATABASE-LC-COLLATE" class="id_link">#</a></dt><dd><p>94        Sets <code class="literal">LC_COLLATE</code> in the database server's operating95        system environment.  The default is the setting of <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a> if specified, otherwise the same96        setting as the template database.  See below for additional97        restrictions.98       </p><p>99        If <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER"><em class="replaceable"><code>locale_provider</code></em></a> is100        <code class="literal">libc</code>, also sets the default collation order to use101        in the new database, overriding the setting <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a>.102       </p></dd><dt id="CREATE-DATABASE-LC-CTYPE"><span class="term"><em class="replaceable"><code>lc_ctype</code></em></span> <a href="#CREATE-DATABASE-LC-CTYPE" class="id_link">#</a></dt><dd><p>103        Sets <code class="literal">LC_CTYPE</code> in the database server's operating104        system environment.  The default is the setting of <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a> if specified, otherwise the same105        setting as the template database.  See below for additional106        restrictions.107       </p><p>108        If <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER"><em class="replaceable"><code>locale_provider</code></em></a> is109        <code class="literal">libc</code>, also sets the default character110        classification to use in the new database, overriding the setting111        <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a>.112       </p></dd><dt id="CREATE-DATABASE-ICU-LOCALE"><span class="term"><em class="replaceable"><code>icu_locale</code></em></span> <a href="#CREATE-DATABASE-ICU-LOCALE" class="id_link">#</a></dt><dd><p>113        Specifies the ICU locale (see <a class="xref" href="collation.html#COLLATION-MANAGING-CREATE-ICU" title="24.2.2.3.2. ICU Collations">Section 24.2.2.3.2</a>) for the database default114        collation order and character classification, overriding the setting115        <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a>.  The <a class="link" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE-PROVIDER">locale provider</a> must be ICU.  The default116        is the setting of <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-LOCALE"><em class="replaceable"><code>locale</code></em></a> if117        specified; otherwise the same setting as the template database.118       </p></dd><dt id="CREATE-DATABASE-ICU-RULES"><span class="term"><em class="replaceable"><code>icu_rules</code></em></span> <a href="#CREATE-DATABASE-ICU-RULES" class="id_link">#</a></dt><dd><p>119        Specifies additional collation rules to customize the behavior of the120        default collation of this database.  This is supported for ICU only.121        See <a class="xref" href="collation.html#ICU-TAILORING-RULES" title="24.2.3.4. ICU Tailoring Rules">Section 24.2.3.4</a> for details.122       </p></dd><dt id="CREATE-DATABASE-LOCALE-PROVIDER"><span class="term"><em class="replaceable"><code>locale_provider</code></em></span> <a href="#CREATE-DATABASE-LOCALE-PROVIDER" class="id_link">#</a></dt><dd><p>123        Specifies the provider to use for the default collation in this124        database.  Possible values are125        <code class="literal">icu</code><a id="id-1.9.3.61.6.2.11.2.1.2" class="indexterm"></a>126        (if the server was built with ICU support) or <code class="literal">libc</code>.127        By default, the provider is the same as that of the <a class="xref" href="sql-createdatabase.html#CREATE-DATABASE-TEMPLATE"><em class="replaceable"><code>template</code></em></a>. See <a class="xref" href="locale.html#LOCALE-PROVIDERS" title="24.1.4. Locale Providers">Section 24.1.4</a> for details.128       </p></dd><dt id="CREATE-DATABASE-COLLATION-VERSION"><span class="term"><em class="replaceable"><code>collation_version</code></em></span> <a href="#CREATE-DATABASE-COLLATION-VERSION" class="id_link">#</a></dt><dd><p>129        Specifies the collation version string to store with the database.130        Normally, this should be omitted, which will cause the version to be131        computed from the actual version of the database collation as provided132        by the operating system.  This option is intended to be used by133        <code class="command">pg_upgrade</code> for copying the version from an existing134        installation.135       </p><p>136        See also <a class="xref" href="sql-alterdatabase.html" title="ALTER DATABASE"><span class="refentrytitle">ALTER DATABASE</span></a> for how to handle137        database collation version mismatches.138       </p></dd><dt id="CREATE-DATABASE-TABLESPACE-NAME"><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span> <a href="#CREATE-DATABASE-TABLESPACE-NAME" class="id_link">#</a></dt><dd><p>139        The name of the tablespace that will be associated with the140        new database, or <code class="literal">DEFAULT</code> to use the141        template database's tablespace. This142        tablespace will be the default tablespace used for objects143        created in this database. See144        <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>145        for more information.146       </p></dd><dt id="CREATE-DATABASE-ALLOWCONN"><span class="term"><em class="replaceable"><code>allowconn</code></em></span> <a href="#CREATE-DATABASE-ALLOWCONN" class="id_link">#</a></dt><dd><p>147         If false then no one can connect to this database.  The default is148         true, allowing connections (except as restricted by other mechanisms,149         such as <code class="literal">GRANT</code>/<code class="literal">REVOKE CONNECT</code>).150        </p></dd><dt id="CREATE-DATABASE-CONNLIMIT"><span class="term"><em class="replaceable"><code>connlimit</code></em></span> <a href="#CREATE-DATABASE-CONNLIMIT" class="id_link">#</a></dt><dd><p>151        How many concurrent connections can be made152        to this database.  -1 (the default) means no limit.153       </p></dd><dt id="CREATE-DATABASE-ISTEMPLATE"><span class="term"><em class="replaceable"><code>istemplate</code></em></span> <a href="#CREATE-DATABASE-ISTEMPLATE" class="id_link">#</a></dt><dd><p>154         If true, then this database can be cloned by any user with <code class="literal">CREATEDB</code>155         privileges; if false (the default), then only superusers or the owner156         of the database can clone it.157        </p></dd><dt id="CREATE-DATABASE-OID"><span class="term"><em class="replaceable"><code>oid</code></em></span> <a href="#CREATE-DATABASE-OID" class="id_link">#</a></dt><dd><p>158         The object identifier to be used for the new database. If this159         parameter is not specified, <span class="productname">PostgreSQL</span>160         will choose a suitable OID automatically. This parameter is primarily161         intended for internal use by <span class="application">pg_upgrade</span>,162         and only <span class="application">pg_upgrade</span> can specify a value163         less than 16384.164        </p></dd></dl></div><p>165   Optional parameters can be written in any order, not only the order166   illustrated above.167  </p></div><div class="refsect1" id="id-1.9.3.61.7"><h2>Notes</h2><p>168    <code class="command">CREATE DATABASE</code> cannot be executed inside a transaction169    block.170   </p><p>171    Errors along the line of <span class="quote">“<span class="quote">could not initialize database directory</span>”</span>172    are most likely related to insufficient permissions on the data173    directory, a full disk, or other file system problems.174   </p><p>175    Use <a class="link" href="sql-dropdatabase.html" title="DROP DATABASE"><code class="command">DROP DATABASE</code></a> to remove a database.176   </p><p>177    The program <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> is a178    wrapper program around this command, provided for convenience.179   </p><p>180    Database-level configuration parameters (set via <a class="link" href="sql-alterdatabase.html" title="ALTER DATABASE"><code class="command">ALTER DATABASE</code></a>) and database-level permissions (set via181    <a class="link" href="sql-grant.html" title="GRANT"><code class="command">GRANT</code></a>) are not copied from the template database.182   </p><p>183   Although it is possible to copy a database other than <code class="literal">template1</code>184   by specifying its name as the template, this is not (yet) intended as185   a general-purpose <span class="quote">“<span class="quote"><code class="command">COPY DATABASE</code></span>”</span> facility.186   The principal limitation is that no other sessions can be connected to187   the template database while it is being copied.  <code class="command">CREATE188   DATABASE</code> will fail if any other connection exists when it starts;189   otherwise, new connections to the template database are locked out190   until <code class="command">CREATE DATABASE</code> completes.191   See <a class="xref" href="manage-ag-templatedbs.html" title="23.3. Template Databases">Section 23.3</a> for more information.192  </p><p>193   The character set encoding specified for the new database must be194   compatible with the chosen locale settings (<code class="literal">LC_COLLATE</code> and195   <code class="literal">LC_CTYPE</code>).  If the locale is <code class="literal">C</code> (or equivalently196   <code class="literal">POSIX</code>), then all encodings are allowed, but for other197   locale settings there is only one encoding that will work properly.198   (On Windows, however, UTF-8 encoding can be used with any locale.)199   <code class="command">CREATE DATABASE</code> will allow superusers to specify200   <code class="literal">SQL_ASCII</code> encoding regardless of the locale settings,201   but this choice is deprecated and may result in misbehavior of202   character-string functions if data that is not encoding-compatible203   with the locale is stored in the database.204  </p><p>205   The encoding and locale settings must match those of the template database,206   except when <code class="literal">template0</code> is used as template.  This is because207   other databases might contain data that does not match the specified208   encoding, or might contain indexes whose sort ordering is affected by209   <code class="literal">LC_COLLATE</code> and <code class="literal">LC_CTYPE</code>.  Copying such data would210   result in a database that is corrupt according to the new settings.211   <code class="literal">template0</code>, however, is known to not contain any data or212   indexes that would be affected.213  </p><p>214   There is currently no option to use a database locale with nondeterministic215   comparisons (see <a class="link" href="sql-createcollation.html" title="CREATE COLLATION"><code class="command">CREATE216   COLLATION</code></a> for an explanation).  If this is needed, then217   per-column collations would need to be used.218  </p><p>219   The <code class="literal">CONNECTION LIMIT</code> option is only enforced approximately;220   if two new sessions start at about the same time when just one221   connection <span class="quote">“<span class="quote">slot</span>”</span> remains for the database, it is possible that222   both will fail.  Also, the limit is not enforced against superusers or223   background worker processes.224  </p></div><div class="refsect1" id="id-1.9.3.61.8"><h2>Examples</h2><p>225   To create a new database:226 227</p><pre class="programlisting">228CREATE DATABASE lusiadas;229</pre><p>230  </p><p>231   To create a database <code class="literal">sales</code> owned by user <code class="literal">salesapp</code>232   with a default tablespace of <code class="literal">salesspace</code>:233 234</p><pre class="programlisting">235CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;236</pre><p>237  </p><p>238   To create a database <code class="literal">music</code> with a different locale:239</p><pre class="programlisting">240CREATE DATABASE music241    LOCALE 'sv_SE.utf8'242    TEMPLATE template0;243</pre><p>244    In this example, the <code class="literal">TEMPLATE template0</code> clause is required if245    the specified locale is different from the one in <code class="literal">template1</code>.246    (If it is not, then specifying the locale explicitly is redundant.)247  </p><p>248   To create a database <code class="literal">music2</code> with a different locale and a249   different character set encoding:250</p><pre class="programlisting">251CREATE DATABASE music2252    LOCALE 'sv_SE.iso885915'253    ENCODING LATIN9254    TEMPLATE template0;255</pre><p>256   The specified locale and encoding settings must match, or an error will be257   reported.258  </p><p>259   Note that locale names are specific to the operating system, so that the260   above commands might not work in the same way everywhere.261  </p></div><div class="refsect1" id="id-1.9.3.61.9"><h2>Compatibility</h2><p>262   There is no <code class="command">CREATE DATABASE</code> statement in the SQL263   standard.  Databases are equivalent to catalogs, whose creation is264   implementation-defined.265  </p></div><div class="refsect1" id="id-1.9.3.61.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 class="navfooter"><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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> CREATE DOMAIN</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai