gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

178 行
14KB

  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 V1.79.1" /><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 12.4 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.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">
  3. CREATE 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. [ LC_COLLATE [=] <em class="replaceable"><code>lc_collate</code></em> ]
  8. [ LC_CTYPE [=] <em class="replaceable"><code>lc_ctype</code></em> ]
  9. [ TABLESPACE [=] <em class="replaceable"><code>tablespace_name</code></em> ]
  10. [ ALLOW_CONNECTIONS [=] <em class="replaceable"><code>allowconn</code></em> ]
  11. [ CONNECTION LIMIT [=] <em class="replaceable"><code>connlimit</code></em> ]
  12. [ IS_TEMPLATE [=] <em class="replaceable"><code>istemplate</code></em> ] ]
  13. </pre></div><div class="refsect1" id="id-1.9.3.61.5"><h2>Description</h2><p>
  14. <code class="command">CREATE DATABASE</code> creates a new
  15. <span class="productname">PostgreSQL</span> database.
  16. </p><p>
  17. To create a database, you must be a superuser or have the special
  18. <code class="literal">CREATEDB</code> privilege.
  19. See <a class="xref" href="sql-createrole.html" title="CREATE ROLE"><span class="refentrytitle">CREATE ROLE</span></a>.
  20. </p><p>
  21. By default, the new database will be created by cloning the standard
  22. system database <code class="literal">template1</code>. A different template can be
  23. specified by writing <code class="literal">TEMPLATE
  24. <em class="replaceable"><code>name</code></em></code>. In particular,
  25. by writing <code class="literal">TEMPLATE template0</code>, you can create a virgin
  26. database containing only the standard objects predefined by your
  27. version of <span class="productname">PostgreSQL</span>. This is useful
  28. if you wish to avoid copying
  29. any installation-local objects that might have been added to
  30. <code class="literal">template1</code>.
  31. </p></div><div class="refsect1" id="id-1.9.3.61.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>
  32. The name of a database to create.
  33. </p></dd><dt><span class="term"><em class="replaceable"><code>user_name</code></em></span></dt><dd><p>
  34. The role name of the user who will own the new database,
  35. or <code class="literal">DEFAULT</code> to use the default (namely, the
  36. user executing the command). To create a database owned by another
  37. role, you must be a direct or indirect member of that role,
  38. or be a superuser.
  39. </p></dd><dt><span class="term"><em class="replaceable"><code>template</code></em></span></dt><dd><p>
  40. The name of the template from which to create the new database,
  41. or <code class="literal">DEFAULT</code> to use the default template
  42. (<code class="literal">template1</code>).
  43. </p></dd><dt><span class="term"><em class="replaceable"><code>encoding</code></em></span></dt><dd><p>
  44. Character set encoding to use in the new database. Specify
  45. a string constant (e.g., <code class="literal">'SQL_ASCII'</code>),
  46. or an integer encoding number, or <code class="literal">DEFAULT</code>
  47. to use the default encoding (namely, the encoding of the
  48. template database). The character sets supported by the
  49. <span class="productname">PostgreSQL</span> server are described in
  50. <a class="xref" href="multibyte.html#MULTIBYTE-CHARSET-SUPPORTED" title="23.3.1. Supported Character Sets">Section 23.3.1</a>. See below for
  51. additional restrictions.
  52. </p></dd><dt><span class="term"><em class="replaceable"><code>lc_collate</code></em></span></dt><dd><p>
  53. Collation order (<code class="literal">LC_COLLATE</code>) to use in the new database.
  54. This affects the sort order applied to strings, e.g. in queries with
  55. ORDER BY, as well as the order used in indexes on text columns.
  56. The default is to use the collation order of the template database.
  57. See below for additional restrictions.
  58. </p></dd><dt><span class="term"><em class="replaceable"><code>lc_ctype</code></em></span></dt><dd><p>
  59. Character classification (<code class="literal">LC_CTYPE</code>) to use in the new
  60. database. This affects the categorization of characters, e.g. lower,
  61. upper and digit. The default is to use the character classification of
  62. the template database. See below for additional restrictions.
  63. </p></dd><dt><span class="term"><em class="replaceable"><code>tablespace_name</code></em></span></dt><dd><p>
  64. The name of the tablespace that will be associated with the
  65. new database, or <code class="literal">DEFAULT</code> to use the
  66. template database's tablespace. This
  67. tablespace will be the default tablespace used for objects
  68. created in this database. See
  69. <a class="xref" href="sql-createtablespace.html" title="CREATE TABLESPACE"><span class="refentrytitle">CREATE TABLESPACE</span></a>
  70. for more information.
  71. </p></dd><dt><span class="term"><em class="replaceable"><code>allowconn</code></em></span></dt><dd><p>
  72. If false then no one can connect to this database. The default is
  73. true, allowing connections (except as restricted by other mechanisms,
  74. such as <code class="literal">GRANT</code>/<code class="literal">REVOKE CONNECT</code>).
  75. </p></dd><dt><span class="term"><em class="replaceable"><code>connlimit</code></em></span></dt><dd><p>
  76. How many concurrent connections can be made
  77. to this database. -1 (the default) means no limit.
  78. </p></dd><dt><span class="term"><em class="replaceable"><code>istemplate</code></em></span></dt><dd><p>
  79. If true, then this database can be cloned by any user with <code class="literal">CREATEDB</code>
  80. privileges; if false (the default), then only superusers or the owner
  81. of the database can clone it.
  82. </p></dd></dl></div><p>
  83. Optional parameters can be written in any order, not only the order
  84. illustrated above.
  85. </p></div><div class="refsect1" id="id-1.9.3.61.7"><h2>Notes</h2><p>
  86. <code class="command">CREATE DATABASE</code> cannot be executed inside a transaction
  87. block.
  88. </p><p>
  89. Errors along the line of <span class="quote">“<span class="quote">could not initialize database directory</span>”</span>
  90. are most likely related to insufficient permissions on the data
  91. directory, a full disk, or other file system problems.
  92. </p><p>
  93. Use <a class="xref" href="sql-dropdatabase.html" title="DROP DATABASE"><span class="refentrytitle">DROP DATABASE</span></a> to remove a database.
  94. </p><p>
  95. The program <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> is a
  96. wrapper program around this command, provided for convenience.
  97. </p><p>
  98. 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
  99. <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a>) are not copied from the template database.
  100. </p><p>
  101. Although it is possible to copy a database other than <code class="literal">template1</code>
  102. by specifying its name as the template, this is not (yet) intended as
  103. a general-purpose <span class="quote">“<span class="quote"><code class="command">COPY DATABASE</code></span>”</span> facility.
  104. The principal limitation is that no other sessions can be connected to
  105. the template database while it is being copied. <code class="command">CREATE
  106. DATABASE</code> will fail if any other connection exists when it starts;
  107. otherwise, new connections to the template database are locked out
  108. until <code class="command">CREATE DATABASE</code> completes.
  109. See <a class="xref" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Section 22.3</a> for more information.
  110. </p><p>
  111. The character set encoding specified for the new database must be
  112. compatible with the chosen locale settings (<code class="literal">LC_COLLATE</code> and
  113. <code class="literal">LC_CTYPE</code>). If the locale is <code class="literal">C</code> (or equivalently
  114. <code class="literal">POSIX</code>), then all encodings are allowed, but for other
  115. locale settings there is only one encoding that will work properly.
  116. (On Windows, however, UTF-8 encoding can be used with any locale.)
  117. <code class="command">CREATE DATABASE</code> will allow superusers to specify
  118. <code class="literal">SQL_ASCII</code> encoding regardless of the locale settings,
  119. but this choice is deprecated and may result in misbehavior of
  120. character-string functions if data that is not encoding-compatible
  121. with the locale is stored in the database.
  122. </p><p>
  123. The encoding and locale settings must match those of the template database,
  124. except when <code class="literal">template0</code> is used as template. This is because
  125. other databases might contain data that does not match the specified
  126. encoding, or might contain indexes whose sort ordering is affected by
  127. <code class="literal">LC_COLLATE</code> and <code class="literal">LC_CTYPE</code>. Copying such data would
  128. result in a database that is corrupt according to the new settings.
  129. <code class="literal">template0</code>, however, is known to not contain any data or
  130. indexes that would be affected.
  131. </p><p>
  132. The <code class="literal">CONNECTION LIMIT</code> option is only enforced approximately;
  133. if two new sessions start at about the same time when just one
  134. connection <span class="quote">“<span class="quote">slot</span>”</span> remains for the database, it is possible that
  135. both will fail. Also, the limit is not enforced against superusers or
  136. background worker processes.
  137. </p></div><div class="refsect1" id="id-1.9.3.61.8"><h2>Examples</h2><p>
  138. To create a new database:
  139. </p><pre class="programlisting">
  140. CREATE DATABASE lusiadas;
  141. </pre><p>
  142. </p><p>
  143. To create a database <code class="literal">sales</code> owned by user <code class="literal">salesapp</code>
  144. with a default tablespace of <code class="literal">salesspace</code>:
  145. </p><pre class="programlisting">
  146. CREATE DATABASE sales OWNER salesapp TABLESPACE salesspace;
  147. </pre><p>
  148. </p><p>
  149. To create a database <code class="literal">music</code> with a different locale:
  150. </p><pre class="programlisting">
  151. CREATE DATABASE music
  152. LC_COLLATE 'sv_SE.utf8' LC_CTYPE 'sv_SE.utf8'
  153. TEMPLATE template0;
  154. </pre><p>
  155. In this example, the <code class="literal">TEMPLATE template0</code> clause is required if
  156. the specified locale is different from the one in <code class="literal">template1</code>.
  157. (If it is not, then specifying the locale explicitly is redundant.)
  158. </p><p>
  159. To create a database <code class="literal">music2</code> with a different locale and a
  160. different character set encoding:
  161. </p><pre class="programlisting">
  162. CREATE DATABASE music2
  163. LC_COLLATE 'sv_SE.iso885915' LC_CTYPE 'sv_SE.iso885915'
  164. ENCODING LATIN9
  165. TEMPLATE template0;
  166. </pre><p>
  167. The specified locale and encoding settings must match, or an error will be
  168. reported.
  169. </p><p>
  170. Note that locale names are specific to the operating system, so that the
  171. above commands might not work in the same way everywhere.
  172. </p></div><div class="refsect1" id="id-1.9.3.61.9"><h2>Compatibility</h2><p>
  173. There is no <code class="command">CREATE DATABASE</code> statement in the SQL
  174. standard. Databases are equivalent to catalogs, whose creation is
  175. implementation-defined.
  176. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-createdomain.html">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">Home</a></td><td width="40%" align="right" valign="top"> CREATE DOMAIN</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1