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_activityview.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 do I use PostgreSQL's XMIN and XMAX features?
- How can I monitor PostgreSQL performance using Zabbix?
- How can I integrate PostgreSQL with Yii2?
- How can I use PostgreSQL with YAML?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How do I use PostgreSQL UNION to combine the results of two queries?
- How do I decide whether to use PostgreSQL VARCHAR or TEXT data types?
- How do I use PostgreSQL with Qt?
- How can I retrieve data from PostgreSQL for yesterday's date?
See more codes...