gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

191 linhas
16KB

  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>18.2. Creating a Database Cluster</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="postgres-user.html" title="18.1. The PostgreSQL User Account" /><link rel="next" href="server-start.html" title="18.3. Starting the Database Server" /></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">18.2. Creating a Database Cluster</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="postgres-user.html" title="18.1. The PostgreSQL User Account">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="runtime.html" title="Chapter 18. Server Setup and Operation">Up</a></td><th width="60%" align="center">Chapter 18. Server Setup and Operation</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="server-start.html" title="18.3. Starting the Database Server">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="CREATING-CLUSTER"><div class="titlepage"><div><div><h2 class="title" style="clear: both">18.2. Creating a Database Cluster</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="creating-cluster.html#CREATING-CLUSTER-MOUNT-POINTS">18.2.1. Use of Secondary File Systems</a></span></dt><dt><span class="sect2"><a href="creating-cluster.html#CREATING-CLUSTER-FILESYSTEM">18.2.2. File Systems</a></span></dt></dl></div><a id="id-1.6.5.4.2" class="indexterm"></a><a id="id-1.6.5.4.3" class="indexterm"></a><p>
  3. Before you can do anything, you must initialize a database storage
  4. area on disk. We call this a <em class="firstterm">database cluster</em>.
  5. (The <acronym class="acronym">SQL</acronym> standard uses the term catalog cluster.) A
  6. database cluster is a collection of databases that is managed by a
  7. single instance of a running database server. After initialization, a
  8. database cluster will contain a database named <code class="literal">postgres</code>,
  9. which is meant as a default database for use by utilities, users and third
  10. party applications. The database server itself does not require the
  11. <code class="literal">postgres</code> database to exist, but many external utility
  12. programs assume it exists. Another database created within each cluster
  13. during initialization is called
  14. <code class="literal">template1</code>. As the name suggests, this will be used
  15. as a template for subsequently created databases; it should not be
  16. used for actual work. (See <a class="xref" href="managing-databases.html" title="Chapter 22. Managing Databases">Chapter 22</a> for
  17. information about creating new databases within a cluster.)
  18. </p><p>
  19. In file system terms, a database cluster is a single directory
  20. under which all data will be stored. We call this the <em class="firstterm">data
  21. directory</em> or <em class="firstterm">data area</em>. It is
  22. completely up to you where you choose to store your data. There is no
  23. default, although locations such as
  24. <code class="filename">/usr/local/pgsql/data</code> or
  25. <code class="filename">/var/lib/pgsql/data</code> are popular. To initialize a
  26. database cluster, use the command <a class="xref" href="app-initdb.html" title="initdb"><span class="refentrytitle">initdb</span></a>,<a id="id-1.6.5.4.5.6" class="indexterm"></a> which is
  27. installed with <span class="productname">PostgreSQL</span>. The desired
  28. file system location of your database cluster is indicated by the
  29. <code class="option">-D</code> option, for example:
  30. </p><pre class="screen">
  31. <code class="prompt">$</code> <strong class="userinput"><code>initdb -D /usr/local/pgsql/data</code></strong>
  32. </pre><p>
  33. Note that you must execute this command while logged into the
  34. <span class="productname">PostgreSQL</span> user account, which is
  35. described in the previous section.
  36. </p><div class="tip"><h3 class="title">Tip</h3><p>
  37. As an alternative to the <code class="option">-D</code> option, you can set
  38. the environment variable <code class="envar">PGDATA</code>.
  39. <a id="id-1.6.5.4.6.1.3" class="indexterm"></a>
  40. </p></div><p>
  41. Alternatively, you can run <code class="command">initdb</code> via
  42. the <a class="xref" href="app-pg-ctl.html" title="pg_ctl"><span class="refentrytitle"><span class="application">pg_ctl</span></span></a>
  43. program<a id="id-1.6.5.4.7.3" class="indexterm"></a> like so:
  44. </p><pre class="screen">
  45. <code class="prompt">$</code> <strong class="userinput"><code>pg_ctl -D /usr/local/pgsql/data initdb</code></strong>
  46. </pre><p>
  47. This may be more intuitive if you are
  48. using <code class="command">pg_ctl</code> for starting and stopping the
  49. server (see <a class="xref" href="server-start.html" title="18.3. Starting the Database Server">Section 18.3</a>), so
  50. that <code class="command">pg_ctl</code> would be the sole command you use
  51. for managing the database server instance.
  52. </p><p>
  53. <code class="command">initdb</code> will attempt to create the directory you
  54. specify if it does not already exist. Of course, this will fail if
  55. <code class="command">initdb</code> does not have permissions to write in the
  56. parent directory. It's generally recommendable that the
  57. <span class="productname">PostgreSQL</span> user own not just the data
  58. directory but its parent directory as well, so that this should not
  59. be a problem. If the desired parent directory doesn't exist either,
  60. you will need to create it first, using root privileges if the
  61. grandparent directory isn't writable. So the process might look
  62. like this:
  63. </p><pre class="screen">
  64. root# <strong class="userinput"><code>mkdir /usr/local/pgsql</code></strong>
  65. root# <strong class="userinput"><code>chown postgres /usr/local/pgsql</code></strong>
  66. root# <strong class="userinput"><code>su postgres</code></strong>
  67. postgres$ <strong class="userinput"><code>initdb -D /usr/local/pgsql/data</code></strong>
  68. </pre><p>
  69. </p><p>
  70. <code class="command">initdb</code> will refuse to run if the data directory
  71. exists and already contains files; this is to prevent accidentally
  72. overwriting an existing installation.
  73. </p><p>
  74. Because the data directory contains all the data stored in the
  75. database, it is essential that it be secured from unauthorized
  76. access. <code class="command">initdb</code> therefore revokes access
  77. permissions from everyone but the
  78. <span class="productname">PostgreSQL</span> user, and optionally, group.
  79. Group access, when enabled, is read-only. This allows an unprivileged
  80. user in the same group as the cluster owner to take a backup of the
  81. cluster data or perform other operations that only require read access.
  82. </p><p>
  83. Note that enabling or disabling group access on an existing cluster requires
  84. the cluster to be shut down and the appropriate mode to be set on all
  85. directories and files before restarting
  86. <span class="productname">PostgreSQL</span>. Otherwise, a mix of modes might
  87. exist in the data directory. For clusters that allow access only by the
  88. owner, the appropriate modes are <code class="literal">0700</code> for directories
  89. and <code class="literal">0600</code> for files. For clusters that also allow
  90. reads by the group, the appropriate modes are <code class="literal">0750</code>
  91. for directories and <code class="literal">0640</code> for files.
  92. </p><p>
  93. However, while the directory contents are secure, the default
  94. client authentication setup allows any local user to connect to the
  95. database and even become the database superuser. If you do not
  96. trust other local users, we recommend you use one of
  97. <code class="command">initdb</code>'s <code class="option">-W</code>, <code class="option">--pwprompt</code>
  98. or <code class="option">--pwfile</code> options to assign a password to the
  99. database superuser.<a id="id-1.6.5.4.12.5" class="indexterm"></a>
  100. Also, specify <code class="option">-A md5</code> or
  101. <code class="option">-A password</code> so that the default <code class="literal">trust</code> authentication
  102. mode is not used; or modify the generated <code class="filename">pg_hba.conf</code>
  103. file after running <code class="command">initdb</code>, but
  104. <span class="emphasis"><em>before</em></span> you start the server for the first time. (Other
  105. reasonable approaches include using <code class="literal">peer</code> authentication
  106. or file system permissions to restrict connections. See <a class="xref" href="client-authentication.html" title="Chapter 20. Client Authentication">Chapter 20</a> for more information.)
  107. </p><p>
  108. <code class="command">initdb</code> also initializes the default
  109. locale<a id="id-1.6.5.4.13.2" class="indexterm"></a> for the database cluster.
  110. Normally, it will just take the locale settings in the environment
  111. and apply them to the initialized database. It is possible to
  112. specify a different locale for the database; more information about
  113. that can be found in <a class="xref" href="locale.html" title="23.1. Locale Support">Section 23.1</a>. The default sort order used
  114. within the particular database cluster is set by
  115. <code class="command">initdb</code>, and while you can create new databases using
  116. different sort order, the order used in the template databases that initdb
  117. creates cannot be changed without dropping and recreating them.
  118. There is also a performance impact for using locales
  119. other than <code class="literal">C</code> or <code class="literal">POSIX</code>. Therefore, it is
  120. important to make this choice correctly the first time.
  121. </p><p>
  122. <code class="command">initdb</code> also sets the default character set encoding
  123. for the database cluster. Normally this should be chosen to match the
  124. locale setting. For details see <a class="xref" href="multibyte.html" title="23.3. Character Set Support">Section 23.3</a>.
  125. </p><p>
  126. Non-<code class="literal">C</code> and non-<code class="literal">POSIX</code> locales rely on the
  127. operating system's collation library for character set ordering.
  128. This controls the ordering of keys stored in indexes. For this reason,
  129. a cluster cannot switch to an incompatible collation library version,
  130. either through snapshot restore, binary streaming replication, a
  131. different operating system, or an operating system upgrade.
  132. </p><div class="sect2" id="CREATING-CLUSTER-MOUNT-POINTS"><div class="titlepage"><div><div><h3 class="title">18.2.1. Use of Secondary File Systems</h3></div></div></div><a id="id-1.6.5.4.16.2" class="indexterm"></a><p>
  133. Many installations create their database clusters on file systems
  134. (volumes) other than the machine's <span class="quote">“<span class="quote">root</span>”</span> volume. If you
  135. choose to do this, it is not advisable to try to use the secondary
  136. volume's topmost directory (mount point) as the data directory.
  137. Best practice is to create a directory within the mount-point
  138. directory that is owned by the <span class="productname">PostgreSQL</span>
  139. user, and then create the data directory within that. This avoids
  140. permissions problems, particularly for operations such
  141. as <span class="application">pg_upgrade</span>, and it also ensures clean failures if
  142. the secondary volume is taken offline.
  143. </p></div><div class="sect2" id="CREATING-CLUSTER-FILESYSTEM"><div class="titlepage"><div><div><h3 class="title">18.2.2. File Systems</h3></div></div></div><p>
  144. Generally, any file system with POSIX semantics can be used for
  145. PostgreSQL. Users prefer different file systems for a variety of reasons,
  146. including vendor support, performance, and familiarity. Experience
  147. suggests that, all other things being equal, one should not expect major
  148. performance or behavior changes merely from switching file systems or
  149. making minor file system configuration changes.
  150. </p><div class="sect3" id="CREATING-CLUSTER-NFS"><div class="titlepage"><div><div><h4 class="title">18.2.2.1. NFS</h4></div></div></div><a id="id-1.6.5.4.17.3.2" class="indexterm"></a><p>
  151. It is possible to use an <acronym class="acronym">NFS</acronym> file system for storing
  152. the <span class="productname">PostgreSQL</span> data directory.
  153. <span class="productname">PostgreSQL</span> does nothing special for
  154. <acronym class="acronym">NFS</acronym> file systems, meaning it assumes
  155. <acronym class="acronym">NFS</acronym> behaves exactly like locally-connected drives.
  156. <span class="productname">PostgreSQL</span> does not use any functionality that
  157. is known to have nonstandard behavior on <acronym class="acronym">NFS</acronym>, such as
  158. file locking.
  159. </p><p>
  160. The only firm requirement for using <acronym class="acronym">NFS</acronym> with
  161. <span class="productname">PostgreSQL</span> is that the file system is mounted
  162. using the <code class="literal">hard</code> option. With the
  163. <code class="literal">hard</code> option, processes can <span class="quote">“<span class="quote">hang</span>”</span>
  164. indefinitely if there are network problems, so this configuration will
  165. require a careful monitoring setup. The <code class="literal">soft</code> option
  166. will interrupt system calls in case of network problems, but
  167. <span class="productname">PostgreSQL</span> will not repeat system calls
  168. interrupted in this way, so any such interruption will result in an I/O
  169. error being reported.
  170. </p><p>
  171. It is not necessary to use the <code class="literal">sync</code> mount option. The
  172. behavior of the <code class="literal">async</code> option is sufficient, since
  173. <span class="productname">PostgreSQL</span> issues <code class="literal">fsync</code>
  174. calls at appropriate times to flush the write caches. (This is analogous
  175. to how it works on a local file system.) However, it is strongly
  176. recommended to use the <code class="literal">sync</code> export option on the NFS
  177. <span class="emphasis"><em>server</em></span> on systems where it exists (mainly Linux).
  178. Otherwise, an <code class="literal">fsync</code> or equivalent on the NFS client is
  179. not actually guaranteed to reach permanent storage on the server, which
  180. could cause corruption similar to running with the parameter <a class="xref" href="runtime-config-wal.html#GUC-FSYNC">fsync</a> off. The defaults of these mount and export
  181. options differ between vendors and versions, so it is recommended to
  182. check and perhaps specify them explicitly in any case to avoid any
  183. ambiguity.
  184. </p><p>
  185. In some cases, an external storage product can be accessed either via NFS
  186. or a lower-level protocol such as iSCSI. In the latter case, the storage
  187. appears as a block device and any available file system can be created on
  188. it. That approach might relieve the DBA from having to deal with some of
  189. the idiosyncrasies of NFS, but of course the complexity of managing
  190. remote storage then happens at other levels.
  191. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="postgres-user.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="runtime.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="server-start.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">18.1. The <span class="productname">PostgreSQL</span> User Account </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 18.3. Starting the Database Server</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1