Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 8266962

Browse files
author
dbatten5
authored
Add support for Unix domain in connection urls (#423)
unix domain connection urls come in the following format: - "postgresql://user:password@/dbname?host=/var/lib/postgresql" or - "postgresql://user:password@/dbname?unix_sock=/var/lib/postgresql"
1 parent 89c9f3f commit 8266962

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

databases/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,11 @@ def password(self) -> typing.Optional[str]:
473473

474474
@property
475475
def hostname(self) -> typing.Optional[str]:
476-
return self.components.hostname
476+
return (
477+
self.components.hostname
478+
or self.options.get("host")
479+
or self.options.get("unix_sock")
480+
)
477481

478482
@property
479483
def port(self) -> typing.Optional[int]:

tests/test_database_url.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ def test_database_url_properties():
2929
assert u.port == 123
3030
assert u.database == "mydatabase"
3131

32+
u = DatabaseURL(
33+
"postgresql://username:password@/mydatabase?host=/var/run/postgresql/.s.PGSQL.5432"
34+
)
35+
assert u.dialect == "postgresql"
36+
assert u.username == "username"
37+
assert u.password == "password"
38+
assert u.hostname == "/var/run/postgresql/.s.PGSQL.5432"
39+
assert u.database == "mydatabase"
40+
41+
u = DatabaseURL(
42+
"postgresql://username:password@/mydatabase?unix_sock=/var/run/postgresql/.s.PGSQL.5432"
43+
)
44+
assert u.hostname == "/var/run/postgresql/.s.PGSQL.5432"
45+
3246

3347
def test_database_url_escape():
3448
u = DatabaseURL(f"postgresql://username:{quote('[password')}@localhost/mydatabase")

0 commit comments

Comments
 (0)