Before anyone can access the database, you must start the database server, which is called postmaster. To direct the postmaster to the data it is supposed to work on, use the -D option. For example, log into the postgres user account and enter:
> postmaster -D /var/lib/pgsql/data |
To start the postmaster in the background:
> postmaster -D /var/lib/pgsql/data > logfile 2>&1 & |
The postmaster can take a number of other command-line options. For more information see the reference page and below under runtime configuration. In particular, in order for the postmaster to accept TCP/IP connections (rather than just UNIX-domain socket ones), you must also specify the -i option.
A shell script wrapper pg_ctl is provided that encapsulates some of the tasks:
pg_ctl start -l logfile |
Normally, you will want to start the database server when the computer boots up. However, this is not required; the PostgreSQL server can be run successfully from non-privileged accounts without root intervention.
Your system has /etc/rc.local and /etc/rc.d/rc.local files which are good places to put such a command. Whatever you do, the server must be run by the PostgreSQL user account and not by root or any other user. Therefore you probably always want to form your commands along the lines of su -c '...' postgres, for example:
su -c 'pg_ctl -D /var/lib/pgsql/data -l serverlog' postgres |
Either add:
/usr/bin/pg_ctl start -l logfile -D /var/lib/pgsql/data |
su -c '/usr/bin/pg_ctl start -l logfile -D /var/lib/pgsql/data' postgres |
While the postmaster is running, its PID is in the file postmaster.pid in the data directory. This is used as an interlock against multiple postmaster programs running in the same data directory, and can also be used for shutting down the postmaster.
There are several common reasons for the postmaster to fail to start up. Check the postmaster log file, or start it by hand (without redirecting standard output or standard error) to see what complaint messages appear. Some of the possible error messages are self-explanatory, but here are some that are not.
FATAL: StreamServerPort: bind() failed: Address already in use Is another postmaster already running on that port? |
> postmaster -i -p 666 FATAL: StreamServerPort: bind() failed: Permission denied Is another postmaster already running on that port? |
A message such as
IpcMemoryCreate: shmget(key=5440001, size=83918612, 01600) failed: Invalid argument FATAL 1: ShmemCreate: cannot create region |
An error such as
IpcSemaphoreCreate: semget(key=5440026, num=16, 01600) failed: No space left on device |
If you get an "illegal system call" error, then it is likely that shared memory or semaphores are not supported at all in your kernel. In that case your only option is to re-configure the kernel to turn on these features.
Although the possible error conditions on the client side are both virtually infinite and application dependent, a few of them might be directly related to how the server was started up. Conditions other than those shown below should be documented with the respective client application.
PQconnectPoll() -- connect() failed: Connection refused Is the postmaster running (with -i) at 'server.joe.com' and accepting connections on TCP/IP port 5432? |
Alternatively, you will get this when attempting UNIX-socket communication to a local postmaster:
connectDBstart() -- connect() failed: No such file or directory. Is the postmaster running locally and accepting connections on UNIX socket '/tmp/.s.PGSQL.5432'? |
The last line is useful in verifying that the client is trying to connect where it is supposed to. If there no postmaster running there, the kernel error message will typically be either Connection refused or No such file or directory, as illustrated. (It is particularly important to realize that Connection refused in this context does not mean that the postmaster got your connection request and rejected it -- that case will produce a different message, as shown in the chapter on Client Authentication.) Other error messages such as Connection timed out may indicate more fundamental problems, such as a lack of network connectivity.