pg_isready: check the connection status of a PostgreSQL server
pg_isready [OPTION]...
What it does
Asks the server whether it accepts connections, without logging in. Exit status 0 = accepting, 1 = rejecting (e.g. starting up), 2 = no response, 3 = no attempt (invalid parameters). What readiness probes and load balancers use.
Options you will use
-d, --dbname=DBNAME- database name.
-q, --quiet- run quietly.
-t, --timeout=SECS- seconds to wait when attempting connection, 0 disables (default: 3).
-h, --host=HOSTNAME- database server host or socket directory.
-p, --port=PORT- database server port.
-U, --username=USERNAME- user name to connect as.
Examples
$ pg_isready/var/run/postgresql:5432 - accepting connections
$ pg_isready -h db.internal -p 5432 -q; echo $?for a script or a probe
Gotchas
- It never logs in: a server that would refuse your password in pg_hba.conf still answers "accepting connections". Readiness, not access - test a login with psql.
- "no response" (exit 2) is the server down, the wrong port or host, or a firewall in between; "rejecting connections" (exit 1) is a server starting up, shutting down or in recovery without hot_standby.
- Without -h it uses the Unix socket in /var/run/postgresql on Debian and Ubuntu, so it can say "accepting" while TCP on 5432 is not listening (listen_addresses).
Taught in
Try pg_isready in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.