2.4. Starting and Stopping PostgreSQL

In this section we cover two options provided with PostgreSQL that are used to start and stop PostgreSQL. The first is a general purpose application called pg_ctl, which should function identically on any machine, regardless of the system. This script is intended to be run by the system user (e.g., the user who owns the data directory) configured to execute the postmaster back end.

The second script provided is the SysV-style script, found in the contrib/start-scripts subdirectory within the PostgreSQL source path. The installation of the SysV script is discussed in Chapter 2. By default this script is named linux, as it is intended for a Linux system's start-script directory, though in the installation instructions it is renamed to a script called postgresql in the system's service start-up directory (e.g.,/etc/init.d ).

The functional difference between pg_ctl and the SysV-style service script is that pg_ctl is intended to be used by the user who runs the postmaster backend (e.g., postgres), whereas the service script is intended to be run by the root user.

The service script is not strictly Linux-specific, and should be compatible with most systems based on SysV start-up scripts. However, if you are not running Linux, you may prefer to stick with the pg_ctl script.

2.4.1. Using pg_ctl

The pg_ctl script is provided with PostgreSQL as a general control application. With it, you can start, stop, restart, or check on the status of PostgreSQL.

Here is the syntax for pg_ctl, from the -  -help option:

	pg_ctl start   [-w] [-D DATADIR] [-s] [-l FILENAME] [-o "OPTIONS"]
	pg_ctl stop    [-W] [-D DATADIR] [-s] [-m SHUTDOWN-MODE]
	pg_ctl restart [-w] [-D DATADIR] [-s] [-m SHUTDOWN-MODE] [-o "OPTIONS"]
	pg_ctl reload  [-D DATADIR] [-s]
	pg_ctl status  [-D DATADIR]
	pg_ctl kill    SIGNALNAME PID

The following actions may be passed to pg_ctl:

start

Instructs the pg_ctl application to attempt to start the database.

stop

Instructs the pg_ctl application to attempt to stop the database.

restart

Instructs the pg_ctl application to attempt to restart the database. In essence this is the same as stopping and then starting the database but in once action instead of two.

reload

Instructs the pg_ctl application to attempt to reload the database. This is a handy action that allows the superuser of PostgreSQL to reinitialize the configuration files of PostgreSQL. For parameters that will reload using this option see the Configured At column in the Section 3.1.15 . The keyword Reload will tell you which ones will reinitialized on the use of this option.

status

Instructs the pg_ctl application to inform the postgresql owner the status of the postmaster. Generally, it will inform the owner whether or not the postmaster is running and at what PID.

kill

Instructs the pg_ctl application to send a specific signal to the postmaster backend. Common options are TERM, QUIT, HUP. This is just a shortcut to using the kill command in Linux and to deal within specific limitations in the Win32 version.

The following options may be passed to pg_ctl:

-w

Causes the pg_ctl application to wait until the operation has finished before returning to a command line. This option may be passed to either the start or restart action; by default, the application sends the command on to the postmaster and exits immediately for these actions.

-W

Causes the pg_ctl application not to wait until the operation has finished before returning to a command line. This option may only be passed to the stop action; by default, the application sends the stop command on to the postmaster, and waits for the action to finish before exiting.

-D DATADIR

Specifies the directory that contains the default database files. This is optional, because you may have this value already set in the PGDATA environment variable. If the PGDATA environment variable is not set, the -D flag is required.

-s

Suppresses any output from the pg_ctl application, aside from system errors. If this flag is not specified, information about the activity within the database (or specific information about startup or shutdown, depending on the action) will be displayed to the screen of the user who initiated the command.

-l FILENAME

Specifies a file FILENAME to append database activity to. This option is only available with the start action.

-m SHUTDOWN-MODE

Sets the SHUTDOWN-MODE with which to shut down the postmaster backend.

smart

Makes postmaster wait for all clients to disconnect before shutting down.

fast

Shuts postmaster down without waiting for clients to disconnect.

immediate

