Starting the Database Server

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 /usr/local/pgsql/data
This leaves the server running in the foreground. Without a -D, the server will try to use the data directory in the environment variable PGDATA; if neither of these works, the command to start postmaster fails.

To start the postmaster in the background:
> postmaster -D /usr/local/pgsql/data > logfile 2>&1 &
It is a good idea to keep the server output around somewhere, as indicated here. It will help both for auditing purposes and to diagnose problems.

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
will start the server in the background and put the output into the named log file. The -D option has the same meaning as when invoking postmaster directly. pg_ctl also implements a symmetric "stop" operation.

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 /usr/local/pgsql/data -l serverlog' postgres

Either add
/usr/local/pgsql/bin/pg_ctl start -l logfile \
   -D /usr/local/pgsql/data
to /etc/rc.d/rc.local or look into the file contrib/start-scripts/linux in the PostgreSQL source distribution to integrate the start and shutdown into the run level system. Whichever approach you take, run the server by using the PostgreSQL user account and not by root or any other user. Therefore, form your command lines along the lines of su -c '...' postgres, for example:
su -c '/usr/local/pgsql/bin/pg_ctl start -l logfile \
-D /usr/local/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.

Server Start-up Failures

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?
This usually means just what it suggests: you accidentally started a second postmaster on the same port where one is already running. However, if the kernel error message is not Address already in use or some variant of that wording, there may be a different problem. For example, trying to start a postmaster on a reserved port number may draw something like
> 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
probably means that your kernel's limit on the size of shared memory areas is smaller than the buffer area that PostgreSQL is trying to create (83918612 bytes in this example). As a temporary workaround, you can try starting the postmaster with a smaller-than-normal number of buffers (-B switch). You will eventually want to reconfigure your kernel to increase the allowed shared memory size, however. You may see this message when trying to start multiple postmaster programs on the same machine, if their total space requests exceed the kernel limit.

An error such as
IpcSemaphoreCreate: semget(key=5440026, num=16, 01600) failed: 
No space left on device
does not mean that you've run out of disk space; it means that your kernel's limit on the number of semaphores is smaller than the number PostgreSQL wants to create. As above, you may be able to work around the problem by starting the postmaster with a reduced number of backend processes (-N switch), but you'll eventually want to increase the kernel limit.

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.

Client Connection Problems

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?
This is the generic "No server could br found" failure. It looks like the above when TCP/IP communication is attempted. A common mistake is to forget the -i to the postmaster to allow TCP/IP connections.

Alternatively, you'll 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.