How to see active SQL queries and open connections in Postgres?

This article will show you how to see a list of open database connections as well as all active queries that are running on a PostgresSQL 8.x database. This information can be very beneficial when profiling your application and determining queries that have “gone wild” and are eating CPU cycles.

  1. Login to the PostgresSQL command-line interface
  2. psql -U [username] [database_name]

  3. Run the following query:
  4. SELECT datname,usename,procpid,client_addr,waiting,query_start,current_query FROM pg_stat_activity;

See also  Understanding PostgreSQL Reset Statistics with pg_stat_reset

5 thoughts on “How to see active SQL queries and open connections in Postgres?”

  1. Hi!

    The Query does not work for newer Versions of PostgreSQL. ‘procpid’ was renamed to ‘pid’ and ‘current_query’ is ‘query’ now. So:
    SELECT datname,usename,pid,client_addr,waiting,query_start,query FROM pg_stat_activity;

    Sebastian

  2. “\c” is not working in windows – psql

    How to clear that error…….?

    ERROR: syntax error at or near “\”
    LINE 3: \c busservice
    ^
    ********** Error **********

    ERROR: syntax error at or near “\”
    SQL state: 42601
    Character: 67

  3. postgres=# SELECT datname,usename,procpid,client_addr,waiting,query_start,current_query FROM pg_stat_activity;
    ERROR: column “procpid” does not exist

    ???

Leave a Comment