How to Set Redshift Connection Timeout in PostgreSQL

Galaxy Glossary

How do I set a connection timeout in Amazon Redshift?

Sets a maximum time to wait when establishing a connection to an Amazon Redshift cluster.

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

Description

Why does my Redshift connection hang?

Network latency, DNS hiccups, or overloaded clusters can leave your SQL client waiting indefinitely. Adding a timeout parameter forces the client to abort after a user-defined number of seconds, returning control to your application quickly.

How do I specify connect_timeout in the connection string?

Add connect_timeout=<seconds> to any libpq-compatible string. The timer starts when the TCP handshake begins and stops when authentication succeeds.

psql example

psql "host=redshift-cluster.abc.us-east-1.redshift.amazonaws.com port=5439 dbname=dev user=awsuser connect_timeout=10"

JDBC example

jdbc:postgresql://redshift-cluster.abc.us-east-1.redshift.amazonaws.com:5439/dev?connectTimeout=10

How can I limit query runtime after connecting?

Use SET statement_timeout. Unlike connect_timeout, this parameter controls how long any single statement may run before being canceled by the server.

SET command

SET statement_timeout TO '15s';

When should I change timeouts in ETL pipelines?

Short timeouts (5-15 s) keep orchestration tools from stalling when clusters reboot. Longer timeouts (30-60 s) suit interactive BI sessions, allowing transient network spikes to clear.

What happens when the timeout is reached?

The client raises an error such as could not connect to server: Operation timed out. Handling this exception lets your code retry, alert, or fail gracefully.

Why How to Set Redshift Connection Timeout in PostgreSQL is important

How to Set Redshift Connection Timeout in PostgreSQL Example Usage


-- Abort connection attempts after 8 seconds
psql "host=redshift-prod.us-east-1.redshift.amazonaws.com port=5439 dbname=shop user=bi_analyst connect_timeout=8"

-- Inside the session limit long aggregations to 25 s
SET statement_timeout TO '25s';

-- Query 30-day sales totals
SELECT p.name, SUM(oi.quantity) AS units_sold
FROM Orders o
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
WHERE o.order_date >= current_date - interval '30 days'
GROUP BY p.name;

How to Set Redshift Connection Timeout in PostgreSQL Syntax


-- libpq / psql
psql "host=<cluster-endpoint> port=5439 dbname=dev user=awsuser password=<pwd> connect_timeout=<seconds>"

-- JDBC
jdbc:postgresql://<cluster-endpoint>:5439/dev?user=awsuser&password=<pwd>&connectTimeout=<seconds>

-- psql environment variable
PGCONNECT_TIMEOUT=<seconds> psql -h <cluster-endpoint> -p 5439 -d dev -U awsuser

-- Limit query duration after connecting
SET statement_timeout TO '<interval>';  -- e.g., '15s', '2min'

-- Ecommerce example
SET statement_timeout TO '20s';
SELECT customer_id, SUM(total_amount) AS lifetime_value
FROM Orders
GROUP BY customer_id;

Common Mistakes

Frequently Asked Questions (FAQs)

Does connect_timeout work on the server side?

No. The timer runs entirely in the client library (psql, JDBC, psycopg2, etc.). Redshift itself is unaware of the parameter.

Can I change connect_timeout mid-session?

No. You must disconnect and reconnect with a new parameter or environment variable to modify the connection timeout.

What is the default timeout value?

libpq defaults to no timeout (wait forever). Some drivers override this to 10 s, so always set it explicitly for predictable behavior.

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