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