Watching psql queries

You can use psql’s \watch meta-command, which re-executes a query every n seconds. Example usage:

SELECT pid, (CURRENT_TIMESTAMP - query_start) as query_time, datname, usename, query
FROM pg_stat_activity
ORDER BY query_time DESC
\watch 1

so you get tail -f-like functionality within psql.

h/t Chris Shaw