Sub topic 3
Checking that the server is running
PostgreSQL uses a client-server model. Installing it puts programs on disk, but nothing can connect until a server process is running. This chapter shows how to confirm that process is up and accepting connections, and how to tell the different failure states apart.
This chapter assumes you have:
- installed PostgreSQL by following the previous chapter
- a terminal on Linux, macOS, or Windows
- a rough idea of what a process and a network port are
Three separate questions
"Is Postgres running?" is really three questions. Each has its own check, and a failure at one level explains symptoms at the next.
- Is the client software installed? This is
psql --version. - Is a server process running under the operating system's service manager?
- Is that process accepting connections on the host and port you expect? This is
pg_isready.
A server can be running but still refuse connections. It might be starting up, recovering after a crash, or listening on a different port than you assumed. Checking all three levels tells you which case you are in.
Check the client and the server together
Start with the two commands from the tutorial plan.
psql --version
pg_isreadypsql --version prints something like psql (PostgreSQL) 16.4. It only reads the local client program. It says nothing about any server. If you get "command not found", the client's bin directory is not on your PATH. This is common on Windows installs and on macOS Homebrew installs of a versioned package such as postgresql@16.
pg_isready is a small utility that ships with PostgreSQL. It opens a connection to the server and reports the result. It does not need a password or a valid role, so it works before you have set up any access. With no options it checks the default host and port. On Linux and macOS the default host is the local Unix socket directory. The default port is 5432.
A healthy server prints a line like this:
/var/run/postgresql:5432 - accepting connectionsThe socket directory in the output varies by platform. Some builds use /tmp. The words "accepting connections" are what matter.
Read the exit code
pg_isready is designed for scripts, so its exit status carries the result. The printed message matches it.
| Exit code | Message | Meaning |
|---|---|---|
| 0 | accepting connections | The server is up and ready. |
| 1 | rejecting connections | The server is reachable but not ready yet, for example while starting or recovering. |
| 2 | no response | Nothing answered at that address. The server is stopped, or you used the wrong host or port. |
| 3 | no attempt | The check was not made because the parameters were invalid. |
You can see the code with your shell's last-status variable:
pg_isready -h localhost -p 5432
echo $?On Windows PowerShell, use $LASTEXITCODE instead of $?. In cmd.exe, use %ERRORLEVEL%.
Code 1 usually clears by itself. After a crash, PostgreSQL replays its write-ahead log before it accepts connections, and that can take seconds or minutes. A script that waits for the database can loop on exit code 1 and give up only on code 2 or 3. Here is a simple wait loop:
for i in $(seq 1 30); do
if pg_isready -q -h localhost -p 5432; then
echo "ready"
break
fi
sleep 1
doneThe -q flag suppresses the message and leaves only the exit status.
Check the service manager
If pg_isready reports "no response", ask the operating system whether the service is running. The commands depend on how you installed PostgreSQL.
Linux with systemd
On Debian and Ubuntu, the packaged service is usually named postgresql:
systemctl status postgresqlOn Red Hat, Fedora, and Rocky Linux, the unit name includes the major version, such as postgresql-16. List what exists with:
systemctl list-units 'postgresql*'Look for active (running) in the status output. On Debian-based systems, the package also provides pg_lsclusters. It prints each cluster's version, port, and status, such as online or down. This is useful when more than one version is installed.
If the service is stopped, start it. Use enable so that it also starts at boot.
sudo systemctl start postgresql
sudo systemctl enable postgresqlmacOS with Homebrew
brew services listFind the postgresql@16 line, or whichever version you installed, and check that its status is started. If it is not, run brew services start postgresql@16. If you used the graphical installer from EnterpriseDB instead, the server is registered with launchd, and the installer's bundled tools can start and stop it.
Windows
The installer registers a Windows service with a name like postgresql-x64-16. In PowerShell:
Get-Service postgresql*The Status column should read Running. You can also open the Services app and look for the same name. To start it from an elevated prompt, run Start-Service postgresql-x64-16, using the exact name from the listing.
Check the port directly
Sometimes the service is running but listening somewhere you did not expect. This happens when two versions are installed side by side, because the second one is given port 5433. To see which processes listen on PostgreSQL ports on Linux:
ss -ltn | grep -E ':543[0-9]'A line containing 127.0.0.1:5432 means a server is listening for local TCP connections on the default port. If you see only 5433, run pg_isready -p 5433 to confirm.
You can also test a Unix socket path directly. The socket is a file named after the port, for example /var/run/postgresql/.s.PGSQL.5432. If the directory exists but that file does not, the server is not running with that port.
Why a running service can still fail
These cases look the same from a distance ("I can't connect") but have different causes.
- Wrong port. Another instance owns 5432, or your server was configured with a different
portinpostgresql.conf. - Listening on the wrong address. By default the server listens only on
localhost. A client on another machine gets "no response" even though the service is healthy. The setting islisten_addresses. - Firewall. The server is reachable locally but blocked remotely. This matters only for remote connections.
- Startup failure. The service manager shows
failed. The cause is usually in the server log, for example a port already in use or a data directory with wrong permissions. Debian keeps logs under/var/log/postgresql/. Usejournalctl -u postgresqlon systemd systems. - Stale lock file. After a hard shutdown, a leftover
postmaster.pidin the data directory can make the next start fail. Check the log before removing anything. Deleting the file while a server is actually running can corrupt data.
Putting it together
A reliable order is:
- Run
psql --versionto confirm the client is installed. - Run
pg_isready. If it exits with 0, move on to connecting. - If it exits with 1, wait a few seconds and repeat.
- If it exits with 2, check the service manager, then the listening port, then the log.
- If it exits with 3, correct the host or port you passed.
This ordering narrows the problem one layer at a time, so you avoid trying random fixes. The next chapter starts psql and uses the same host and port.
Exercise
You run pg_isready -h localhost -p 5432 and then echo $?. The output is localhost:5432 - no response and 2. On Ubuntu, systemctl status postgresql shows active (exited), and pg_lsclusters shows a cluster on port 5433 with status online. What is the most likely cause, and what command confirms it?
Show answer
The server is running, but on port 5433, not 5432, so the check at 5432 gets no response. (On Debian-based systems the postgresql unit is a wrapper, so active (exited) is normal.) Run pg_isready -h localhost -p 5433. It should print "accepting connections" and exit with 0. Then use -p 5433 in later psql commands.
Exercise
A deployment script starts PostgreSQL and immediately runs a migration. It sometimes fails right after a crash restart. Using pg_isready, how would you make it wait correctly, and which exit codes should end the wait?
Show answer
Loop on pg_isready -q and sleep between attempts. Exit code 0 ends the wait successfully. Exit code 1 means the server is still starting or recovering, so keep waiting. Exit codes 2 and 3 are failures (nothing is listening, or the parameters are wrong), so stop after a time limit or immediately for 3.