How to use the command pg_isready (with examples)
The pg_isready
command is used to check the connection status of a PostgreSQL server. It provides a simple way to determine if a PostgreSQL server is accepting connections or not.
Use case 1: Check connection
Code:
pg_isready
Motivation: This use case is useful when you want to quickly check if the PostgreSQL server is accepting connections.
Explanation: In this use case, the pg_isready
command is used without any arguments. It checks the connection status of the default PostgreSQL server running on the local machine.
Example output:
/var/run/postgresql:5432 - accepting connections
Explanation: The command checks the connection status of the PostgreSQL server on the default port (5432) and displays the result. In this example, it shows that the server is accepting connections.
Use case 2: Check connection with a specific hostname and port
Code:
pg_isready --host=hostname --port=port
Motivation: This use case is useful when you want to check the connection status of a PostgreSQL server running on a specific hostname and port.
Explanation: In this use case, the --host
and --port
options are used to specify the hostname and port of the PostgreSQL server that needs to be checked.
Example output:
hostname:port - accepting connections
Explanation: The command checks the connection status of the PostgreSQL server running on the specified hostname and port. In this example, it shows that the server is accepting connections.
Use case 3: Check connection displaying a message only when the connection fails
Code:
pg_isready --quiet
Motivation: This use case is useful when you want to check the connection status of a PostgreSQL server and display a message only when the connection fails.
Explanation: In this use case, the --quiet
option is used to suppress the normal output of the pg_isready
command when the connection is successful. It will only display a message when the connection fails.
Example output:
ok
Explanation: The command checks the connection status of the default PostgreSQL server running on the local machine. Since the connection is successful, it doesn’t display any output.