Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): map "server closed the connection unexpectedly" to a friendly validation error (#63729)

Tom Owers8ae676c6f794
diff --git a/posthog/temporal/data_imports/sources/postgres/source.py b/posthog/temporal/data_imports/sources/postgres/source.py
index 1952b8d0fcb5..7ce7e882207e 100644
--- a/posthog/temporal/data_imports/sources/postgres/source.py
+++ b/posthog/temporal/data_imports/sources/postgres/source.py
@@ -62,6 +62,15 @@ PostgresErrors = {
"SSL/TLS connection is required": "SSL/TLS connection is required but your database does not support it. Please enable SSL/TLS on your PostgreSQL server.",
"server does not support SSL, but SSL was required": "SSL/TLS connection is required but your database does not support it. Please enable SSL/TLS on your PostgreSQL server.",
"SSL connection has been closed unexpectedly": "The SSL/TLS connection to your database was closed unexpectedly. Check your database's SSL configuration and that the port is correct.",
+ # libpq reports a server-side socket close during the startup handshake with this wording. During
+ # credential validation it almost always means the host/port points at something that isn't (or
+ # won't accept) a Postgres connection — a wrong port, a service that requires SSL/TLS, or a
+ # pooler/firewall/SSH tunnel that drops the connection. Map it to an actionable message so
+ # validation stops surfacing this expected user/upstream condition as captured error noise.
+ # NB: this is intentionally NOT added to `get_non_retryable_errors` — the same wording is a
+ # transient mid-stream drop in the streaming path (`_CONNECTION_DROPPED_ERROR_SUBSTRINGS`) and
+ # must stay retryable there.
+ "server closed the connection unexpectedly": "Your database closed the connection unexpectedly while connecting. This usually means the host or port is wrong, the server requires SSL/TLS, or a connection pooler, firewall, or SSH tunnel dropped the connection. Check your host, port, and SSL settings.",
}
diff --git a/posthog/temporal/data_imports/sources/postgres/test_postgres.py b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
index 4b6bee3f7256..6bb005ecf7a0 100644
--- a/posthog/temporal/data_imports/sources/postgres/test_postgres.py
+++ b/posthog/temporal/data_imports/sources/postgres/test_postgres.py
@@ -907,6 +907,14 @@ class TestValidateCredentialsErrorMapping:
"The SSL/TLS connection to your database was closed unexpectedly. "
"Check your database's SSL configuration and that the port is correct.",
),
+ (
+ 'connection failed: connection to server at "127.0.0.1", port 43185 failed: '
+ "server closed the connection unexpectedly\n\tThis probably means the server terminated abnormally\n"
+ "\tbefore or while processing the request.",
+ "Your database closed the connection unexpectedly while connecting. This usually means the host "
+ "or port is wrong, the server requires SSL/TLS, or a connection pooler, firewall, or SSH tunnel "
+ "dropped the connection. Check your host, port, and SSL settings.",
+ ),
# Unmapped errors fall back to the generic message.
(
"some brand new failure",