check psql session being used database code example
Example 1: psql connections
SELECT * FROM pg_stat_activity;
Example 2: postgresql print sessions using the database
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;