Show active database connections
Read-only
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Lists every connection currently open on the server with the connected user, database, application name, client address, and query state so you can identify idle sessions or pending queries.
postgresqlconnectionsactivityinspect
Commands
Display all active and idle connections with their current query
psql -c "SELECT pid, usename, datname, application_name, client_addr, state, query FROM pg_stat_activity WHERE state IS NOT NULL ORDER BY state;"Verification
psql -c "SELECT count(*) AS connection_count FROM pg_stat_activity;"A positive count of current connections is printed, including the query session itself.
Undo
Not undoable
This recipe only reads server activity metadata and changes nothing.
Pitfalls
- The output includes the current psql session itself; expect at least one row.
- Only superusers can see queries run by other users; non-superusers see <insufficient privilege>.
- State may be "idle" for connections holding long-lived transactions that are still blocking resources.