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