Shuts postmaster down more abruptly (immediately) than fast mode, bypassing normal shutdown procedures. This mode causes the database to restart in recovery mode the next time it starts, which verifies the integrity of the system. This mode should not be used unless absolutely neccessary.

Note

The -m SHUTDOWN-MODE is available to the stop and restart actions.

-o "OPTIONS"

Passes the options specified by OPTIONS (within double quotes) to be passed directly through to the postmaster (e.g., the -i flag to enable TCP/IP connections). See Section 2.4.3" later in this chapter for a complete list of these flags.

SIGNALNAME

The signal name to pass to pg_ctl when using the killaction. For more information on signals refer to the signal(7) manpage in Linux.

PID

The PID of the postmaster that you wish to send a signal to when using the kill action.

Note

Many of the run-time configuration options for postmaster can be found in the postgresql.conf file, which is stored in the PostgreSQL data path (e.g., /usr/local/pgsql/data). The options in this file are of a more technical nature, and should not be modified unless you are sure you understand their purpose.

2.4.1.1. Starting PostgreSQL with pg_ctl

To start PostgreSQL's postmaster backend, the start argument must be passed to pg_ctl. Remember that pg_ctl must be run by the postgres user (or whatever user you have configured to own the PostgreSQL data path).

Example 2-12 starts the postmaster backend, using the data path of /usr/local/pgsql/data. The database system starts up successfully, reports the last time the database system was shut down, and provides various debugging statements before returning the postgres user to a shell prompt.

Example 2-12. Starting PostgreSQL with pg_ctl

[postgres@booktown ~]$ pg_ctl -D /usr/local/pgsql/data start
LOG:  database system was shut down at 2005-08-22 11:36:51 PDT
LOG:  checkpoint record is at 0/33FB28
LOG:  redo record is at 0/33FB28; undo record is at 0/0; shutdown TRUE
LOG:  next transaction ID: 565; next OID: 10791
LOG:  next MultiXactId: 1; next MultiXactOffset: 0
LOG:  database system is ready
LOG:  transaction ID wrap limit is 2147484144, limited by database "postgres"

[postgres@booktown ~]$

2.4.1.2. Stopping PostgreSQL with pg_ctl

The PostgreSQL postmaster backend can be stopped in the same fashion that it is started—by passing the stop argument to pg_ctl. The application pg_ctl checks for the running postmaster process, and, if the stop command was executed by the user who owns the running processes (e.g., postgres) the server is shut down.

There are three ways in which PostgreSQL can shut down the backend: smart, fast, and immediate. These arguments are passed to pg_ctl following the -m flag, to indicate the desired shutdown mode.

A smart shutdown (the default) causes PostgreSQL to wait for all clients to first cancel their connections before shutting down. A fast shutdown causes PostgreSQL to simply shut down through its normal routine, without checking client status. An immediate shutdown bypasses the normal shutdown procedure, and will require the system to go through a recovery mode when restarted.

Example 2-13 calls the pg_ctl script to stop the postmaster process in fast mode. The postmaster backend will not wait for any client connections to disconnect before shutting down.

Example 2-13. Stopping PostgreSQL with pg_ctl

[postgres@booktown ~]$ pg_ctl -D /usr/local/pgsql/data stop -m fast
waiting for postmaster to shut down....LOG:  received fast shutdown request
LOG:  shutting down
LOG:  database system is shut down
done
postmaster stopped

Note

The smart shutdown is equivalent to a kill -TERM on the running postmaster process, while fast is equivalent to a kill -INT, and immediate is equivalent to a kill -QUIT.

2.4.1.3. Restarting PostgreSQL with pg_ctl

You may pass the restart argument to pg_ctl as shorthand for sequential stop and start calls to pg_ctl. This argument may also specify the -m flag to indicate the preferred shutdown mode.

PostgreSQL stores the most recently used start-up options in a temporary file called postmaster.opts, within the PostgreSQL data path (PGDATA). This file is used when pg_ctl is invoked with the restart argument to ensure that your run-time options are preserved. Avoid placing your own configurations on the postmaster.opts file, as it will be overwritten when pg_ctl is executed with the start argument.

