pg_isready(1)
Check the connection status of a PostgreSQL server.
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
| Flag | What it does |
|---|---|
-h, --host=HOSTNAME | Database server hostname or IP address (default: localhost) |
-p, --port=PORT | Database server port number (default: 5432) |
-U, --username=USERNAME | PostgreSQL username for the connection attempt |
-d, --dbname=DBNAME | Database name to connect to |
-t, --timeout=SECONDS | Maximum wait time in seconds (default: 3) |
-q, --quiet | Suppress output; only return exit code |
-V, --version | Show version information and exit |
-?, --help | Show help message and exit |
Examples
Check if PostgreSQL is running on localhost:5432 (default settings)
pg_isreadyCheck remote PostgreSQL server on db.example.com port 5433, quiet mode
pg_isready -h db.example.com -p 5433 -qVerify 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; doneCheck specific database connection as a particular user
pg_isready -h 192.168.1.10 -d myapp_db -U appuserCheck server status and display the exit code (0=ready, 1=rejecting, 2=no response)
pg_isready -h localhost; echo $?