Photo by Rohit Varma on Unsplash
How to Set PostgreSQL to Auto-Close Idle Sessions Over 2 Minutes
PostgreSQL settings to close idle sessions after 2 minutes
To configure PostgreSQL to close idle sessions after a specific period, such as 2 minutes, you can use the idle_in_transaction_session_timeout parameter.
Open the PostgreSQL configuration file postgresql.conf for editing. The location of this file varies depending on your operating system and installation method. Common locations are /etc/postgresql/{version}/main/postgresql.conf on Linux or C:\Program Files\PostgreSQL\{version}\data\postgresql.conf on Windows.
Search for the idle_in_transaction_session_timeout parameter in the postgresql.conf file. If it's not present, you can add it to the file.
Set the value of idle_in_transaction_session_timeout to 120000 milliseconds, which is equivalent to 2 minutes. The value is specified in milliseconds.
idle_in_transaction_session_timeout = 120000
Save the changes to the postgresql.conf file and restart the PostgreSQL server for the changes to take effect.
After making this configuration, any idle session that remains idle for more than 2 minutes (120000 milliseconds) will be automatically terminated by the server. This setting helps to reduce resource usage and free up connections that are no longer needed.
Please note that setting a very low value for idle_in_transaction_session_timeout may lead to unexpected termination of sessions that are performing long-running tasks. Therefore, it's essential to choose an appropriate value based on your application's requirements and workload.