Example 2-14 restarts the Book Town database server with the postgres user.

Example 2-14. Restarting PostgreSQL with pg_ctl

[postgres@booktown ~]$ pg_ctl -D /usr/local/pgsql/data restart
waiting for postmaster to shut down....LOG:  received smart shutdown request
LOG:  shutting down
LOG:  database system is shut down
done
postmaster stopped
postmaster starting
[jd@jd pgsqldev]$ LOG:  database system was shut down at 2005-08-22 11:38:07 PDT
LOG:  checkpoint record is at 0/33FBB0
LOG:  redo record is at 0/33FBB0; undo record is at 0/0; shutdown TRUE
LOG:  next transaction ID: 565; next OID: 10791
LOG:  next MultiXactId: 1; next MultiXactOffset: 0
LOG:  database system is ready
LOG:  transaction ID wrap limit is 2147484144, limited by database "postgres"
[postgres@booktown ~]$

2.4.1.4. Checking status of PostgreSQL with pg_ctl

You may use the status argument to check the status of a running postmaster process. While not having any effect on the data itself, the data path must be known to pg_ctl. If the PGDATA environmental variable is not set, the -D flag must be passed to pg_ctl.

Example 2-15 checks the status of the Book Town PostgreSQL server.

Example 2-15. Checking status with pg_ctl

[postgres@booktown ~]$ pg_ctl -D /usr/local/pgsql/data status
pg_ctl: postmaster is running (PID: 26522)
/tmp/pgsqldev/bin/postgres
[postgres@booktown ~]$

2.4.2. Configuring the PostgreSQL SysV Script

The SysV script will allow the graceful control of the PostgreSQL database through the use of the SysV runlevel system. The SysV script can be used for starting, stopping, and status-checking of PostgreSQL. It is known to work with most Red Hat based versions of Linux, including Mandrake; however, it should work with other SysV systems (e.g., UnixWare, Solaris, etc.) with little modification. The script is named linux. To use it, you will first need to copy the linux script to your init.d directory. You may require root access to do this.

First, change to the directory where you unpacked the PostgreSQL source. In our case, the path to that directory is /usr/local/src/postgresql-8.0.1/. Then, issue a cp command to copy the script from contrib/start-scripts into the init.d directory. Example 2-16 demonstrates how to do this on a Red Hat Linux system.

Example 2-16. Copying the linux script

$ cd /usr/local/src/postgresql-8.0.1/
$ su -c "cp contrib/start-scripts/linux /etc/init.d/postgresql"

Example 2-16 arbitrarily re-names the new copy to postgresql; you may call it whatever you prefer, though it is typically named either postgresql, or postgres.

You will need to make the script file executable after copying it. To do so, use the command shown in Example 2-17.

Example 2-17. Making the linux script executable

$ su -c "chmod a+x /etc/init.d/postgresql"

There are no additional requirements to use the SysV script with Red Hat/Fedora, if you do not intend on using it to start PostgreSQL automatically (i.e., if you wish to use the script manually). However, if you do wish for the script to startup PostgreSQL automatically when the machine boots up (or changes runlevels) you will want to check your Linux distributions documentation on the specific ways this task is handled. RedHat and Fedora both use a program called chkconfig and the SysV script is configured to work with the chkconfig application.

Example 2-18. Starting PostgreSQL with the SysV script

$ /etc/init.d/postgresql start
Starting PostgreSQL: ok
$ /etc/init.d/postgresql stop
Stopping PostgreSQL: ok

Note

The SysV script logs redirects all PostgreSQL debugging output to $PGDATA/serverlog, by default. You can adjust this to put the log anywhere you like. It is common to have the log placed in /var/log/postgres.

The instructions for installation of the postgresql script are covered in Chapter 2. Depending on your machine's configuration, there may be more than one method of invoking the script once it has been properly installed. Remember that the actual name of the SysV script file in the /etc/init.d/ directory may be an arbitrary name, depending on how it was copied. The most common names given to this script are postgresql and postgres.

