Connect to PostgreSQL:   psql -U xxx -d xxx

Method 1:

SELECT COUNT(pid) FROM pg_stat_activity;

Method 2:

SELECT sum(numbackends) FROM pg_stat_database;