Is it possible to configure PostgreSQL to automatically close idle connections?

Finally, I have a solution. I'll use the tool PgBouncer with its parameter server_idle_timeout.


Another approach would consist in using some cron-like tool running a query periodically to find idle connections.

Once those idle connections are found, a simple call to pg_terminate_backend will close them.

See an in depth description of this approach in the anwser of the following question:

How to close idle connections in PostgreSQL automatically?


Since Postgres 9.5 you can configure a timeout for connection in the state "idle in transaction" - which is a bit different just "idle" but typically "idle in transaction" connection generate more problems

See the manual for details: idle_in_transaction_session_timeout