]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/pg_ctl-ref.sgml
Make smart shutdown work in combination with Hot Standby/Streaming Replication.
[postgresql] / doc / src / sgml / ref / pg_ctl-ref.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/pg_ctl-ref.sgml,v 1.50 2010/04/08 01:39:37 rhaas Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="app-pg-ctl">
7  <refmeta>
8   <refentrytitle><application>pg_ctl</application></refentrytitle>
9   <manvolnum>1</manvolnum>
10   <refmiscinfo>Application</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>pg_ctl</refname>
15   <refpurpose>initialize, start, stop, or restart a <productname>PostgreSQL</productname> server</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="app-pg-ctl">
19   <primary>pg_ctl</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23   <cmdsynopsis>
24
25    <command>pg_ctl</command>
26    <arg choice="plain">init[db]</arg>
27    <arg>-s</arg>
28    <arg>-D <replaceable>datadir</replaceable></arg>
29    <arg>-o <replaceable>options</replaceable></arg>
30    <sbr>
31
32    <command>pg_ctl</command>
33    <arg choice="plain">start</arg>
34    <arg>-w</arg>
35    <arg>-t <replaceable>seconds</replaceable></arg>
36    <arg>-s</arg>
37    <arg>-D <replaceable>datadir</replaceable></arg>
38    <arg>-l <replaceable>filename</replaceable></arg>
39    <arg>-o <replaceable>options</replaceable></arg>
40    <arg>-p <replaceable>path</replaceable></arg>
41    <arg>-c</arg>
42    <sbr>
43
44    <command>pg_ctl</command>
45    <arg choice="plain">stop</arg>
46    <arg>-W</arg>
47    <arg>-t <replaceable>seconds</replaceable></arg>
48    <arg>-s</arg>
49    <arg>-D <replaceable>datadir</replaceable></arg>
50    <arg>-m
51      <group choice="plain">
52        <arg>s[mart]</arg>
53        <arg>f[ast]</arg>
54        <arg>i[mmediate]</arg>
55      </group>
56    </arg>
57    <sbr>
58
59    <command>pg_ctl</command>
60    <arg choice="plain">restart</arg>
61    <arg>-w</arg>
62    <arg>-t <replaceable>seconds</replaceable></arg>
63    <arg>-s</arg>
64    <arg>-D <replaceable>datadir</replaceable></arg>
65    <arg>-c</arg>
66    <arg>-m
67      <group choice="plain">
68        <arg>s[mart]</arg>
69        <arg>f[ast]</arg>
70        <arg>i[mmediate]</arg>
71      </group>
72    </arg>
73    <arg>-o <replaceable>options</replaceable></arg>
74    <sbr>
75
76    <command>pg_ctl</command>
77    <arg choice="plain">reload</arg>
78    <arg>-s</arg>
79    <arg>-D <replaceable>datadir</replaceable></arg>
80    <sbr>
81
82    <command>pg_ctl</command>
83    <arg choice="plain">status</arg>
84    <arg>-D <replaceable>datadir</replaceable></arg>
85    <sbr>
86
87    <command>pg_ctl</command>
88    <arg choice="plain">kill</arg>
89    <arg><replaceable>signal_name</replaceable></arg>
90    <arg><replaceable>process_id</replaceable></arg>
91    <sbr>
92
93    <command>pg_ctl</command>
94    <arg choice="plain">register</arg>
95    <arg>-N <replaceable>servicename</replaceable></arg>
96    <arg>-U <replaceable>username</replaceable></arg>
97    <arg>-P <replaceable>password</replaceable></arg>
98    <arg>-D <replaceable>datadir</replaceable></arg>
99    <arg>-w</arg>
100    <arg>-t <replaceable>seconds</replaceable></arg>
101    <arg>-o <replaceable>options</replaceable></arg>
102    <sbr>
103
104    <command>pg_ctl</command>
105    <arg choice="plain">unregister</arg>
106    <arg>-N <replaceable>servicename</replaceable></arg>
107
108   </cmdsynopsis>
109  </refsynopsisdiv>
110
111
112  <refsect1 id="app-pg-ctl-description">
113   <title>Description</title>
114   <para>
115    <application>pg_ctl</application> is a utility for initializing a
116    <productname>PostgreSQL</productname> database cluster, starting,
117    stopping, or restarting the <productname>PostgreSQL</productname>
118    backend server (<xref linkend="app-postgres">), or displaying the
119    status of a running server.  Although the server can be started
120    manually, <application>pg_ctl</application> encapsulates tasks such
121    as redirecting log output and properly detaching from the terminal
122    and process group. It also provides convenient options for
123    controlled shutdown.
124   </para>
125
126   <para>
127    The <option>init</option> or <option>initdb</option> mode creates a
128    new
129    <productname>PostgreSQL</productname> database cluster.  A database
130    cluster is a collection of databases that are managed by a single
131    server instance.  This mode invokes the <command>initdb</command>
132    command.  See <xref linkend="app-initdb"> for details.
133   </para>
134
135   <para>
136    In <option>start</option> mode, a new server is launched.  The
137    server is started in the background, and standard input is attached to
138    <filename>/dev/null</filename>.  The standard output and standard
139    error are either appended to a log file (if the <option>-l</option>
140    option is used), or redirected to <application>pg_ctl</application>'s 
141    standard output (not standard error).  If no log file is chosen, the 
142    standard output of <application>pg_ctl</application> should be redirected 
143    to a file or piped to another process such as a log rotating program
144    like <application>rotatelogs</>; otherwise <command>postgres</command> 
145    will write its output to the controlling terminal (from the background) 
146    and will not leave the shell's process group.
147   </para>
148
149   <para>
150    In <option>stop</option> mode, the server that is running in
151    the specified data directory is shut down.  Three different
152    shutdown methods can be selected with the <option>-m</option>
153    option: <quote>Smart</quote> mode waits for online backup mode
154    to finish and all the clients to disconnect.  This is the default.
155    If the server is in recovery, recovery and streaming replication
156    will be terminated once all clients have disconnected.
157    <quote>Fast</quote> mode does not wait for clients to disconnect and
158    will terminate an online backup in progress.  All active transactions are
159    rolled back and clients are forcibly disconnected, then the
160    server is shut down.  <quote>Immediate</quote> mode will abort
161    all server processes without a clean shutdown.  This will lead to 
162    a recovery run on restart.
163   </para>
164
165   <para>
166    <option>restart</option> mode effectively executes a stop followed
167    by a start.  This allows changing the <command>postgres</command>
168    command-line options.
169   </para>
170
171   <para>
172    <option>reload</option> mode simply sends the
173    <command>postgres</command> process a <systemitem>SIGHUP</>
174    signal, causing it to reread its configuration files
175    (<filename>postgresql.conf</filename>,
176    <filename>pg_hba.conf</filename>, etc.).  This allows changing of
177    configuration-file options that do not require a complete restart
178    to take effect.
179   </para>
180
181   <para>
182    <option>status</option> mode checks whether a server is running in
183    the specified data directory. If it is, the <acronym>PID</acronym>
184    and the command line options that were used to invoke it are
185    displayed.
186   </para>
187
188   <para>
189    <option>kill</option> mode allows you to send a signal to a specified
190     process.  This is particularly valuable for <productname>Microsoft Windows</>
191     which does not have a <application>kill</> command.  Use 
192     <literal>--help</> to see a list of supported signal names.
193   </para>
194
195   <para>
196    <option>register</option> mode allows you to register a system service
197    on <productname>Microsoft Windows</>.
198   </para>
199
200   <para>
201    <option>unregister</option> mode allows you to unregister a system service
202    on <productname>Microsoft Windows</>, previously registered with the
203    <option>register</option> command.
204   </para>
205  </refsect1>
206
207  <refsect1 id="app-pg-ctl-options">
208   <title>Options</title>
209
210     <variablelist>
211
212      <varlistentry>
213       <term><option>-c</option></term>
214       <listitem>
215        <para>
216         Attempt to allow server crashes to produce core files, on platforms
217         where this available, by lifting any soft resource limit placed on 
218         them. 
219         This is useful in debugging or diagnosing problems by allowing a 
220         stack trace to be obtained from a failed server process.
221        </para>
222       </listitem>
223      </varlistentry>
224
225      <varlistentry>
226       <term><option>-D <replaceable class="parameter">datadir</replaceable></option></term>
227       <listitem>
228        <para>
229         Specifies the file system location of the database files.  If
230         this is omitted, the environment variable
231         <envar>PGDATA</envar> is used.
232        </para>
233       </listitem>
234      </varlistentry>
235
236      <varlistentry>
237       <term><option>-l <replaceable class="parameter">filename</replaceable></option></term>
238       <listitem>
239        <para>
240         Append the server log output to
241         <replaceable>filename</replaceable>.  If the file does not
242         exist, it is created.  The <systemitem>umask</> is set to 077, so access to
243         the log file from other users is disallowed by default.
244        </para>
245       </listitem>
246      </varlistentry>
247
248      <varlistentry>
249       <term><option>-m <replaceable class="parameter">mode</replaceable></option></term>
250       <listitem>
251        <para>
252         Specifies the shutdown mode.  <replaceable>mode</replaceable>
253         can be <literal>smart</literal>, <literal>fast</literal>, or
254         <literal>immediate</literal>, or the first letter of one of
255         these three.
256        </para>
257       </listitem>
258      </varlistentry>
259
260      <varlistentry>
261       <term><option>-o <replaceable class="parameter">options</replaceable></option></term>
262       <listitem>
263        <para>
264         Specifies options to be passed directly to the
265         <command>postgres</command> command.
266        </para>
267        <para>
268         The options are usually surrounded by single or double
269         quotes to ensure that they are passed through as a group.
270        </para>
271       </listitem>
272      </varlistentry>
273
274      <varlistentry>
275       <term><option>-p <replaceable class="parameter">path</replaceable></option></term>
276       <listitem>
277        <para>
278         Specifies the location of the <filename>postgres</filename>
279         executable.  By default the <filename>postgres</filename> executable is taken from the same
280         directory as <command>pg_ctl</command>, or failing that, the hard-wired
281         installation directory.  It is not necessary to use this
282         option unless you are doing something unusual and get errors
283         that the <filename>postgres</filename> executable was not found.
284        </para>
285
286        <para>
287         In <literal>init</literal> mode, this option analogously
288         specifies the location of the <filename>initdb</filename>
289         executable.
290        </para>
291       </listitem>
292      </varlistentry>
293
294      <varlistentry>
295       <term><option>-s</option></term>
296       <listitem>
297        <para>
298         Only print errors, no informational messages.
299        </para>
300       </listitem>
301      </varlistentry>
302
303      <varlistentry>
304       <term><option>-t</option></term>
305       <listitem>
306        <para>
307         The number of seconds to wait when waiting for start or shutdown
308         to complete.
309        </para>
310       </listitem>
311      </varlistentry>
312
313      <varlistentry>
314       <term><option>-w</option></term>
315       <listitem>
316        <para>
317         Wait for the start or shutdown to complete.  The default wait time
318         is 60 seconds.  This is the default option for shutdowns. A successful 
319         shutdown is indicated by removal of the <acronym>PID</acronym> 
320         file. For starting up, a successful <command>psql -l</command> 
321         indicates success. <command>pg_ctl</command> will attempt to 
322         use the proper port for <application>psql</>. If the environment variable 
323         <envar>PGPORT</envar> exists, that is used. Otherwise, it will see if a port 
324         has been set in the <filename>postgresql.conf</filename> file. 
325         If neither of those is used, it will use the default port that 
326         <productname>PostgreSQL</productname> was compiled with 
327         (5432 by default). When waiting, <command>pg_ctl</command> will
328         return an accurate exit code based on the success of the startup 
329         or shutdown.
330        </para>
331       </listitem>
332      </varlistentry>
333
334      <varlistentry>
335       <term><option>-W</option></term>
336       <listitem>
337        <para>
338         Do not wait for start or shutdown to complete.  This is the
339         default for starts and restarts.
340        </para>
341       </listitem>
342      </varlistentry>
343    </variablelist>
344
345   <refsect2 id="app-pg-ctl-windows-options">
346    <title>Options for Windows</title>
347
348    <variablelist>
349     <varlistentry>
350      <term><option>-N <replaceable class="parameter">servicename</replaceable></option></term>
351      <listitem>
352       <para>
353        Name of the system service to register. The name will be used
354        as both the service name and the display name.
355       </para>
356      </listitem>
357     </varlistentry>
358
359     <varlistentry>
360      <term><option>-P <replaceable class="parameter">password</replaceable></option></term>
361      <listitem>
362       <para>
363        Password for the user to start the service.
364       </para>
365      </listitem>
366     </varlistentry>
367
368     <varlistentry>
369      <term><option>-U <replaceable class="parameter">username</replaceable></option></term>
370      <listitem>
371       <para>
372        User name for the user to start the service. For domain users, use the
373        format <literal>DOMAIN\username</literal>.
374       </para>
375      </listitem>
376     </varlistentry>
377    </variablelist>
378   </refsect2>
379
380  </refsect1>
381
382
383  <refsect1>
384   <title>Environment</title>
385
386   <variablelist>
387    <varlistentry>
388     <term><envar>PGDATA</envar></term>
389
390     <listitem>
391      <para>
392       Default data directory location.
393      </para>
394     </listitem>
395    </varlistentry>
396
397    <varlistentry>
398     <term><envar>PGHOST</envar></term>
399
400     <listitem>
401      <para>
402       Default hostname or Unix-domain socket location for <xref
403       linkend="app-psql"> (used by the -w option).
404      </para>
405     </listitem>
406    </varlistentry>
407
408    <varlistentry>
409     <term><envar>PGPORT</envar></term>
410
411     <listitem>
412      <para>
413       Default port number for <xref linkend="app-psql"> (used by the -w option).
414      </para>
415     </listitem>
416    </varlistentry>
417
418   </variablelist>
419
420   <para>
421    For additional server variables, see <xref linkend="app-postgres">.
422    This utility, like most other <productname>PostgreSQL</> utilities,
423    also uses the environment variables supported by <application>libpq</>
424    (see <xref linkend="libpq-envars">).
425   </para>
426  </refsect1>
427
428
429  <refsect1>
430   <title>Files</title>
431
432   <variablelist>
433    <varlistentry>
434     <term><filename>postmaster.pid</filename></term>
435
436     <listitem>
437      <para>
438       The existence of this file in the data directory is used to help
439       <application>pg_ctl</application> determine if the server is
440       currently running or not.
441      </para>
442     </listitem>
443    </varlistentry>
444
445    <varlistentry>
446     <term><filename>postmaster.opts</filename></term>
447
448     <listitem>
449      <para>If this file exists in the data directory,
450       <application>pg_ctl</application> (in <option>restart</option> mode) 
451       will pass the contents of the file as options to
452       <application>postgres</application>, unless overridden 
453       by the <option>-o</option> option. The contents of this file 
454       are also displayed in <option>status</option> mode.
455      </para>
456     </listitem>
457    </varlistentry>
458
459    <varlistentry>
460     <term><filename>postgresql.conf</filename></term>
461
462     <listitem>
463      <para>
464       This file, located in the data directory, is parsed to find the
465       proper port to use with <application>psql</application> when the
466       <option>-w</option> is given in <option>start</option> mode.
467      </para>
468     </listitem>
469    </varlistentry>
470
471   </variablelist>
472  </refsect1>
473
474
475  <refsect1>
476   <title>Notes</title>
477
478   <para>
479    Waiting for complete start is not a well-defined operation and might
480    fail if access control is set up so that a local client cannot
481    connect without manual interaction (e.g., password authentication).  For
482    additional connection variables, see <xref linkend="libpq-envars">,
483    and for passwords, also see <xref linkend="libpq-pgpass">.
484   </para>
485  </refsect1>
486
487
488  <refsect1 id="R1-APP-PGCTL-2">
489   <title>Examples</title>
490
491   <refsect2 id="R2-APP-PGCTL-3">
492    <title>Starting the Server</title>
493
494    <para>
495     To start up a server:
496 <screen>
497 <prompt>$</prompt> <userinput>pg_ctl start</userinput>
498 </screen>
499    </para>
500
501    <para>
502     An example of starting the server, blocking until the server has
503     come up is:
504 <screen>
505 <prompt>$</prompt> <userinput>pg_ctl -w start</userinput>
506 </screen>
507    </para>
508
509    <para>
510     For a server using port 5433, and
511     running without <function>fsync</function>, use:
512 <screen>
513 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" start</userinput>
514 </screen>
515    </para>
516   </refsect2>
517
518   <refsect2 id="R2-APP-PGCTL-4">
519    <title>Stopping the Server</title>
520    <para>
521 <screen>
522 <prompt>$</prompt> <userinput>pg_ctl stop</userinput>
523 </screen>
524     stops the server. Using the <option>-m</option> switch allows one
525     to control <emphasis>how</emphasis> the backend shuts down.
526    </para>
527   </refsect2>
528
529   <refsect2 id="R2-APP-PGCTL-5">
530    <title>Restarting the Server</title>
531
532    <para>
533     Restarting the server is almost equivalent to stopping the
534     server and starting it again
535     except that <command>pg_ctl</command> saves and reuses the command line options that
536     were passed to the previously running instance.  To restart
537     the server in the simplest form, use:
538 <screen>
539 <prompt>$</prompt> <userinput>pg_ctl restart</userinput>
540 </screen>
541    </para>
542
543    <para>
544     To restart server,
545     waiting for it to shut down and to come up:
546 <screen>
547 <prompt>$</prompt> <userinput>pg_ctl -w restart</userinput>
548 </screen>
549    </para>
550
551    <para>
552     To restart using port 5433 and disabling <function>fsync</> after restarting:
553 <screen>
554 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" restart</userinput>
555 </screen>
556    </para>
557   </refsect2>
558
559   <refsect2 id="R2-APP-PGCTL-6">
560    <title>Showing the Server Status</title>
561
562    <para>
563     Here is a sample status output from
564     <application>pg_ctl</application>:
565 <screen>
566 <prompt>$</prompt> <userinput>pg_ctl status</userinput>
567 <computeroutput>
568 pg_ctl: server is running (pid: 13718)
569 Command line was:
570 /usr/local/pgsql/bin/postgres '-D' '/usr/local/pgsql/data' '-p' '5433' '-B' '128'
571 </computeroutput>
572 </screen>
573     This is the command line that would be invoked in restart mode.
574    </para>
575   </refsect2>
576  </refsect1>
577
578
579  <refsect1>
580   <title>See Also</title>
581
582   <simplelist type="inline">
583    <member><xref linkend="app-initdb"></member>
584    <member><xref linkend="app-postgres"></member>
585   </simplelist>
586  </refsect1>
587
588 </refentry>