How to do it…

To see which connected users are running at this moment, just run the following:

SELECT datname, usename, state, query
FROM pg_stat_activity;

On systems with a lot of users, you may notice that the majority of backends have state set to idle. This denotes that no query is actually running, and PostgreSQL is waiting for new commands from the user. The query field shows the statement that was last executed by that particular backend.

If, on the other hand, you are interested in active queries only, limit your selection to those records that have state set to active:

SELECT datname, usename, state, query
FROM pg_stat_activity WHERE state = 'active';