Sub topic 3
Checking That the PostgreSQL Server Is Running
This chapter assumes that PostgreSQL is installed and that you are comfortable using a terminal. You should also know the difference between a database server and a database client. The server stores data and accepts connections. The psql client sends commands to the server.
Client and server have different jobs
PostgreSQL usually runs as a background service. It listens for incoming connections on a network address and port. The default port is 5432.
psql is a command-line client. It does not store your databases. It connects to a running PostgreSQL server, sends SQL statements, and displays the results.
Check the installed tools
First, confirm that the client is installed:
psql --versionA successful command prints a version such as:
psql (PostgreSQL) 16.4The version number can differ from this example. This command checks only the psql program. It does not prove that the database server is running.
Next, use pg_isready:
pg_isreadyA successful result looks similar to this:
/var/run/postgresql:5432 - accepting connectionsThis means a PostgreSQL server is listening at the reported address and port. It does not authenticate you as a particular database user, and it does not confirm that you can access every database.
If the server is stopped, you may see:
/var/run/postgresql:5432 - no responseThat result usually means that no server is listening at the address and port checked by pg_isready.
Check a specific server
You can provide the host and port explicitly:
pg_isready -h localhost -p 5432The -h option sets the host. localhost means the server is running on your own computer. The -p option sets the port.
A server can be running but listening on a different port. In that case, checking port 5432 reports no response even though PostgreSQL is active elsewhere. Use the port configured for your installation.
Test a real connection
The following command connects to the local server as the postgres role:
psql -h localhost -p 5432 -U postgresIf PostgreSQL requests a password, enter the password for that role. If the connection succeeds, psql displays a prompt such as:
postgres=#Run a simple query:
SELECT version();The server returns its full version string. End the client session with:
\qDiagnose the result
| Result | Likely meaning | Next check |
|---|---|---|
| `accepting connections` | The server is ready for connections | Try `psql` |
| `no response` | The server is stopped or the address or port is wrong | Start PostgreSQL or check its port |
| `connection refused` | Nothing is listening at that host and port | Check the service and port |
| `password authentication failed` | The server responded, but the credentials were rejected | Check the role and password |
Exercise
What does pg_isready confirm, and what does it not confirm?
Show answer
It confirms that a PostgreSQL server is accepting connections at a host and port. It does not confirm that a specific user can authenticate or access a particular database.