Check whether PostgreSQL User is connected or not

In this tutorial, we find certain user connected to PostgreSQL server or not by querying pg_stat_activity system view.

If below query returns any results means, then user is connected to PostgreSQL Server database.

Check whether PostgreSQL User is connected or not:

select usename,datname from pg_stat_activity where usename='anil';

Output:

postgres=> select usename,datname from pg_stat_activity where usename='anil'; usename | datname ---------+---------- anil | postgres (1 row)

Which means user anil connected to database in PostgreSQL Server.

Check whether PostgreSQL User is connected or not