How to know when your Postgres Service is ready

I am sure a lot of us have found ourselves in a situation where we need to wait for a service to be ready. I don’t mean the VM or container is running. I mean the service is up and running and fully functional.

Postgres provides an easy to use command called pg_isready that allows us to know exactly when postgres is ready to accept connections.

pg_isready is a utility for checking the connection status of a PostgreSQL database server. The exit status specifies the result of the connection check. (taken from

Example Script

Here is a simple shell script where we poll the postgres service every 2 seconds until it is ready to accept connections.

pg_uri="postgres://postgres:[email protected]:5432/shield"
# make sure pg is ready to accept connections
until pg_isready -h postgres-host -p 5432 -U postgres
  echo "Waiting for postgres at: $pg_uri"
  sleep 2;
# Now able to connect to postgres

Spread the word

twitter icon facebook icon linkedin icon