Skip to content

Commit

Permalink
feat(postgres): use port in connection string
Browse files Browse the repository at this point in the history
Before this was just ignored, so if you tryied to connect to
anything besides port 5432, it would
just use the default of 5432
  • Loading branch information
NickCrews authored and cpcloud committed Apr 6, 2024
1 parent 2b8d52b commit d561c01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion ibis/backends/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def _from_url(self, url: str, **kwargs):
A backend instance
"""

url = urlparse(url)
database, *schema = url.path[1:].split("/", 1)
query_params = parse_qs(url.query)
Expand All @@ -70,6 +69,7 @@ def _from_url(self, url: str, **kwargs):
"host": url.hostname,
"database": database or "",
"schema": schema[0] if schema else "",
"port": url.port,
}

for name, value in query_params.items():
Expand Down Expand Up @@ -98,6 +98,9 @@ def _from_url(self, url: str, **kwargs):
if "password" in kwargs and kwargs["password"] is None:
del kwargs["password"]

if "port" in kwargs and kwargs["port"] is None:
del kwargs["port"]

return self.connect(**kwargs)

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
Expand Down
9 changes: 8 additions & 1 deletion ibis/backends/postgres/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
import ibis.expr.types as ir
from ibis.backends.tests.errors import PsycoPg2OperationalError

pytest.importorskip("psycopg2")

Expand Down Expand Up @@ -247,6 +248,12 @@ def test_timezone_from_column(contz, snapshot):

def test_kwargs_passthrough_in_connect():
con = ibis.connect(
"postgresql://postgres:postgres@localhost/ibis_testing?sslmode=allow"
"postgresql://postgres:postgres@localhost:5432/ibis_testing?sslmode=allow"
)
assert con.current_catalog == "ibis_testing"


def test_port():
# check that we parse and use the port (and then of course fail cuz it's bogus)
with pytest.raises(PsycoPg2OperationalError):
ibis.connect("postgresql://postgres:postgres@localhost:1337/ibis_testing")

0 comments on commit d561c01

Please sign in to comment.