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
6 <refentry id="app-pg-ctl">
8 <refentrytitle id="app-pg-ctl-title"><application>pg_ctl</application></refentrytitle>
9 <manvolnum>1</manvolnum>
10 <refmiscinfo>Application</refmiscinfo>
14 <refname>pg_ctl</refname>
15 <refpurpose>start, stop, or restart a <productname>PostgreSQL</productname> server</refpurpose>
18 <indexterm zone="app-pg-ctl">
19 <primary>pg_ctl</primary>
25 <command>pg_ctl</command>
26 <arg choice="plain">start</arg>
28 <arg>-t <replaceable>seconds</replaceable></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>
37 <command>pg_ctl</command>
38 <arg choice="plain">stop</arg>
40 <arg>-t <replaceable>seconds</replaceable></arg>
42 <arg>-D <replaceable>datadir</replaceable></arg>
44 <group choice="plain">
47 <arg>i[mmediate]</arg>
52 <command>pg_ctl</command>
53 <arg choice="plain">restart</arg>
55 <arg>-t <replaceable>seconds</replaceable></arg>
57 <arg>-D <replaceable>datadir</replaceable></arg>
60 <group choice="plain">
63 <arg>i[mmediate]</arg>
66 <arg>-o <replaceable>options</replaceable></arg>
69 <command>pg_ctl</command>
70 <arg choice="plain">reload</arg>
72 <arg>-D <replaceable>datadir</replaceable></arg>
75 <command>pg_ctl</command>
76 <arg choice="plain">status</arg>
77 <arg>-D <replaceable>datadir</replaceable></arg>
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>
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>
93 <arg>-t <replaceable>seconds</replaceable></arg>
94 <arg>-o <replaceable>options</replaceable></arg>
97 <command>pg_ctl</command>
98 <arg choice="plain">unregister</arg>
99 <arg>-N <replaceable>servicename</replaceable></arg>
105 <refsect1 id="app-pg-ctl-description">
106 <title>Description</title>
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
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.
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.
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.
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
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
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.
176 <option>register</option> mode allows you to register a system service
177 on <productname>Microsoft Windows</>.
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.
187 <refsect1 id="app-pg-ctl-options">
188 <title>Options</title>
193 <term><option>-c</option></term>
196 Attempt to allow server crashes to produce core files, on platforms
197 where this available, by lifting any soft resource limit placed on
199 This is useful in debugging or diagnosing problems by allowing a
200 stack trace to be obtained from a failed server process.
206 <term><option>-D <replaceable class="parameter">datadir</replaceable></option></term>
209 Specifies the file system location of the database files. If
210 this is omitted, the environment variable
211 <envar>PGDATA</envar> is used.
217 <term><option>-l <replaceable class="parameter">filename</replaceable></option></term>
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.
229 <term><option>-m <replaceable class="parameter">mode</replaceable></option></term>
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
241 <term><option>-o <replaceable class="parameter">options</replaceable></option></term>
244 Specifies options to be passed directly to the
245 <command>postgres</command> command.
248 The options are usually surrounded by single or double
249 quotes to ensure that they are passed through as a group.
255 <term><option>-p <replaceable class="parameter">path</replaceable></option></term>
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.
269 <term><option>-s</option></term>
272 Only print errors, no informational messages.
278 <term><option>-t</option></term>
281 The number of seconds to wait when waiting for start or shutdown
288 <term><option>-w</option></term>
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
309 <term><option>-W</option></term>
312 Do not wait for start or shutdown to complete. This is the
313 default for starts and restarts.
319 <refsect2 id="app-pg-ctl-windows-options">
320 <title>Options for Windows</title>
324 <term><option>-N <replaceable class="parameter">servicename</replaceable></option></term>
327 Name of the system service to register. The name will be used
328 as both the service name and the display name.
334 <term><option>-P <replaceable class="parameter">password</replaceable></option></term>
337 Password for the user to start the service.
343 <term><option>-U <replaceable class="parameter">username</replaceable></option></term>
346 User name for the user to start the service. For domain users, use the
347 format <literal>DOMAIN\username</literal>.
358 <title>Environment</title>
362 <term><envar>PGDATA</envar></term>
366 Default data directory location.
372 <term><envar>PGPORT</envar></term>
376 Default port for <xref linkend="app-psql"> (used by the -w option).
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">).
396 <term><filename>postmaster.pid</filename></term>
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.
408 <term><filename>postmaster.opts.default</filename></term>
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.
422 <term><filename>postmaster.opts</filename></term>
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.
436 <term><filename>postgresql.conf</filename></term>
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.
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">.
464 <refsect1 id="R1-APP-PGCTL-2">
465 <title>Examples</title>
467 <refsect2 id="R2-APP-PGCTL-3">
468 <title>Starting the Server</title>
471 To start up a server:
473 <prompt>$</prompt> <userinput>pg_ctl start</userinput>
478 An example of starting the server, blocking until the server has
481 <prompt>$</prompt> <userinput>pg_ctl -w start</userinput>
486 For a server using port 5433, and
487 running without <function>fsync</function>, use:
489 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" start</userinput>
494 <refsect2 id="R2-APP-PGCTL-4">
495 <title>Stopping the Server</title>
498 <prompt>$</prompt> <userinput>pg_ctl stop</userinput>
500 stops the server. Using the <option>-m</option> switch allows one
501 to control <emphasis>how</emphasis> the backend shuts down.
505 <refsect2 id="R2-APP-PGCTL-5">
506 <title>Restarting the Server</title>
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:
515 <prompt>$</prompt> <userinput>pg_ctl restart</userinput>
521 waiting for it to shut down and to come up:
523 <prompt>$</prompt> <userinput>pg_ctl -w restart</userinput>
528 To restart using port 5433 and disabling <function>fsync</> after restarting:
530 <prompt>$</prompt> <userinput>pg_ctl -o "-F -p 5433" restart</userinput>
535 <refsect2 id="R2-APP-PGCTL-6">
536 <title>Showing the Server Status</title>
539 Here is a sample status output from
540 <application>pg_ctl</application>:
542 <prompt>$</prompt> <userinput>pg_ctl status</userinput>
544 pg_ctl: server is running (pid: 13718)
546 /usr/local/pgsql/bin/postgres '-D' '/usr/local/pgsql/data' '-p' '5433' '-B' '128'
549 This is the command line that would be invoked in restart mode.
556 <title>See Also</title>
559 <xref linkend="app-postgres">