How to find the number of active database connections in PostgreSQL

In this article, we will see how to find the number of active database connections in PostgreSQL. We can find number active connections to entire PostgreSQL server and individual databases using pg_stat_database view.

1. Find the number of active total connections in PostgreSQL Server.

SELECT sum(numbackends) FROM pg_stat_database;

How to find the number of active database connections in PostgreSQL

2. Find active connections on each database in PostgreSQL Server.

select datname, numbackends from pg_stat_database;