Skip to content
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

Add documentation for connecting to a database over ssh #2238

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from

Conversation

dat-a-man
Copy link
Collaborator

Description

This PR updates the SQL database configuration documentation to explain how to connect to a database using an SSH tunnel. Key additions include:

  • Detailed steps for setting up an SSH tunnel.
  • Examples of using ssh tunnel and SQLAlchemy for secure connections.
  • Using .dlt/secrets.toml for managing credentials.

These changes aim to help users securely access remote databases behind firewalls or requiring SSH.

@dat-a-man dat-a-man self-assigned this Jan 27, 2025
@dat-a-man dat-a-man added the documentation Improvements or additions to documentation label Jan 27, 2025
Copy link

netlify bot commented Jan 27, 2025

Deploy Preview for dlt-hub-docs ready!

Name Link
🔨 Latest commit c04284c
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/67ceaddbaa7b690007d86501
😎 Deploy Preview https://deploy-preview-2238--dlt-hub-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.


# Close the SSH tunnel to release resources
tunnel.close()
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be more pythonic:

    ssh_creds = dlt.secrets["ssh"]
    db_creds = dlt.secrets["destination.sqlalchemy.credentials"]

    with SSHTunnelForwarder(
        (ssh_creds["server_ip_address"], 22),
        ssh_username=ssh_creds["username"],
        ssh_pkey=ssh_creds["private_key_path"],
        ssh_private_key_password=ssh_creds.get("private_key_password"),
        remote_bind_address=("127.0.0.1", 5432),
    ) as tunnel:
        engine = create_engine(
            f"postgresql://{db_creds['username']}:{db_creds['password']}"
            f"@127.0.0.1:{tunnel.local_bind_port}/{db_creds['database']}"
        )

        # Access database table as a dlt resource
        table_resource = sql_table(engine, table="employees", schema="public")

        # Define and run the pipeline
        pipeline = dlt.pipeline(
            pipeline_name="remote_db_pipeline",
            destination="duckdb",
            dataset_name="remote_dataset"
        )

        print(pipeline.run(table_resource))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated as per the above code


#### Using SqlAlchemy Engine as credentials
### Using SqlAlchemy Engine as credentials

You are able to pass an instance of SqlAlchemy Engine instead of credentials:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You are able to pass an instance of SqlAlchemy Engine instead of credentials:
You are able to pass an instance of SQLAlchemy Engine instead of credentials:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@@ -160,9 +160,9 @@ source = sql_database(credentials).with_resources("family")
It is recommended to configure credentials in `.dlt/secrets.toml` and to not include any sensitive information in the pipeline code.
:::

### Other connection options
## Other connection options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Other connection options

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@dat-a-man dat-a-man force-pushed the docs/update-sql-documentation-connect-via-ssh branch from 8a46bac to 8d61c8a Compare February 24, 2025 13:54
@dat-a-man dat-a-man force-pushed the docs/update-sql-documentation-connect-via-ssh branch from bbdd9f7 to c04284c Compare March 10, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants