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 do I install PostgreSQL and Zabbix on my system?
- How can I retrieve data from PostgreSQL for yesterday's date?
- How can I use PostgreSQL with YAML?
- How do I create a PostgreSQL function?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I use PostgreSQL's "zero if null" feature?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How can I extract the year from a PostgreSQL timestamp?
- How can I use PostgreSQL with Zabbix?
See more codes...