Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to check postgresql database status

Tags:

postgresql

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?

like image 598
prisoner_of_azkaban Avatar asked Sep 13 '17 09:09

prisoner_of_azkaban


People also ask

How do I find active sessions in PostgreSQL?

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.

What is Pg_isready in PostgreSQL?

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.

How can I tell if postgres is running on localhost?

Alternatively you can just "SELECT 1" with psql, and check output: =$ psql -h 127.0. 0.

How do I select a specific database in PostgreSQL?

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 −

How to connect to testdb using PostgreSQL client?

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.

How to check if PostgreSQL database server is active or not?

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.

How to check if PostgreSQL server is running on port 5432?

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 :


1 Answers

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

like image 71
3 revs, 2 users 80% Avatar answered Oct 09 '22 22:10

3 revs, 2 users 80%