Skip to content

Psycopg2SqlClient: accept extra options #2755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dlt/destinations/impl/postgres/sql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from contextlib import contextmanager
from typing import Any, AnyStr, ClassVar, Iterator, Optional, Sequence
from urllib.parse import unquote

from dlt.destinations.exceptions import (
DatabaseTerminalException,
Expand Down Expand Up @@ -45,9 +46,10 @@ def __init__(
self.credentials = credentials

def open_connection(self) -> "psycopg2.connection":
query_options = unquote(self.credentials.get_query().get("options", ""))
self._conn = psycopg2.connect(
dsn=self.credentials.to_native_representation(),
options=f"-c search_path={self.fully_qualified_dataset_name()},public",
options=f"{query_options} -csearch_path={self.fully_qualified_dataset_name()},public",
)
# we'll provide explicit transactions see _reset
self._reset_connection()
Expand Down
3 changes: 2 additions & 1 deletion docs/website/docs/dlt-ecosystem/destinations/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ password = "<password>" # replace with your password
host = "localhost" # or the IP address location of your database
port = 5432
connect_timeout = 15
query.options = "-ctimezone=Europe/Paris"
```

You can also pass a database connection string similar to the one used by the `psycopg2` library or [SQLAlchemy](https://docs.sqlalchemy.org/en/20/core/engines.html#postgresql). The credentials above will look like this:
```toml
# Keep it at the top of your TOML file, before any section starts
destination.postgres.credentials="postgresql://loader:<password>@localhost/dlt_data?connect_timeout=15"
destination.postgres.credentials="postgresql://loader:<password>@localhost/dlt_data?connect_timeout=15&options=-ctimezone%3dEurope/Paris"
```

To pass credentials directly, use the [explicit instance of the destination](../../general-usage/destination.md#pass-explicit-credentials)
Expand Down
3 changes: 2 additions & 1 deletion tests/load/postgres/test_postgres_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ def test_postgres_credentials_native_value(environment) -> None:

def test_postgres_query_params() -> None:
# test postgres timeout
dsn = "postgres://loader:pass@localhost:5432/dlt_data?client_encoding=utf-8&connect_timeout=600"
dsn = "postgres://loader:pass@localhost:5432/dlt_data?client_encoding=utf-8&connect_timeout=600&options=-ctimezone%3dEurope/Paris"
csc = PostgresCredentials()
csc.parse_native_representation(dsn)
assert csc.connect_timeout == 600
assert csc.client_encoding == "utf-8"
assert csc.get_query().get("options") == "-ctimezone%3dEurope/Paris"
assert csc.to_native_representation() == dsn


Expand Down