]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/pg_ctl-ref.sgml
Document that PGDATA has to point to the configuration files, rather
[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 control 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"><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.
192   </para>
193
194   <para>
195    <option>reload</option> mode simply sends the
196    <command>postgres</command> process a <systemitem>SIGHUP</>
197    signal, causing it to reread its configuration files
198    (<filename>postgresql.conf</filename>,
199    <filename>pg_hba.conf</filename>, etc.).  This allows changing of
200    configuration-file options that do not require a complete restart
201    to take effect.
202   </para>
203
204   <para>
205    <option>status</option> mode checks whether a server is running in
206    the specified data directory. If it is, the <acronym>PID</acronym>
207    and the command line options that were used to invoke it are
208    displayed.  If the server is not running, the process returns an
209    exit status of 3.
210   </para>
211
212   <para>
213    In <option>promote</option> mode, the standby server that is
214    running in the specified data directory is commanded to exit
215    recovery and begin read-write operations.
216   </para>
217
218   <para>
219    <option>kill</option> mode allows you to send a signal to a specified
220     process.  This is particularly valuable for <productname>Microsoft Windows</>
221     which does not have a <application>kill</> command.  Use
222     <literal>--help</> to see a list of supported signal names.
223   </para>
224
225   <para>
226    <option>register</option> mode allows you to register a system service
227    on <productname>Microsoft Windows</>.  The <option>-S</option> option
228    allows selection of service start type, either <quote>auto</quote> (start
229    service automatically on system startup) or <quote>demand</quote> (start
230    service on demand).
231   </para>
232
233   <para>
234    <option>unregister</option> mode allows you to unregister a system service
235    on <productname>Microsoft Windows</>.  This undoes the effects of the
236    <option>register</option> command.
237   </para>
238  </refsect1>
239
240  <refsect1 id="app-pg-ctl-options">
241   <title>Options</title>
242
243     <variablelist>
244
245      <varlistentry>
246       <term><option>-c</option></term>
247       <term><option>--core-file</option></term>
248       <listitem>
249        <para>
250         Attempt to allow server crashes to produce core files, on platforms
251         where this is possible, by lifting any soft resource limit placed on
252         core files.
253         This is useful in debugging or diagnosing problems by allowing a
254         stack trace to be obtained from a failed server process.
255        </para>
256       </listitem>
257      </varlistentry>
258
259      <varlistentry>
260       <term><option>-D <replaceable class="parameter">datadir</replaceable></option></term>
261       <term><option>--pgdata <replaceable class="parameter">datadir</replaceable></option></term>
262       <listitem>
263        <para>
264         Specifies the file system location of the database configuration files.  If
265         this is omitted, the environment variable
266         <envar>PGDATA</envar> is used.
267        </para>
268       </listitem>
269      </varlistentry>
270
271      <varlistentry>
272       <term><option>-l <replaceable class="parameter">filename</replaceable></option></term>
273       <term><option>--log <replaceable class="parameter">filename</replaceable></option></term>
274       <listitem>
275        <para>
276         Append the server log output to
277         <replaceable>filename</replaceable>.  If the file does not
278         exist, it is created.  The <systemitem>umask</> is set to 077,
279         so access to the log file is disallowed to other users by default.
280        </para>
281       </listitem>
282      </varlistentry>
283
284      <varlistentry>
285       <term><option>-m <replaceable class="parameter">mode</replaceable></option></term>
286       <term><option>--mode <replaceable class="parameter">mode</replaceable></option></term>
287       <listitem>
288        <para>
289         Specifies the shutdown mode.  <replaceable>mode</replaceable>
290         can be <literal>smart</literal>, <literal>fast</literal>, or
291         <literal>immediate</literal>, or the first letter of one of
292         these three.  If this is omitted, <literal>smart</literal> is used.
293        </para>
294       </listitem>
295      </varlistentry>
296
297      <varlistentry>
298       <term><option>-o <replaceable class="parameter">options</replaceable></option></term>
299       <listitem>
300        <para>
301         Specifies options to be passed directly to the
302         <command>postgres</command> command.
303        </para>
304        <para>
305         The options should usually be surrounded by single or double
306         quotes to ensure that they are passed through as a group.
307        </para>
308       </listitem>
309      </varlistentry>
310
311      <varlistentry>
312       <term><option>-o <replaceable class="parameter">initdb-options</replaceable></option></term>
313       <listitem>
314        <para>
315         Specifies options to be passed directly to the
316         <command>initdb</command> command.
317        </para>
318        <para>
319         The options should usually be surrounded by single or double
320         quotes to ensure that they are passed through as a group.
321        </para>
322       </listitem>
323      </varlistentry>
324
325      <varlistentry>
326       <term><option>-p <replaceable class="parameter">path</replaceable></option></term>
327       <listitem>
328        <para>
329         Specifies the location of the <filename>postgres</filename>
330         executable.  By default the <filename>postgres</filename> executable is taken from the same
331         directory as <command>pg_ctl</command>, or failing that, the hard-wired
332         installation directory.  It is not necessary to use this
333         option unless you are doing something unusual and get errors
334         that the <filename>postgres</filename> executable was not found.
335        </para>
336
337        <para>
338         In <literal>init</literal> mode, this option analogously
339         specifies the location of the <filename>initdb</filename>
340         executable.
341        </para>
342       </listitem>
343      </varlistentry>
344
345      <varlistentry>
346       <term><option>-s</option></term>
347       <term><option>--silent</option></term>
348       <listitem>
349        <para>
350         Print only errors, no informational messages.
351        </para>
352       </listitem>
353      </varlistentry>
354
355      <varlistentry>
356       <term><option>-t</option></term>
357       <term><option>--timeout</option></term>
358       <listitem>
359        <para>
360         The maximum number of seconds to wait when waiting for startup or
361         shutdown to complete.  The default is 60 seconds.
362        </para>
363       </listitem>
364      </varlistentry>
365
366      <varlistentry>
367       <term><option>-V</></term>
368       <term><option>--version</></term>
369        <listitem>
370         <para>
371          Print the <application>pg_ctl</application> version and exit.
372         </para>
373        </listitem>
374      </varlistentry>
375
376      <varlistentry>
377       <term><option>-w</option></term>
378       <listitem>
379        <para>
380         Wait for the startup or shutdown to complete.
381         Waiting is the default option for shutdowns, but not startups.
382         When waiting for startup, <command>pg_ctl</command> repeatedly
383         attempts to connect to the server.
384         When waiting for shutdown, <command>pg_ctl</command> waits for
385         the server to remove its <acronym>PID</acronym> file.
386         This option allows the entry of an <acronym>SSL</> passphrase on startup.
387         <command>pg_ctl</command> returns an exit code based on the
388         success of the startup or shutdown.
389        </para>
390       </listitem>
391      </varlistentry>
392
393      <varlistentry>
394       <term><option>-W</option></term>
395       <listitem>
396        <para>
397         Do not wait for startup or shutdown to complete.  This is the
398         default for start and restart modes.
399        </para>
400       </listitem>
401      </varlistentry>
402
403      <varlistentry>
404       <term><option>-?</></term>
405       <term><option>--help</></term>
406       <listitem>
407        <para>
408         Show help about <application>pg_ctl</application> command line
409         arguments, and exit.
410        </para>
411       </listitem>
412      </varlistentry>
413    </variablelist>
414
415   <refsect2 id="app-pg-ctl-windows-options">
416    <title>Options for Windows</title>
417
418    <variablelist>
419     <varlistentry>
420      <term><option>-N <replaceable class="parameter">servicename</replaceable></option></term>
421      <listitem>
422       <para>
423        Name of the system service to register. The name will be used
424        as both the service name and the display name.
425       </para>
426      </listitem>
427     </varlistentry>
428
429     <varlistentry>
430      <term><option>-P <replaceable class="parameter">password</replaceable></option></term>
431      <listitem>
432       <para>
433        Password for the user to start the service.
434       </para>
435      </listitem>
436     </varlistentry>
437
438     <varlistentry>
439      <term><option>-S <replaceable class="parameter">start-type</replaceable></option></term>
440      <listitem>
441       <para>
442        Start type of the system service to register.  start-type can
443        be <literal>auto</literal>, or <literal>demand</literal>, or
444        the first letter of one of these two. If this is omitted,
445        <literal>auto</literal> is used.
446       </para>
447      </listitem>
448     </varlistentry>
449
450     <varlistentry>
451      <term><option>-U <replaceable class="parameter">username</replaceable></option></term>
452      <listitem>
453       <para>
454        User name for the user to start the service. For domain users, use the
455        format <literal>DOMAIN\username</literal>.
456       </para>
457      </listitem>
458     </varlistentry>
459    </variablelist>
460   </refsect2>
461
462  </refsect1>
463
464
465  <refsect1>
466   <title>Environment</title>
467
468   <variablelist>
469    <varlistentry>
470     <term><envar>PGDATA</envar></term>
471
472     <listitem>
473      <para>
474       Default data directory location.
475      </para>
476     </listitem>
477    </varlistentry>
478
479   </variablelist>
480
481   <para>
482    <command>pg_ctl</command>, like most other <productname>PostgreSQL</>
483    utilities,
484    also uses the environment variables supported by <application>libpq</>
485    (see <xref linkend="libpq-envars">).
486    For additional server variables, see <xref linkend="app-postgres">.
487   </para>
488  </refsect1>
489
490
491  <refsect1>
492   <title>Files</title>
493
494   <variablelist>
495    <varlistentry>
496     <term><filename>postmaster.pid</filename></term>
497
498     <listitem>
499      <para>
500       The existence of this file in the data directory is used to help
501       <application>pg_ctl</application> determine if the server is
502       currently running.
503      </para>
504     </listitem>
505    </varlistentry>
506
507    <varlistentry>
508     <term><filename>postmaster.opts</filename></term>
509
510     <listitem>
511      <para>If this file exists in the data directory,
512       <application>pg_ctl</application> (in <option>restart</option> mode)
513       will pass the contents of the file as options to
514       <application>postgres</application>, unless overridden
515       by the <option>-o</option> option. The contents of this file
516       are also displayed in <option>status</option> mode.
517      </para>
518     </listitem>
519    </varlistentry>
520
521   </variablelist>
522  </refsect1>
523
524
525  <refsect1 id="R1-APP-PGCTL-2">
526   <title>Examples</title>
527
528   <refsect2 id="R2-APP-PGCTL-3">
529    <title>Starting the Server</title>
530
531    <para>
532     To start the server:
533 <screen>
534 <prompt>$</prompt> <userinput>pg_ctl start</userinput>
535 </screen>
536    </para>
537
538    <para>
539     To start the server, waiting until the server is
540     accepting connections:
541 <screen>
542 <prompt>$</prompt> <userinput>pg_ctl -w start</userinput>
543 </screen>
544    </para>
545
546    <para>
547     To start the server using port 5433, and
548     running without <function>fsync</function>, use:
549 <screen>
550 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" start</userinput>
551 </screen></para>
552   </refsect2>
553
554   <refsect2 id="R2-APP-PGCTL-4">
555    <title>Stopping the Server</title>
556    <para>
557     To stop the server, use:
558 <screen>
559 <prompt>$</prompt> <userinput>pg_ctl stop</userinput>
560 </screen>
561     The <option>-m</option> option allows control over
562     <emphasis>how</emphasis> the server shuts down:
563 <screen>
564 <prompt>$</prompt> <userinput>pg_ctl stop -m fast</userinput>
565 </screen></para>
566   </refsect2>
567
568   <refsect2 id="R2-APP-PGCTL-5">
569    <title>Restarting the Server</title>
570
571    <para>
572     Restarting the server is almost equivalent to stopping the
573     server and starting it again,
574     except that <command>pg_ctl</command> saves and reuses the command line options that
575     were passed to the previously running instance.  To restart
576     the server in the simplest form, use:
577 <screen>
578 <prompt>$</prompt> <userinput>pg_ctl restart</userinput>
579 </screen>
580    </para>
581
582    <para>
583     To restart the server,
584     waiting for it to shut down and restart:
585 <screen>
586 <prompt>$</prompt> <userinput>pg_ctl -w restart</userinput>
587 </screen>
588    </para>
589
590    <para>
591     To restart using port 5433, disabling <function>fsync</> upon restart:
592 <screen>
593 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" restart</userinput>
594 </screen></para>
595   </refsect2>
596
597   <refsect2 id="R2-APP-PGCTL-6">
598    <title>Showing the Server Status</title>
599
600    <para>
601     Here is sample status output from
602     <application>pg_ctl</application>:
603 <screen>
604 <prompt>$</prompt> <userinput>pg_ctl status</userinput>
605 <computeroutput>
606 pg_ctl: server is running (PID: 13718)
607 /usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"
608 </computeroutput>
609 </screen>
610     This is the command line that would be invoked in restart mode.
611    </para>
612   </refsect2>
613  </refsect1>
614
615
616  <refsect1>
617   <title>See Also</title>
618
619   <simplelist type="inline">
620    <member><xref linkend="app-initdb"></member>
621    <member><xref linkend="app-postgres"></member>
622   </simplelist>
623  </refsect1>
624
625 </refentry>