How to find which queries are running PostgreSQL

In this article, we will see how to find which queries are running PostgreSQL. We can find the present running queries by querying against the system view pg_stat_activity.

To find, we must be logged in as a superuser or as the same database user we want to check.

What needs to be done to check this?

We have to set parameter track_activities=on in the postgresql.conf file or using the following SQL statement.

set track_activities=on

Find which queries are running PostgreSQL

Following is the query to get queries running in PostgreSQL server.

select datname, usename, state, query from pg_stat_activity where state='active';