How to Set Connection Timeouts in PostgreSQL

Galaxy Glossary

How do I set and use connection timeout parameters in PostgreSQL?

Connection timeouts prevent long-running connection attempts or queries from hanging your PostgreSQL resources.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

Why do connection timeouts matter in PostgreSQL?

Timeouts free up server resources, avoid app hangs, and surface network issues quickly. They act as safety nets for slow connects, runaway queries, and forgotten sessions.

Which timeout settings can I use?

Client side: connect_timeout in the libpq connection string or PGCONNECT_TIMEOUT env var. Server side: statement_timeout, idle_in_transaction_session_timeout, and from v14+ idle_session_timeout.

How do I set connect_timeout?

Add the parameter to any libpq URL or keyword string. The value is in seconds and limits how long the client waits while opening a TCP socket and authenticating.

Example

postgresql://app_user:secret@db.example.com:5432/shop?connect_timeout=5

How do I limit query run time with statement_timeout?

Apply at session, user, or database level. When a single statement exceeds the limit, PostgreSQL cancels it automatically.

Example

SET statement_timeout TO '8s';

How do I clean up idle sessions?

Use idle_in_transaction_session_timeout or idle_session_timeout. These parameters terminate sessions that sit idle, preventing table locks and conserving connections.

Example

ALTER DATABASE shop SET idle_session_timeout = '30min';

Best practices for ecommerce workloads?

Short connect_timeout (2-5s) keeps APIs snappy. Moderate statement_timeout (5-15s) stops cart queries from monopolizing CPU. Use idle_session_timeout to auto-close abandoned dashboard tabs.

Where should I configure these timeouts?

Embed connect_timeout in application connection strings. Set server-side limits in postgresql.conf, ALTER ROLE, or per request with SET.

Why How to Set Connection Timeouts in PostgreSQL is important

How to Set Connection Timeouts in PostgreSQL Example Usage


-- Run a report but cancel if it exceeds 10 seconds
SET statement_timeout = '10s';
SELECT c.name,
       COUNT(o.id) AS orders_last_month,
       SUM(o.total_amount) AS revenue
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
WHERE o.order_date > CURRENT_DATE - INTERVAL '30 days'
GROUP BY c.name
ORDER BY revenue DESC;

How to Set Connection Timeouts in PostgreSQL Syntax


-- Client connection (5-second timeout)
postgresql://app_user:secret@db.example.com:5432/shop?connect_timeout=5

-- Server session limits
SET statement_timeout = '8s';                     -- cancels slow SELECT on Orders
SET idle_in_transaction_session_timeout = '1min'; -- ends forgotten BEGIN;
SET idle_session_timeout = '30min';               -- auto-closes idle tabs

-- Persistent settings
ALTER ROLE api_user SET statement_timeout = '10s';
ALTER DATABASE shop SET idle_session_timeout = '30min';

Common Mistakes

Frequently Asked Questions (FAQs)

Does connect_timeout cancel long SQL statements?

No. It only limits the initial connection attempt. Use statement_timeout for running queries.

Are timeouts enforced on prepared statements?

Yes. Once EXECUTE starts, statement_timeout applies exactly as it does to ad-hoc SQL.

Do idle timeouts roll back open transactions?

PostgreSQL ends the session, which implicitly rolls back any uncommitted work.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo