diff --git a/ibis/backends/postgres/__init__.py b/ibis/backends/postgres/__init__.py index 7a0ddcfd28bd..b3631cbffde7 100644 --- a/ibis/backends/postgres/__init__.py +++ b/ibis/backends/postgres/__init__.py @@ -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) @@ -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(): @@ -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: diff --git a/ibis/backends/postgres/tests/test_client.py b/ibis/backends/postgres/tests/test_client.py index bb2195bcbb8c..82aedbd34ba2 100644 --- a/ibis/backends/postgres/tests/test_client.py +++ b/ibis/backends/postgres/tests/test_client.py @@ -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") @@ -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")