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.

119 lines
9.0KB

  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>1.3. 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="tutorial-arch.html" title="1.2. Architectural Fundamentals" /><link rel="next" href="tutorial-accessdb.html" title="1.4. Accessing a Database" /></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">1.3. Creating a Database</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-arch.html" title="1.2. Architectural Fundamentals">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-start.html" title="Chapter 1. Getting Started">Up</a></td><th width="60%" align="center">Chapter 1. Getting Started</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="tutorial-accessdb.html" title="1.4. Accessing a Database">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-CREATEDB"><div class="titlepage"><div><div><h2 class="title" style="clear: both">1.3. Creating a Database</h2></div></div></div><a id="id-1.4.3.4.2" class="indexterm"></a><a id="id-1.4.3.4.3" class="indexterm"></a><p>
  3. The first test to see whether you can access the database server
  4. is to try to create a database. A running
  5. <span class="productname">PostgreSQL</span> server can manage many
  6. databases. Typically, a separate database is used for each
  7. project or for each user.
  8. </p><p>
  9. Possibly, your site administrator has already created a database
  10. for your use. In that case you can omit this step and skip ahead
  11. to the next section.
  12. </p><p>
  13. To create a new database, in this example named
  14. <code class="literal">mydb</code>, you use the following command:
  15. </p><pre class="screen">
  16. <code class="prompt">$</code> <strong class="userinput"><code>createdb mydb</code></strong>
  17. </pre><p>
  18. If this produces no response then this step was successful and you can skip over the
  19. remainder of this section.
  20. </p><p>
  21. If you see a message similar to:
  22. </p><pre class="screen">
  23. createdb: command not found
  24. </pre><p>
  25. then <span class="productname">PostgreSQL</span> was not installed properly. Either it was not
  26. installed at all or your shell's search path was not set to include it.
  27. Try calling the command with an absolute path instead:
  28. </p><pre class="screen">
  29. <code class="prompt">$</code> <strong class="userinput"><code>/usr/local/pgsql/bin/createdb mydb</code></strong>
  30. </pre><p>
  31. The path at your site might be different. Contact your site
  32. administrator or check the installation instructions to
  33. correct the situation.
  34. </p><p>
  35. Another response could be this:
  36. </p><pre class="screen">
  37. createdb: could not connect to database postgres: could not connect to server: No such file or directory
  38. Is the server running locally and accepting
  39. connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
  40. </pre><p>
  41. This means that the server was not started, or it was not started
  42. where <code class="command">createdb</code> expected it. Again, check the
  43. installation instructions or consult the administrator.
  44. </p><p>
  45. Another response could be this:
  46. </p><pre class="screen">
  47. createdb: could not connect to database postgres: FATAL: role "joe" does not exist
  48. </pre><p>
  49. where your own login name is mentioned. This will happen if the
  50. administrator has not created a <span class="productname">PostgreSQL</span> user account
  51. for you. (<span class="productname">PostgreSQL</span> user accounts are distinct from
  52. operating system user accounts.) If you are the administrator, see
  53. <a class="xref" href="user-manag.html" title="Chapter 21. Database Roles">Chapter 21</a> for help creating accounts. You will need to
  54. become the operating system user under which <span class="productname">PostgreSQL</span>
  55. was installed (usually <code class="literal">postgres</code>) to create the first user
  56. account. It could also be that you were assigned a
  57. <span class="productname">PostgreSQL</span> user name that is different from your
  58. operating system user name; in that case you need to use the <code class="option">-U</code>
  59. switch or set the <code class="envar">PGUSER</code> environment variable to specify your
  60. <span class="productname">PostgreSQL</span> user name.
  61. </p><p>
  62. If you have a user account but it does not have the privileges required to
  63. create a database, you will see the following:
  64. </p><pre class="screen">
  65. createdb: database creation failed: ERROR: permission denied to create database
  66. </pre><p>
  67. Not every user has authorization to create new databases. If
  68. <span class="productname">PostgreSQL</span> refuses to create databases
  69. for you then the site administrator needs to grant you permission
  70. to create databases. Consult your site administrator if this
  71. occurs. If you installed <span class="productname">PostgreSQL</span>
  72. yourself then you should log in for the purposes of this tutorial
  73. under the user account that you started the server as.
  74. <a href="#ftn.id-1.4.3.4.10.4" class="footnote"><sup class="footnote" id="id-1.4.3.4.10.4">[1]</sup></a>
  75. </p><p>
  76. You can also create databases with other names.
  77. <span class="productname">PostgreSQL</span> allows you to create any
  78. number of databases at a given site. Database names must have an
  79. alphabetic first character and are limited to 63 bytes in
  80. length. A convenient choice is to create a database with the same
  81. name as your current user name. Many tools assume that database
  82. name as the default, so it can save you some typing. To create
  83. that database, simply type:
  84. </p><pre class="screen">
  85. <code class="prompt">$</code> <strong class="userinput"><code>createdb</code></strong>
  86. </pre><p>
  87. </p><p>
  88. If you do not want to use your database anymore you can remove it.
  89. For example, if you are the owner (creator) of the database
  90. <code class="literal">mydb</code>, you can destroy it using the following
  91. command:
  92. </p><pre class="screen">
  93. <code class="prompt">$</code> <strong class="userinput"><code>dropdb mydb</code></strong>
  94. </pre><p>
  95. (For this command, the database name does not default to the user
  96. account name. You always need to specify it.) This action
  97. physically removes all files associated with the database and
  98. cannot be undone, so this should only be done with a great deal of
  99. forethought.
  100. </p><p>
  101. More about <code class="command">createdb</code> and <code class="command">dropdb</code> can
  102. be found in <a class="xref" href="app-createdb.html" title="createdb"><span class="refentrytitle"><span class="application">createdb</span></span></a> and <a class="xref" href="app-dropdb.html" title="dropdb"><span class="refentrytitle"><span class="application">dropdb</span></span></a>
  103. respectively.
  104. </p><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.4.3.4.10.4" class="footnote"><p><a href="#id-1.4.3.4.10.4" class="para"><sup class="para">[1] </sup></a>
  105. As an explanation for why this works:
  106. <span class="productname">PostgreSQL</span> user names are separate
  107. from operating system user accounts. When you connect to a
  108. database, you can choose what
  109. <span class="productname">PostgreSQL</span> user name to connect as;
  110. if you don't, it will default to the same name as your current
  111. operating system account. As it happens, there will always be a
  112. <span class="productname">PostgreSQL</span> user account that has the
  113. same name as the operating system user that started the server,
  114. and it also happens that that user always has permission to
  115. create databases. Instead of logging in as that user you can
  116. also specify the <code class="option">-U</code> option everywhere to select
  117. a <span class="productname">PostgreSQL</span> user name to connect as.
  118. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-arch.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-start.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-accessdb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">1.2. Architectural Fundamentals </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 1.4. Accessing a Database</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1