$linuxjunkies
>

pg_isready(1)

Check the connection status of a PostgreSQL server.

UbuntuDebianFedoraArch

Synopsis

pg_isready [OPTION]... [DBNAME]

Description

pg_isready is a utility that checks the connection status of a PostgreSQL database server. It attempts to connect to the specified server and reports whether the server is accepting connections. This tool is useful for scripts and monitoring systems that need to verify PostgreSQL availability before running queries.

The command returns an exit code indicating the server state: 0 if accepting connections, 1 if rejecting connections, 2 if no response, or 3 if no attempt was made. This makes it ideal for use in shell scripts and automated health checks.

Common options

FlagWhat it does
-h, --host=HOSTNAMEDatabase server hostname or IP address (default: localhost)
-p, --port=PORTDatabase server port number (default: 5432)
-U, --username=USERNAMEPostgreSQL username for the connection attempt
-d, --dbname=DBNAMEDatabase name to connect to
-t, --timeout=SECONDSMaximum wait time in seconds (default: 3)
-q, --quietSuppress output; only return exit code
-V, --versionShow version information and exit
-?, --helpShow help message and exit

Examples

Check if PostgreSQL is running on localhost:5432 (default settings)

pg_isready

Check remote PostgreSQL server on db.example.com port 5433, quiet mode

pg_isready -h db.example.com -p 5433 -q

Verify server is ready before executing a query

pg_isready -h localhost -U postgres && psql -h localhost -U postgres -c 'SELECT version();'

Retry connection check up to 5 times with 1-second intervals

for i in {1..5}; do pg_isready -h db_server -t 2 && break || sleep 1; done

Check specific database connection as a particular user

pg_isready -h 192.168.1.10 -d myapp_db -U appuser

Check server status and display the exit code (0=ready, 1=rejecting, 2=no response)

pg_isready -h localhost; echo $?

Related commands