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