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(supabase): name both fields when the pooler username lands in the host (#100800)

Tom Owers87647c9fe914
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/source.py
index ca9429b5823c..931fd7abae51 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/source.py
@@ -49,6 +49,18 @@ _SUPABASE_DIRECT_HOST_RE = re.compile(r"^db\.[a-z0-9]+\.supabase\.co$", re.IGNOR
# just yields an opaque DNS failure, so detect it and point users at the actual database host.
_SUPABASE_PROJECT_HOST_RE = re.compile(r"^(?P<ref>[a-z0-9]+)\.supabase\.co$", re.IGNORECASE)
+# The pooler username is `postgres.<project-ref>`, which reads like a host name — our own pooler
+# guidance spells it out, and it lands in the host field often enough to be worth naming. A project
+# ref is 20 characters, and a bare `postgres.<label>` that long is not a resolvable host either
+# way, so matching it can't reject a host that would have worked.
+_SUPABASE_POOLER_USERNAME_HOST_RE = re.compile(r"^postgres\.[a-z0-9]{16,}$", re.IGNORECASE)
+
+_SUPABASE_POOLER_USERNAME_AS_HOST_ERROR = (
+ "The host looks like your Supabase pooler username, not a database host. Enter the Session "
+ "pooler host (aws-0-<region>.pooler.supabase.com) as the host, and postgres.<project-ref> as "
+ "the user."
+)
+
def _strip_host_scheme(host: str) -> str:
"""Reduce a pasted value to a bare host: drop any URL scheme, path, and surrounding whitespace."""
@@ -197,6 +209,9 @@ class SupabaseSource(PostgresSource):
require_ssl: bool = False,
) -> tuple[bool, str | None]:
bare_host = _strip_host_scheme(config.host or "")
+ if _SUPABASE_POOLER_USERNAME_HOST_RE.match(bare_host):
+ return False, _SUPABASE_POOLER_USERNAME_AS_HOST_ERROR
+
project_host = _SUPABASE_PROJECT_HOST_RE.match(bare_host)
if project_host:
# Project refs are lowercase, and the pooler username (postgres.<ref>) is
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/test_supabase_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/test_supabase_source.py
index 581c34d019d6..533a25f8a84b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/test_supabase_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/supabase/test_supabase_source.py
@@ -113,6 +113,31 @@ def test_project_url_host_is_rejected_before_connecting(host):
assert "postgres.abcdefgh" in error
+ "host",
+ [
+ "postgres.abcdefghijklmnop",
+ "POSTGRES.ABCDEFGHIJKLMNOP",
+ " postgres.abcdefghijklmnop ",
+ "postgres://postgres.abcdefghijklmnop",
+ ],
+)
+def test_pooler_username_as_host_is_rejected_before_connecting(host):
+ # The pooler username (`postgres.<project-ref>`) reads like a host name, so it lands in the
+ # host field; it can never resolve, so short-circuit to guidance that names both fields
+ # instead of attempting a doomed connection that yields an opaque DNS error.
+ config = mock.MagicMock(host=host)
+
+ with mock.patch.object(PostgresSource, "validate_credentials") as super_validate:
+ success, error = SupabaseSource().validate_credentials(config, team_id=1)
+
+ super_validate.assert_not_called()
+ assert success is False
+ assert error is not None
+ assert "pooler username" in error
+ assert "pooler.supabase.com" in error
+
+
@pytest.mark.parametrize(
"host",
[
@@ -137,6 +162,10 @@ def test_successful_connection_delegates_to_postgres(host):
[
"aws-0-us-east-1.pooler.supabase.com",
"my-db.internal",
+ # A resolvable host whose first label is `postgres` must not be read as the pooler
+ # username, so the username check only claims a single long trailing label.
+ "postgres.example.com",
+ "postgres.internal",
],
)
def test_non_direct_host_failure_uses_postgres_error(host):