Skip to main content

A package for managing local and remote database connections with optional SSH tunneling.

Project description

DBTunnelBridge

DBTunnelBridge is a Python package that simplifies database connections with optional SSH tunneling support. It provides a clean, Pythonic interface for managing both local and remote database connections through SSH tunnels, with basic automatic resource management and environment variable integration.

Features

  • 🔌 Simple database connection management
  • 🔒 Secure SSH tunneling support
  • 🔑 Environment variable configuration
  • 🛠 SQLAlchemy integration
  • 📦 Context manager support for automatic resource cleanup
  • 🔄 Automatic port forwarding management

Installation

pip install dbtunnelbridge

Quick Start

Basic Local Connection

from dbtunnelbridge import create_db_connection
# Create a direct database connection
with create_db_connection(
        db_user="your_user",
        db_password="your_password",
        db_host="localhost",
        db_port=3306
        ) as db:
    session = db.get_session()
    # Your database operations here

Create a connection through SSH tunnel

from dbtunnelbridge import create_db_connection

with create_db_connection(
    db_user="your_user",
    db_password="your_password",
    db_host="internal.database.host",
    db_port=3306,
    use_ssh_tunnel=True,
    ssh_host="bastion.example.com",
    ssh_username="ssh_user",
    ssh_pkey="/path/to/private/key"
) as db:
    session = db.get_session()
    # Your database operations here

Environment Variables Support

DBTunnelBridge supports configuration through environment variables:

Database Configuration

  • DB_USER: Database username
  • DB_PASSWORD: Database password
  • DB_HOST: Database host
  • DB_PORT: Database port

SSH Tunnel Configuration

  • USE_SSH_TUNNEL: Enable/disable SSH tunneling ('true'/'false')
  • SSH_HOST: SSH tunnel host
  • SSH_USERNAME: SSH username
  • SSH_PKEY: Path to SSH private key

Advanced Usage

Manual Resource Management

db = create_db_connection(
    db_user="your_user",
    db_password="your_password",
    db_host="localhost",
    db_port=3306
)
try:
    db.create_engine()
    session = db.get_session()
    # Your database operations here
finally:
    db.close_tunnel_if_needed()

Requirements

  • Python 3.10+
  • SQLAlchemy
  • PyMySQL
  • sshtunnel
  • paramiko

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbtunnelbridge-0.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbtunnelbridge-0.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file dbtunnelbridge-0.0.1.tar.gz.

File metadata

  • Download URL: dbtunnelbridge-0.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for dbtunnelbridge-0.0.1.tar.gz
Algorithm Hash digest
SHA256 80bd81be6a19e9ca9e7487759da8b667418494355f645bf5e109c591b9956bf1
MD5 2fa21c3c40f35c586c840189e48fd9c1
BLAKE2b-256 723d365c46a83aeb4c2ccc3b54dddff5b2f814dc3261ae7c9e07e562a55d6847

See more details on using hashes here.

File details

Details for the file dbtunnelbridge-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: dbtunnelbridge-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for dbtunnelbridge-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df7cfdd760316d30d667cdfd2bfe3c9de5e881a69bba86740704e1d77fedf125
MD5 cdd24b48645fb22bcdd8aae852f074c6
BLAKE2b-256 3b268a3bee616b9c4c067433b43923c5fc5df9fb44a3c54baba16fb113f289dd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page