Share

How to check active user sessions in PostGres

By: Arif Khan.

Every Database has its own data dictionaries, which contains useful information about Database Activities/Sessions and Objects. Postgres also has such dictionaries.

In this block we will see that, how to check active database session in postgresql.

pg_stat_activity

This dictionary holds the information about user sessions, we can access that information by querying this dictionary in any query editor or in PgAdmin query tool. Dictionary contains following columns / information.

Query

select pid as process_id, 
       usename as username, 
       datname as database_name, 
       client_addr as client_address, 
       application_name,
       backend_start,
       state,
       state_change
from pg_stat_activity;

After execution of above query you will see the resulting rows. Each row represent one active session. So, by executing this query you will be able to see all sessions and their state. You can run this query in any query builder or PgAdmin i have executed it in PgAdmin4, as shown below.

Session in PgAdmin4

To check the database sessions in PgAdmin4 login to PgAdmin4, click the user, select Dashboard from above menu, you will see the server activity as seen in below image.