If your system supports the service command, you should be able to use it as a wrapper to the installed PostgreSQL script with the following syntax:

service postgresql { start | stop | restart | reload | status }

The service command accepts only the parameters described in the preceding syntax. No other options are accepted. You can modify the way any of these general modes runs by editing the script (e.g., /etc/rc.d/init.d/postgresql) manually. Example 2-19 uses the service command to start PostgreSQL.

Example 2-19. Starting PostgreSQL with service command

[root@booktown ~]# service postgresql start
Starting PostgreSQL: ok
[root@booktown ~]#

Alternatively, if the service command does not exist on your system, the postgresql script can be manually invoked with its complete system path:

/etc/init.d/postgresql { start | stop | restart | reload | status }

Example 2-20 checks the status of PostgreSQL's backend process by directly calling the postgresql script in the complete path. This assumes that your system has its SysV start-up scripts installed in the /etc/rc.d/init.d/ directory.

Example 2-20. Checking status with postgresql script

[root@booktown ~]# /etc/rc.d/init.d/postgresql status
pg_ctl: postmaster is running (PID: 26522)
/tmp/pgsqldev/bin/postgres
[root@booktown ~]#

As you can see from the output of Example 2-20, the SysV script is just a convenient wrapper to the pg_ctl command discussed in the previous section.

2.4.3. Calling postmaster Directly

The postmaster program is the multi-user PostgreSQL database server backend itself. This is the process that your PostgreSQL clients actually connect to, where a connection to a postgres backend is negotiated.

This binary is typically not called manually, but is indirectly executed through either the pg_ctl or SysV script discussed earlier in this section. However, these scripts at some point call the postmaster binary directly, and it can be helpful in configuring your PostgreSQL system to know what the postmaster is, and what it does.

The postmaster can only access one database cluster at a time, though you may have several concurrent postmaster applications running on different TCP/IP ports with a different database cluster for each.

Although you should never have to execute the postmaster directly. It can be useful for certain diagnostic situations. Next is the syntax for the postmaster program:

	postmaster is the PostgreSQL server.
	
	Usage:
	postmaster [OPTION]...
	
	Options:
	-B NBUFFERS     number of shared buffers
	-c NAME=VALUE   set run-time parameter
	-d 1-5          debugging level
	-D DATADIR      database directory
	-F              turn fsync off
	-h HOSTNAME     host name or IP address to listen on
	-i              enable TCP/IP connections
	-k DIRECTORY    Unix-domain socket location
	-N MAX-CONNECT  maximum number of allowed connections
	-o OPTIONS      pass "OPTIONS" to each server process
	-p PORT         port number to listen on
	-S              silent mode (start in background without logging output)
	--help          show this help, then exit
	--version       output version information, then exit
	
	Developer options:
	-n              do not reinitialize shared memory after abnormal exit
	-s              send SIGSTOP to all backend servers if one dies
	
	Please read the documentation for the complete list of run-time
	configuration settings and how to set them on the command line or in
	the configuration file.

The majority of the postmaster options can and should be set from the Section 3.1. If you need more information on the specific postmaster options please refer to the online documentation from the PostgreSQL project. However there is one option that can be useful if you find yourself needing to reindex system catalogs.

In the rare case that you find your system indexes corrupted you must shutdown your postgresql installation and start up the postmaster directly using the -P option.

Example 2-21. Using the postmaster -P option

         /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data -o -P
         

The displayed command will allow you to enter postgresql using psql and execute the reindex command on system level indexes.

The corruption of system indexes is rare and is usually indicitive of other problems such as faulty hardware or a significantly long time between routine maintenance tasks.

The postmaster also accepts the following two debugging options, for interested developers:

-n

The -n flag stops postmaster from re-initializing shared data structures. A debugging tool can then be used to gain information about the memory's state at the time of the crash.

-s

The -s flag causes postmaster to use the SIGSTOP signal to stop backend processes, without terminating the processes. Using this signal will keep the backend processes in memory instead of terminating them, which allows a developer to collect a core dump from each backend process manually. Each core dump can then be individually examined for debugging information.