PostgreSQL Error 57P05: Idle Session Timeout Fix
In this article
PostgreSQL error 57P05 means the server terminated your connection because the session sat idle longer than the configured idle_session_timeout allows. The fix is to either raise or disable that timeout on the server, or keep the connection active from the client. You cannot change the timeout from inside a session that has already been killed; you need a new connection with sufficient privileges.
What You'll Need
- Access to the PostgreSQL server as a superuser or a role with permission to change server settings (for example,
ALTER SYSTEMor membership inpg_read_all_settingsplusALTER SYSTEMrights). - A client tool such as
psql, pgAdmin, or your application's database console. - The ability to edit
postgresql.confor run SQL commands, depending on how your server is managed (self-hosted, cloud, or container). - Knowledge of whether your provider manages the setting for you (many managed services expose it as a parameter group or console option).
Step-by-Step
- Confirm the error is really 57P05. The message usually reads
FATAL: terminating connection due to idle-session timeoutand the SQLSTATE is57P05. If you see57P01(admin shutdown) or57P02(crash shutdown), this is a different problem. - Check the current setting. Connect with
psqland runSHOW idle_session_timeout;. A value of0means the timeout is disabled; any other value is a duration such as5minor300000(milliseconds). Also runSELECT name, setting, source FROM pg_settings WHERE name = 'idle_session_timeout';to see where the value comes from (configuration file, database, role, or session). - Decide whether to change the server default or a specific role/database. For a global change, use
ALTER SYSTEM SET idle_session_timeout = 0;(or a larger value like'30min') and thenSELECT pg_reload_conf();. For a single role, useALTER ROLE your_role SET idle_session_timeout = 0;. For a single database, useALTER DATABASE your_db SET idle_session_timeout = 0;. Role and database settings take effect on new sessions for that role or database. - If you prefer editing the configuration file, open
postgresql.conf(location varies by installation; on Linux it is often under/etc/postgresql/<version>/main/or/var/lib/pgsql/data/). Find or add the lineidle_session_timeout = 0(or a larger duration), save, and reload the server withpg_ctl reloadorSELECT pg_reload_conf();. A full restart is not required for this parameter. - For managed cloud databases, use the provider's console or parameter group. The exact menu labels differ by provider and version, so look for a database parameter named
idle_session_timeoutand set it to0or a larger value, then apply the change. Some providers do not allow disabling it; in that case set the largest value they permit. - Keep client connections alive if you cannot change the server. Configure your connection pool or driver to send a lightweight query before the timeout expires. For example, set a pool
validationQueryorkeepaliveinterval shorter than the timeout. Inpsql, you can run a periodicSELECT 1;manually, but for applications use the pool's built-in keepalive option. - Reconnect and verify. Open a new session and run
SHOW idle_session_timeout;again. Leave the session idle for longer than the old timeout and confirm it stays connected. If it still drops, check for other timeouts such asidle_in_transaction_session_timeoutor a network/firewall idle timeout.
Common Mistakes
- Confusing
idle_session_timeoutwithidle_in_transaction_session_timeout. The latter kills sessions that are idle inside a transaction, which is a separate setting and a separate error context. Check both. - Changing the setting only for the current session.
SET idle_session_timeout = 0;affects only your session; it does not help other connections or future sessions. UseALTER SYSTEM,ALTER ROLE, orALTER DATABASEfor persistent changes. - Forgetting to reload.
ALTER SYSTEMwrites topostgresql.auto.confbut does not take effect until you runSELECT pg_reload_conf();or reload the server. - Assuming a restart is needed.
idle_session_timeoutis a user-context parameter and can be changed with a reload; a restart is unnecessary and disruptive. - Ignoring connection pool settings. A pool may hold idle connections longer than the server allows. Set the pool's maximum idle time below the server timeout, or enable keepalive.
- Overlooking network idle timeouts. Firewalls, load balancers, and NAT devices can drop idle TCP connections even when PostgreSQL would keep them open. If the error appears at a suspiciously round interval, check those devices too.
Tips
- Set
idle_session_timeoutto a value that matches your workload: long enough for interactive users, short enough to free resources from abandoned connections. - Use
ALTER ROLE ... SETfor application roles that need long-lived connections, and keep the global default stricter. - Monitor sessions with
SELECT pid, state, query_start, state_change FROM pg_stat_activity WHERE state = 'idle';to see how long connections sit idle. - If you use a connection pooler such as PgBouncer, its own
server_idle_timeoutandclient_idle_timeoutsettings can interact with PostgreSQL timeouts; review both layers. - Document the change and the reason in your runbook so future maintainers do not re-enable a timeout that breaks long-running jobs.
Troubleshooting
If the error persists after changing the setting, work through these checks in order:
- Verify the setting is active for the session that fails. Run
SHOW idle_session_timeout;in a new connection using the same role and database. If it still shows the old value, the change did not apply to that role or database, or the server was not reloaded. - Check for overriding settings. Query
pg_settingsas shown above; thesourcecolumn tells you whether the value comes from a configuration file, environment variable, role, database, or session. Fix the highest-priority source. - Look for other timeout errors. If the message mentions
idle-in-transaction timeout, adjustidle_in_transaction_session_timeoutinstead. If it mentionsstatement timeout, that isstatement_timeout. - Test with a raw connection. Connect with
psqland leave it idle for longer than the timeout. Ifpsqlstays connected but your application drops, the problem is in the application's pool or driver, not the server. - Check network devices. If the connection drops at a fixed interval regardless of PostgreSQL settings, inspect firewall, load balancer, and NAT idle timeouts. Increase them or enable TCP keepalive.
- Consult the server log. The PostgreSQL log records the termination reason and the setting in effect. Look for lines containing
idle-session timeoutand the PID to correlate with your client.
Frequently Asked Questions
What does SQLSTATE 57P05 mean exactly?
It is the error code for idle_session_timeout: the server closed the connection because it was idle for longer than the configured limit. It is not a data corruption or permission error.
Can I disable idle_session_timeout completely?
Yes, set it to 0 to disable it. Some managed providers may not allow 0; in that case use the largest value they permit.
Does changing idle_session_timeout require a server restart?
No. It is a user-context parameter and takes effect after a configuration reload (SELECT pg_reload_conf();). A restart is not needed.
Why does my connection still drop after I set it to 0?
Another timeout or a network device is likely responsible. Check idle_in_transaction_session_timeout, your connection pool's idle settings, and firewall or load balancer idle timeouts.
Can I set a different timeout for one application?
Yes. Use ALTER ROLE app_role SET idle_session_timeout = '30min'; so only that role gets the longer timeout, while other roles keep the global default.
Is 57P05 the same as a statement timeout?
No. Statement timeout uses a different error code and applies to running queries, not idle sessions. The messages and fixes are different.