Skip to content

Commit e1c01d5

Browse files
authored
Merge pull request #214 from Maxteabag/postgres-default-localhost
Default empty Postgres host to localhost when a port is set
2 parents fc63200 + c316830 commit e1c01d5

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

sqlit/domains/connections/providers/postgresql/adapter.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ def connect(self, config: ConnectionConfig) -> Any:
5252
"connect_timeout": 10,
5353
"database": endpoint.database or "postgres",
5454
}
55-
if endpoint.host:
56-
connect_args["host"] = endpoint.host
55+
host = endpoint.host
56+
# If the user only set a port (e.g. Postgres on a non-default port
57+
# like 5433), default the host to "localhost" — matches the Server
58+
# field's placeholder. Without this, libpq drops both args and
59+
# falls back to the default unix socket, ignoring the port.
60+
if not host and endpoint.port:
61+
host = "localhost"
62+
if host:
63+
connect_args["host"] = host
5764
connect_args["port"] = int(endpoint.port or get_default_port("postgresql"))
5865
if endpoint.username:
5966
connect_args["user"] = endpoint.username

tests/unit/test_postgresql_adapter.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,45 @@ def test_postgresql_peer_auth_omits_empty_tcp_args() -> None:
3232
assert "port" not in kwargs
3333
assert "user" not in kwargs
3434
assert "password" not in kwargs
35+
36+
37+
def test_postgresql_uses_custom_port_when_server_left_blank() -> None:
38+
"""Regression test for issue #205.
39+
40+
A user pointed sqlit at a docker Postgres on 127.0.0.1:5433. They left
41+
the Server field blank (its placeholder says 'localhost') and set the
42+
Port field to 5433. libpq then reported it couldn't reach
43+
/run/postgresql/.s.PGSQL.5432 — i.e. neither host nor port made it
44+
through.
45+
46+
Expected: with a port set, the adapter must pass both port=5433 and
47+
a sensible host (localhost, matching the form placeholder) to
48+
psycopg2.connect so the connection actually reaches the docker
49+
container.
50+
"""
51+
mock_psycopg2 = MagicMock()
52+
with patch.dict("sys.modules", {"psycopg2": mock_psycopg2}):
53+
from sqlit.domains.connections.providers.postgresql.adapter import PostgreSQLAdapter
54+
55+
adapter = PostgreSQLAdapter()
56+
config = ConnectionConfig(
57+
name="timescale",
58+
db_type="postgresql",
59+
server="", # blank — UI placeholder says "localhost"
60+
port="5433", # user explicitly chose 5433
61+
database="postgres",
62+
username="postgres",
63+
password="password",
64+
)
65+
66+
adapter.connect(config)
67+
68+
kwargs = mock_psycopg2.connect.call_args.kwargs
69+
assert kwargs.get("port") == 5433, (
70+
"port=5433 must reach psycopg2.connect, but the adapter "
71+
f"silently drops it when server is blank. kwargs={kwargs!r}"
72+
)
73+
assert kwargs.get("host") == "localhost", (
74+
"blank server must default to 'localhost' (matching the "
75+
f"connection form placeholder). kwargs={kwargs!r}"
76+
)

0 commit comments

Comments
 (0)