gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
6.6KB

  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>22.2. Creating a 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="manage-ag-overview.html" title="22.1. Overview" /><link rel="next" href="manage-ag-templatedbs.html" title="22.3. Template Databases" /></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">22.2. Creating a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-overview.html" title="22.1. Overview">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="managing-databases.html" title="Chapter 22. Managing Databases">Up</a></td><th width="60%" align="center">Chapter 22. Managing Databases</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="manage-ag-templatedbs.html" title="22.3. Template Databases">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="MANAGE-AG-CREATEDB"><div class="titlepage"><div><div><h2 class="title" style="clear: both">22.2. Creating a Database</h2></div></div></div><a id="id-1.6.9.5.2" class="indexterm"></a><p>
  3. In order to create a database, the <span class="productname">PostgreSQL</span>
  4. server must be up and running (see <a class="xref" href="server-start.html" title="18.3. Starting the Database Server">Section 18.3</a>).
  5. </p><p>
  6. Databases are created with the SQL command
  7. <a class="xref" href="sql-createdatabase.html" title="CREATE DATABASE"><span class="refentrytitle">CREATE DATABASE</span></a>:
  8. </p><pre class="synopsis">
  9. CREATE DATABASE <em class="replaceable"><code>name</code></em>;
  10. </pre><p>
  11. where <em class="replaceable"><code>name</code></em> follows the usual rules for
  12. <acronym class="acronym">SQL</acronym> identifiers. The current role automatically
  13. becomes the owner of the new database. It is the privilege of the
  14. owner of a database to remove it later (which also removes all
  15. the objects in it, even if they have a different owner).
  16. </p><p>
  17. The creation of databases is a restricted operation. See <a class="xref" href="role-attributes.html" title="21.2. Role Attributes">Section 21.2</a> for how to grant permission.
  18. </p><p>
  19. Since you need to be connected to the database server in order to
  20. execute the <code class="command">CREATE DATABASE</code> command, the
  21. question remains how the <span class="emphasis"><em>first</em></span> database at any given
  22. site can be created. The first database is always created by the
  23. <code class="command">initdb</code> command when the data storage area is
  24. initialized. (See <a class="xref" href="creating-cluster.html" title="18.2. Creating a Database Cluster">Section 18.2</a>.) This
  25. database is called
  26. <code class="literal">postgres</code>.<a id="id-1.6.9.5.6.6" class="indexterm"></a> So to
  27. create the first <span class="quote">“<span class="quote">ordinary</span>”</span> database you can connect to
  28. <code class="literal">postgres</code>.
  29. </p><p>
  30. A second database,
  31. <code class="literal">template1</code>,<a id="id-1.6.9.5.7.2" class="indexterm"></a>
  32. is also created during database cluster initialization. Whenever a
  33. new database is created within the
  34. cluster, <code class="literal">template1</code> is essentially cloned.
  35. This means that any changes you make in <code class="literal">template1</code> are
  36. propagated to all subsequently created databases. Because of this,
  37. avoid creating objects in <code class="literal">template1</code> unless you want them
  38. propagated to every newly created database. More details
  39. appear in <a class="xref" href="manage-ag-templatedbs.html" title="22.3. Template Databases">Section 22.3</a>.
  40. </p><p>
  41. As a convenience, there is a program you can
  42. execute from the shell to create new databases,
  43. <code class="command">createdb</code>.<a id="id-1.6.9.5.8.2" class="indexterm"></a>
  44. </p><pre class="synopsis">
  45. createdb <em class="replaceable"><code>dbname</code></em>
  46. </pre><p>
  47. <code class="command">createdb</code> does no magic. It connects to the <code class="literal">postgres</code>
  48. database and issues the <code class="command">CREATE DATABASE</code> command,
  49. exactly as described above.
  50. The <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> reference page contains the invocation
  51. details. Note that <code class="command">createdb</code> without any arguments will create
  52. a database with the current user name.
  53. </p><div class="note"><h3 class="title">Note</h3><p>
  54. <a class="xref" href="client-authentication.html" title="Chapter 20. Client Authentication">Chapter 20</a> contains information about
  55. how to restrict who can connect to a given database.
  56. </p></div><p>
  57. Sometimes you want to create a database for someone else, and have them
  58. become the owner of the new database, so they can
  59. configure and manage it themselves. To achieve that, use one of the
  60. following commands:
  61. </p><pre class="programlisting">
  62. CREATE DATABASE <em class="replaceable"><code>dbname</code></em> OWNER <em class="replaceable"><code>rolename</code></em>;
  63. </pre><p>
  64. from the SQL environment, or:
  65. </p><pre class="programlisting">
  66. createdb -O <em class="replaceable"><code>rolename</code></em> <em class="replaceable"><code>dbname</code></em>
  67. </pre><p>
  68. from the shell.
  69. Only the superuser is allowed to create a database for
  70. someone else (that is, for a role you are not a member of).
  71. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-overview.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="managing-databases.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="manage-ag-templatedbs.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.1. Overview </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 22.3. Template Databases</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1