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