gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

87 rindas
8.1KB

  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.3. Template Databases</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-createdb.html" title="22.2. Creating a Database" /><link rel="next" href="manage-ag-config.html" title="22.4. Database Configuration" /></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.3. Template Databases</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="manage-ag-createdb.html" title="22.2. Creating a Database">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-config.html" title="22.4. Database Configuration">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="MANAGE-AG-TEMPLATEDBS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">22.3. Template Databases</h2></div></div></div><p>
  3. <code class="command">CREATE DATABASE</code> actually works by copying an existing
  4. database. By default, it copies the standard system database named
  5. <code class="literal">template1</code>.<a id="id-1.6.9.6.2.3" class="indexterm"></a> Thus that
  6. database is the <span class="quote">“<span class="quote">template</span>”</span> from which new databases are
  7. made. If you add objects to <code class="literal">template1</code>, these objects
  8. will be copied into subsequently created user databases. This
  9. behavior allows site-local modifications to the standard set of
  10. objects in databases. For example, if you install the procedural
  11. language <span class="application">PL/Perl</span> in <code class="literal">template1</code>, it will
  12. automatically be available in user databases without any extra
  13. action being taken when those databases are created.
  14. </p><p>
  15. There is a second standard system database named
  16. <code class="literal">template0</code>.<a id="id-1.6.9.6.3.2" class="indexterm"></a> This
  17. database contains the same data as the initial contents of
  18. <code class="literal">template1</code>, that is, only the standard objects
  19. predefined by your version of
  20. <span class="productname">PostgreSQL</span>. <code class="literal">template0</code>
  21. should never be changed after the database cluster has been
  22. initialized. By instructing
  23. <code class="command">CREATE DATABASE</code> to copy <code class="literal">template0</code> instead
  24. of <code class="literal">template1</code>, you can create a <span class="quote">“<span class="quote">virgin</span>”</span> user
  25. database that contains none of the site-local additions in
  26. <code class="literal">template1</code>. This is particularly handy when restoring a
  27. <code class="literal">pg_dump</code> dump: the dump script should be restored in a
  28. virgin database to ensure that one recreates the correct contents
  29. of the dumped database, without conflicting with objects that
  30. might have been added to <code class="literal">template1</code> later on.
  31. </p><p>
  32. Another common reason for copying <code class="literal">template0</code> instead
  33. of <code class="literal">template1</code> is that new encoding and locale settings
  34. can be specified when copying <code class="literal">template0</code>, whereas a copy
  35. of <code class="literal">template1</code> must use the same settings it does.
  36. This is because <code class="literal">template1</code> might contain encoding-specific
  37. or locale-specific data, while <code class="literal">template0</code> is known not to.
  38. </p><p>
  39. To create a database by copying <code class="literal">template0</code>, use:
  40. </p><pre class="programlisting">
  41. CREATE DATABASE <em class="replaceable"><code>dbname</code></em> TEMPLATE template0;
  42. </pre><p>
  43. from the SQL environment, or:
  44. </p><pre class="programlisting">
  45. createdb -T template0 <em class="replaceable"><code>dbname</code></em>
  46. </pre><p>
  47. from the shell.
  48. </p><p>
  49. It is possible to create additional template databases, and indeed
  50. one can copy any database in a cluster by specifying its name
  51. as the template for <code class="command">CREATE DATABASE</code>. It is important to
  52. understand, however, that this is not (yet) intended as
  53. a general-purpose <span class="quote">“<span class="quote"><code class="command">COPY DATABASE</code></span>”</span> facility.
  54. The principal limitation is that no other sessions can be connected to
  55. the source database while it is being copied. <code class="command">CREATE
  56. DATABASE</code> will fail if any other connection exists when it starts;
  57. during the copy operation, new connections to the source database
  58. are prevented.
  59. </p><p>
  60. Two useful flags exist in <code class="literal">pg_database</code><a id="id-1.6.9.6.7.2" class="indexterm"></a> for each
  61. database: the columns <code class="literal">datistemplate</code> and
  62. <code class="literal">datallowconn</code>. <code class="literal">datistemplate</code>
  63. can be set to indicate that a database is intended as a template for
  64. <code class="command">CREATE DATABASE</code>. If this flag is set, the database can be
  65. cloned by any user with <code class="literal">CREATEDB</code> privileges; if it is not set,
  66. only superusers and the owner of the database can clone it.
  67. If <code class="literal">datallowconn</code> is false, then no new connections
  68. to that database will be allowed (but existing sessions are not terminated
  69. simply by setting the flag false). The <code class="literal">template0</code>
  70. database is normally marked <code class="literal">datallowconn = false</code> to prevent its modification.
  71. Both <code class="literal">template0</code> and <code class="literal">template1</code>
  72. should always be marked with <code class="literal">datistemplate = true</code>.
  73. </p><div class="note"><h3 class="title">Note</h3><p>
  74. <code class="literal">template1</code> and <code class="literal">template0</code> do not have any special
  75. status beyond the fact that the name <code class="literal">template1</code> is the default
  76. source database name for <code class="command">CREATE DATABASE</code>.
  77. For example, one could drop <code class="literal">template1</code> and recreate it from
  78. <code class="literal">template0</code> without any ill effects. This course of action
  79. might be advisable if one has carelessly added a bunch of junk in
  80. <code class="literal">template1</code>. (To delete <code class="literal">template1</code>,
  81. it must have <code class="literal">pg_database.datistemplate = false</code>.)
  82. </p><p>
  83. The <code class="literal">postgres</code> database is also created when a database
  84. cluster is initialized. This database is meant as a default database for
  85. users and applications to connect to. It is simply a copy of
  86. <code class="literal">template1</code> and can be dropped and recreated if necessary.
  87. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="manage-ag-createdb.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-config.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">22.2. Creating a Database </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 22.4. Database Configuration</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1