How to get the total of current connection in PostgreSQL?

Dung Do Tien Apr 10 2021 57

Hello, I have a small project using the PostgreSQL database. As I know default maximum number connection of PostgreSQL is 100 connections at the same time. So how can I get the total current connection is connecting to my database?

I want to make a service and send a message to Telegram if the total current connection larger than 70%.

I try the query below but it's not correct:

select count(*) from pg_stat_activity;

Please, help me how to get the total of the current connection in PostgreSQL?

Thank a lot.

Have 2 answer(s) found.
  • J

    Jose Luis Apr 10 2021

    I usually command below to tracking  total current connections in PostgreSQL :

    SELECT usename, datname, COUNT(1)
    FROM pg_stat_activity
    GROUP BY usename, datname

    Because a database maybe has many users with many DB names so above command will filter by user login.

    I hope it useful for you.

  • D

    Denis Cano Apr 10 2021

    It depends on the version of Postgre but I usually use the command below to get count current connections

    SELECT sum(numbackends) FROM pg_stat_database;

    You can use SELECT * FROM pg_stat_database; command to get all columns to have in that table and you can get more information from it such as DB name, deadlock, conflict...

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close