Is there any query to check the database status? I just want to know if it is active or not on the remote host.So, Is there any query which return 200 OK if db is running and other db detail?
SELECT * FROM pg_stat_activity WHERE datname = 'dbname' and state = 'active'; Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active , database name and connection state should be included in the query to get the desired output.
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.
Alternatively you can just "SELECT 1" with psql, and check output: =$ psql -h 127.0. 0.
You can select the database using either of the following methods − Assume you have already launched your PostgreSQL client and you have landed at the following SQL prompt − You can check the available database list using \l, i.e., backslash el command as follows −
Assume you have already launched your PostgreSQL client and you have landed at the following SQL prompt − You can check the available database list using \l, i.e., backslash el command as follows − Now, type the following command to connect/select a desired database; here, we will connect to the testdb database.
After that, in order to check the service whether it is active or not, it is important to know the service name which represents PostgreSQL Database Server. It can be done in several Linux operating system distribution by checking the service configuration file which is normally located in /etc depends on the Linux variant used.
Type the following command to do that : cat /etc/service | grep 5432 If PostgreSQL database server has already installed and it is configured to listen and handle request in port 5432, below is the possible outcome which is going to be shown by executing the above command in the bash prompt terminal :
https://www.postgresql.org/docs/current/static/app-pg-isready.html
-bash-4.2$ if pg_isready93 -d postgres://localhost:5432/template; then echo "200 OK"; else echo "500 NOT OK"; fi
200 OK
-bash-4.2$ if pg_isready93 -d postgres://localhost:5430/template; then echo "200 OK"; else echo "500 NOT OK"; fi
500 NOT OK
credits to Abelisto - I would not think of pg_isready
in this context
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With