SQLAlchemy engine utilities for PostgreSQL and Google Cloud SQL
Project description
Google Cloud SQL PostgreSQL SQLAlchemy
A Python library that provides utilities for creating SQLAlchemy engines that work with PostgreSQL databases, both locally and on Google Cloud SQL.
Features
- Simple PostgreSQL connections: Create SQLAlchemy engines for local PostgreSQL databases
- Google Cloud SQL support: Seamlessly connect to Cloud SQL PostgreSQL instances using the Cloud SQL Python Connector
- Cloud SQL Proxy utilities: Cross-platform utilities for managing Cloud SQL Proxy lifecycle
- Automatic routing: Intelligently choose between local and Cloud SQL connections based on configuration
- Type-safe: Full type hints support for better IDE integration and code quality
Installation
pip install google-cloud-sql-postgres-sqlalchemy
Usage
Local PostgreSQL Connection
from google_cloud_sql_postgres_sqlalchemy import create_postgres_engine
engine = create_postgres_engine(
username="myuser",
password="mypassword",
host="localhost",
database="mydatabase",
)
Creating SQLAlchemy URL Strings
If you need to generate a SQLAlchemy connection URL string (e.g., for configuration files or frameworks):
from google_cloud_sql_postgres_sqlalchemy import create_sqlalchemy_url
# Without port
url = create_sqlalchemy_url(
username="myuser",
password="mypassword",
host="localhost",
database="mydatabase",
)
# Returns: 'postgresql+pg8000://myuser:mypassword@localhost/mydatabase'
# With port
url = create_sqlalchemy_url(
username="myuser",
password="mypassword",
host="localhost",
database="mydatabase",
port=5432,
)
# Returns: 'postgresql+pg8000://myuser:mypassword@localhost:5432/mydatabase'
Connection Pool Configuration
All engine creation functions support connection pool configuration:
engine = create_postgres_engine(
username="myuser",
password="mypassword",
host="localhost",
database="mydatabase",
pool_size=20, # Number of connections to maintain (default: 20)
max_overflow=10, # Additional connections beyond pool_size (default: 10)
pool_timeout=30, # Seconds to wait for connection (default: 30)
pool_recycle=3600, # Recycle connections after N seconds (default: 3600)
)
Pool Parameters:
pool_size: Number of connections to keep open. Default is 20 for production use.max_overflow: Maximum number of connections that can be created beyondpool_size. Total connections = pool_size + max_overflow.pool_timeout: Seconds to wait before giving up on getting a connection from the pool.pool_recycle: Number of seconds after which a connection is automatically recycled. Helps prevent stale connections.
Google Cloud SQL Connection
from google_cloud_sql_postgres_sqlalchemy import create_postgres_engine_in_cloud_sql
engine = create_postgres_engine_in_cloud_sql(
username="myuser",
password="mypassword",
host="project:region:instance", # Cloud SQL instance connection name
database="mydatabase",
)
Automatic Connection Selection
from google_cloud_sql_postgres_sqlalchemy import create_database_engine
# Automatically chooses Cloud SQL if google_cloud_project_id is provided
engine = create_database_engine(
username="myuser",
password="mypassword",
host="localhost", # or Cloud SQL instance connection name
database="mydatabase",
google_cloud_project_id=None, # Set to project ID to use Cloud SQL
)
Configuring Logging
The library uses Python's standard logging module. To see Cloud SQL Proxy startup messages:
import logging
# Configure logging to see INFO messages from the library
logging.basicConfig(level=logging.INFO)
# Or configure only for this library
logging.getLogger('google_cloud_sql_postgres_sqlalchemy').setLevel(logging.INFO)
Using Cloud SQL Proxy (Alternative Method)
For development or testing, you can use the Cloud SQL Proxy utilities to connect to Cloud SQL through a local proxy:
from google_cloud_sql_postgres_sqlalchemy import (
cloud_sql_proxy_running,
create_postgres_engine,
)
# Start Cloud SQL Proxy and connect through it
with cloud_sql_proxy_running(
instance_connection_name="project:region:instance",
port=5432,
):
# While the proxy is running, connect as if it's a local database
engine = create_postgres_engine(
username="myuser",
password="mypassword",
host="localhost",
database="mydatabase",
)
# Use the engine...
with engine.connect() as conn:
result = conn.execute("SELECT 1")
Cross-platform proxy detection: The cloud_sql_proxy_running context manager automatically detects the Cloud SQL Proxy binary path on macOS (Intel/Apple Silicon), Linux, and Windows. You can also provide an explicit path:
from google_cloud_sql_postgres_sqlalchemy import get_cloud_sql_proxy_path
# Auto-detect proxy path
proxy_path = get_cloud_sql_proxy_path()
print(f"Using proxy at: {proxy_path}")
# Or specify a custom path
with cloud_sql_proxy_running(
instance_connection_name="project:region:instance",
port=5432,
cloud_sql_proxy_path="/custom/path/to/cloud-sql-proxy",
):
# ...
Requirements
- Python 3.10+
- SQLAlchemy 2.0+
- pg8000 1.30+
- cloud-sql-python-connector 1.0+
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Authors
- Javad
Changelog
1.0.0 (2026-01-02)
- Support for local PostgreSQL connections
- Support for Google Cloud SQL connections using Cloud SQL Python Connector
- Automatic connection routing based on configuration
- Cross-platform Cloud SQL Proxy utilities
cloud_sql_proxy_running()context manager for managing proxy lifecycleget_cloud_sql_proxy_path()for automatic proxy detection across macOS, Linux, and Windowscreate_sqlalchemy_url()utility for generating connection URL strings- Validation for Cloud SQL instance connection names
- Connection pool configuration (pool_size, max_overflow, pool_timeout, pool_recycle)
- Structured logging with Python's logging module
- Comprehensive unit tests achieving 100% code coverage
- Full type hints support
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 google_cloud_sql_postgres_sqlalchemy-1.0.0.tar.gz.
File metadata
- Download URL: google_cloud_sql_postgres_sqlalchemy-1.0.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
679f468c49a0f4bd3a907209346c3c85cbfd1f2b51a14f25effe12a9c71d5064
|
|
| MD5 |
ef58b6eacdf4f9e30a3ac0c093dfa3c9
|
|
| BLAKE2b-256 |
0383ad38fad5ce82ea47ec54e701711207e0850f46d7b0e4676b40566312bf4b
|
File details
Details for the file google_cloud_sql_postgres_sqlalchemy-1.0.0-py3-none-any.whl.
File metadata
- Download URL: google_cloud_sql_postgres_sqlalchemy-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a62a3564356e580a23370df973e6fe495c7bf89c7282621fb1824202b98e460b
|
|
| MD5 |
4db8f471dd11fe5558923275b6619d44
|
|
| BLAKE2b-256 |
2c15e627bd75e47a726c8032ee4c283f4ab72eee95990c4abf79adef85594589
|