postgresqlHow do I kill idle connections in PostgreSQL?
Using the pg_terminate_backend() function, you can kill idle connections in PostgreSQL.
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'DBNAME'
AND state = 'idle';
This query will terminate all idle connections in the database.
The parts of the query are as follows:
SELECT pg_terminate_backend(pid)
- This part of the query will terminate the backend process.FROM pg_stat_activity
- This part of the query will get the list of processes from thepg_stat_activity
view.WHERE datname = 'DBNAME'
- This part of the query will filter the results by the database name.AND state = 'idle'
- This part of the query will filter the results by the state of the connection.
For more information, please refer to the PostgreSQL Documentation.
More of Postgresql
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can Zalando use PostgreSQL to improve its software development?
- How do I install and configure PostgreSQL on a Windows machine?
- How can I use PostgreSQL and ZFS snapshots together?
- How can I set a PostgreSQL interval to zero?
- How can I use PostgreSQL XML functions to manipulate XML data?
- How can I convert XML data to a PostgreSQL table?
- How can I use PostgreSQL and Node.js together to develop a software application?
- How do I use the PostgreSQL hash function?
- How can I use PostgreSQL's "zero if null" feature?
See more codes...