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.

252 lines
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.3. Starting the Database Server</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="creating-cluster.html" title="18.2. Creating a Database Cluster" /><link rel="next" href="kernel-resources.html" title="18.4. Managing Kernel Resources" /></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.3. Starting the Database Server</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="creating-cluster.html" title="18.2. Creating a Database Cluster">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="kernel-resources.html" title="18.4. Managing Kernel Resources">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="SERVER-START"><div class="titlepage"><div><div><h2 class="title" style="clear: both">18.3. Starting the Database Server</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="server-start.html#SERVER-START-FAILURES">18.3.1. Server Start-up Failures</a></span></dt><dt><span class="sect2"><a href="server-start.html#CLIENT-CONNECTION-PROBLEMS">18.3.2. Client Connection Problems</a></span></dt></dl></div><p>
  3. Before anyone can access the database, you must start the database
  4. server. The database server program is called
  5. <code class="command">postgres</code>.<a id="id-1.6.5.5.2.2" class="indexterm"></a>
  6. The <code class="command">postgres</code> program must know where to
  7. find the data it is supposed to use. This is done with the
  8. <code class="option">-D</code> option. Thus, the simplest way to start the
  9. server is:
  10. </p><pre class="screen">
  11. $ <strong class="userinput"><code>postgres -D /usr/local/pgsql/data</code></strong>
  12. </pre><p>
  13. which will leave the server running in the foreground. This must be
  14. done while logged into the <span class="productname">PostgreSQL</span> user
  15. account. Without <code class="option">-D</code>, the server will try to use
  16. the data directory named by the environment variable <code class="envar">PGDATA</code>.
  17. If that variable is not provided either, it will fail.
  18. </p><p>
  19. Normally it is better to start <code class="command">postgres</code> in the
  20. background. For this, use the usual Unix shell syntax:
  21. </p><pre class="screen">
  22. $ <strong class="userinput"><code>postgres -D /usr/local/pgsql/data &gt;logfile 2&gt;&amp;1 &amp;</code></strong>
  23. </pre><p>
  24. It is important to store the server's <span class="systemitem">stdout</span> and
  25. <span class="systemitem">stderr</span> output somewhere, as shown above. It will help
  26. for auditing purposes and to diagnose problems. (See <a class="xref" href="logfile-maintenance.html" title="24.3. Log File Maintenance">Section 24.3</a> for a more thorough discussion of log
  27. file handling.)
  28. </p><p>
  29. The <code class="command">postgres</code> program also takes a number of other
  30. command-line options. For more information, see the
  31. <a class="xref" href="app-postgres.html" title="postgres"><span class="refentrytitle"><span class="application">postgres</span></span></a> reference page
  32. and <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a> below.
  33. </p><p>
  34. This shell syntax can get tedious quickly. Therefore the wrapper
  35. program
  36. <a class="xref" href="app-pg-ctl.html" title="pg_ctl"><span class="refentrytitle"><span class="application">pg_ctl</span></span></a><a id="id-1.6.5.5.5.2" class="indexterm"></a>
  37. is provided to simplify some tasks. For example:
  38. </p><pre class="programlisting">
  39. pg_ctl start -l logfile
  40. </pre><p>
  41. will start the server in the background and put the output into the
  42. named log file. The <code class="option">-D</code> option has the same meaning
  43. here as for <code class="command">postgres</code>. <code class="command">pg_ctl</code>
  44. is also capable of stopping the server.
  45. </p><p>
  46. Normally, you will want to start the database server when the
  47. computer boots.<a id="id-1.6.5.5.6.1" class="indexterm"></a>
  48. Autostart scripts are operating-system-specific.
  49. There are a few distributed with
  50. <span class="productname">PostgreSQL</span> in the
  51. <code class="filename">contrib/start-scripts</code> directory. Installing one will require
  52. root privileges.
  53. </p><p>
  54. Different systems have different conventions for starting up daemons
  55. at boot time. Many systems have a file
  56. <code class="filename">/etc/rc.local</code> or
  57. <code class="filename">/etc/rc.d/rc.local</code>. Others use <code class="filename">init.d</code> or
  58. <code class="filename">rc.d</code> directories. Whatever you do, the server must be
  59. run by the <span class="productname">PostgreSQL</span> user account
  60. <span class="emphasis"><em>and not by root</em></span> or any other user. Therefore you
  61. probably should form your commands using
  62. <code class="literal">su postgres -c '...'</code>. For example:
  63. </p><pre class="programlisting">
  64. su postgres -c 'pg_ctl start -D /usr/local/pgsql/data -l serverlog'
  65. </pre><p>
  66. </p><p>
  67. Here are a few more operating-system-specific suggestions. (In each
  68. case be sure to use the proper installation directory and user
  69. name where we show generic values.)
  70. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  71. For <span class="productname">FreeBSD</span>, look at the file
  72. <code class="filename">contrib/start-scripts/freebsd</code> in the
  73. <span class="productname">PostgreSQL</span> source distribution.
  74. <a id="id-1.6.5.5.8.1.1.1.4" class="indexterm"></a>
  75. </p></li><li class="listitem"><p>
  76. On <span class="productname">OpenBSD</span>, add the following lines
  77. to the file <code class="filename">/etc/rc.local</code>:
  78. <a id="id-1.6.5.5.8.1.2.1.3" class="indexterm"></a>
  79. </p><pre class="programlisting">
  80. if [ -x /usr/local/pgsql/bin/pg_ctl -a -x /usr/local/pgsql/bin/postgres ]; then
  81. su -l postgres -c '/usr/local/pgsql/bin/pg_ctl start -s -l /var/postgresql/log -D /usr/local/pgsql/data'
  82. echo -n ' postgresql'
  83. fi
  84. </pre><p>
  85. </p></li><li class="listitem"><p>
  86. On <span class="productname">Linux</span> systems either add
  87. <a id="id-1.6.5.5.8.1.3.1.2" class="indexterm"></a>
  88. </p><pre class="programlisting">
  89. /usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgsql/data
  90. </pre><p>
  91. to <code class="filename">/etc/rc.d/rc.local</code>
  92. or <code class="filename">/etc/rc.local</code> or look at the file
  93. <code class="filename">contrib/start-scripts/linux</code> in the
  94. <span class="productname">PostgreSQL</span> source distribution.
  95. </p><p>
  96. When using <span class="application">systemd</span>, you can use the following
  97. service unit file (e.g.,
  98. at <code class="filename">/etc/systemd/system/postgresql.service</code>):<a id="id-1.6.5.5.8.1.3.2.3" class="indexterm"></a>
  99. </p><pre class="programlisting">
  100. [Unit]
  101. Description=PostgreSQL database server
  102. Documentation=man:postgres(1)
  103. [Service]
  104. Type=notify
  105. User=postgres
  106. ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
  107. ExecReload=/bin/kill -HUP $MAINPID
  108. KillMode=mixed
  109. KillSignal=SIGINT
  110. TimeoutSec=0
  111. [Install]
  112. WantedBy=multi-user.target
  113. </pre><p>
  114. Using <code class="literal">Type=notify</code> requires that the server binary was
  115. built with <code class="literal">configure --with-systemd</code>.
  116. </p><p>
  117. Consider carefully the timeout
  118. setting. <span class="application">systemd</span> has a default timeout of 90
  119. seconds as of this writing and will kill a process that does not notify
  120. readiness within that time. But a <span class="productname">PostgreSQL</span>
  121. server that might have to perform crash recovery at startup could take
  122. much longer to become ready. The suggested value of 0 disables the
  123. timeout logic.
  124. </p></li><li class="listitem"><p>
  125. On <span class="productname">NetBSD</span>, use either the
  126. <span class="productname">FreeBSD</span> or
  127. <span class="productname">Linux</span> start scripts, depending on
  128. preference.
  129. <a id="id-1.6.5.5.8.1.4.1.4" class="indexterm"></a>
  130. </p></li><li class="listitem"><p>
  131. On <span class="productname">Solaris</span>, create a file called
  132. <code class="filename">/etc/init.d/postgresql</code> that contains
  133. the following line:
  134. <a id="id-1.6.5.5.8.1.5.1.3" class="indexterm"></a>
  135. </p><pre class="programlisting">
  136. su - postgres -c "/usr/local/pgsql/bin/pg_ctl start -l logfile -D /usr/local/pgsql/data"
  137. </pre><p>
  138. Then, create a symbolic link to it in <code class="filename">/etc/rc3.d</code> as
  139. <code class="filename">S99postgresql</code>.
  140. </p></li></ul></div><p>
  141. </p><p>
  142. While the server is running, its
  143. <acronym class="acronym">PID</acronym> is stored in the file
  144. <code class="filename">postmaster.pid</code> in the data directory. This is
  145. used to prevent multiple server instances from
  146. running in the same data directory and can also be used for
  147. shutting down the server.
  148. </p><div class="sect2" id="SERVER-START-FAILURES"><div class="titlepage"><div><div><h3 class="title">18.3.1. Server Start-up Failures</h3></div></div></div><p>
  149. There are several common reasons the server might fail to
  150. start. Check the server's log file, or start it by hand (without
  151. redirecting standard output or standard error) and see what error
  152. messages appear. Below we explain some of the most common error
  153. messages in more detail.
  154. </p><p>
  155. </p><pre class="screen">
  156. LOG: could not bind IPv4 address "127.0.0.1": Address already in use
  157. HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
  158. FATAL: could not create any TCP/IP sockets
  159. </pre><p>
  160. This usually means just what it suggests: you tried to start
  161. another server on the same port where one is already running.
  162. However, if the kernel error message is not <code class="computeroutput">Address
  163. already in use</code> or some variant of that, there might
  164. be a different problem. For example, trying to start a server
  165. on a reserved port number might draw something like:
  166. </p><pre class="screen">
  167. $ <strong class="userinput"><code>postgres -p 666</code></strong>
  168. LOG: could not bind IPv4 address "127.0.0.1": Permission denied
  169. HINT: Is another postmaster already running on port 666? If not, wait a few seconds and retry.
  170. FATAL: could not create any TCP/IP sockets
  171. </pre><p>
  172. </p><p>
  173. A message like:
  174. </p><pre class="screen">
  175. FATAL: could not create shared memory segment: Invalid argument
  176. DETAIL: Failed system call was shmget(key=5440001, size=4011376640, 03600).
  177. </pre><p>
  178. probably means your kernel's limit on the size of shared memory is
  179. smaller than the work area <span class="productname">PostgreSQL</span>
  180. is trying to create (4011376640 bytes in this example). Or it could
  181. mean that you do not have System-V-style shared memory support
  182. configured into your kernel at all. As a temporary workaround, you
  183. can try starting the server with a smaller-than-normal number of
  184. buffers (<a class="xref" href="runtime-config-resource.html#GUC-SHARED-BUFFERS">shared_buffers</a>). You will eventually want
  185. to reconfigure your kernel to increase the allowed shared memory
  186. size. You might also see this message when trying to start multiple
  187. servers on the same machine, if their total space requested
  188. exceeds the kernel limit.
  189. </p><p>
  190. An error like:
  191. </p><pre class="screen">
  192. FATAL: could not create semaphores: No space left on device
  193. DETAIL: Failed system call was semget(5440126, 17, 03600).
  194. </pre><p>
  195. does <span class="emphasis"><em>not</em></span> mean you've run out of disk
  196. space. It means your kernel's limit on the number of <span class="systemitem">System V</span> semaphores is smaller than the number
  197. <span class="productname">PostgreSQL</span> wants to create. As above,
  198. you might be able to work around the problem by starting the
  199. server with a reduced number of allowed connections
  200. (<a class="xref" href="runtime-config-connection.html#GUC-MAX-CONNECTIONS">max_connections</a>), but you'll eventually want to
  201. increase the kernel limit.
  202. </p><p>
  203. If you get an <span class="quote">“<span class="quote">illegal system call</span>”</span> error, it is likely that
  204. shared memory or semaphores are not supported in your kernel at
  205. all. In that case your only option is to reconfigure the kernel to
  206. enable these features.
  207. </p><p>
  208. Details about configuring <span class="systemitem">System V</span>
  209. <acronym class="acronym">IPC</acronym> facilities are given in <a class="xref" href="kernel-resources.html#SYSVIPC" title="18.4.1. Shared Memory and Semaphores">Section 18.4.1</a>.
  210. </p></div><div class="sect2" id="CLIENT-CONNECTION-PROBLEMS"><div class="titlepage"><div><div><h3 class="title">18.3.2. Client Connection Problems</h3></div></div></div><p>
  211. Although the error conditions possible on the client side are quite
  212. varied and application-dependent, a few of them might be directly
  213. related to how the server was started. Conditions other than
  214. those shown below should be documented with the respective client
  215. application.
  216. </p><p>
  217. </p><pre class="screen">
  218. psql: could not connect to server: Connection refused
  219. Is the server running on host "server.joe.com" and accepting
  220. TCP/IP connections on port 5432?
  221. </pre><p>
  222. This is the generic <span class="quote">“<span class="quote">I couldn't find a server to talk
  223. to</span>”</span> failure. It looks like the above when TCP/IP
  224. communication is attempted. A common mistake is to forget to
  225. configure the server to allow TCP/IP connections.
  226. </p><p>
  227. Alternatively, you'll get this when attempting Unix-domain socket
  228. communication to a local server:
  229. </p><pre class="screen">
  230. psql: could not connect to server: No such file or directory
  231. Is the server running locally and accepting
  232. connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
  233. </pre><p>
  234. </p><p>
  235. The last line is useful in verifying that the client is trying to
  236. connect to the right place. If there is in fact no server
  237. running there, the kernel error message will typically be either
  238. <code class="computeroutput">Connection refused</code> or
  239. <code class="computeroutput">No such file or directory</code>, as
  240. illustrated. (It is important to realize that
  241. <code class="computeroutput">Connection refused</code> in this context
  242. does <span class="emphasis"><em>not</em></span> mean that the server got your
  243. connection request and rejected it. That case will produce a
  244. different message, as shown in <a class="xref" href="client-authentication-problems.html" title="20.15. Authentication Problems">Section 20.15</a>.) Other error messages
  245. such as <code class="computeroutput">Connection timed out</code> might
  246. indicate more fundamental problems, like lack of network
  247. connectivity.
  248. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="creating-cluster.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="kernel-resources.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">18.2. Creating a Database Cluster </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 18.4. Managing Kernel Resources</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1