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 usernameDB_PASSWORD: Database passwordDB_HOST: Database hostDB_PORT: Database port
SSH Tunnel Configuration
USE_SSH_TUNNEL: Enable/disable SSH tunneling ('true'/'false')SSH_HOST: SSH tunnel hostSSH_USERNAME: SSH usernameSSH_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80bd81be6a19e9ca9e7487759da8b667418494355f645bf5e109c591b9956bf1
|
|
| MD5 |
2fa21c3c40f35c586c840189e48fd9c1
|
|
| BLAKE2b-256 |
723d365c46a83aeb4c2ccc3b54dddff5b2f814dc3261ae7c9e07e562a55d6847
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df7cfdd760316d30d667cdfd2bfe3c9de5e881a69bba86740704e1d77fedf125
|
|
| MD5 |
cdd24b48645fb22bcdd8aae852f074c6
|
|
| BLAKE2b-256 |
3b268a3bee616b9c4c067433b43923c5fc5df9fb44a3c54baba16fb113f289dd
|