psql active connections code example
Example 1: check connections to database postgres
SELECT * FROM pg_stat_activity WHERE datname='db_name';
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;