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