SQLModel ORM integration for Google Cloud SQL PostgreSQL with multi-database support
Project description
sqlmodel-gcp-postgres
SQLModel ORM integration for Google Cloud SQL PostgreSQL instances with seamless support for local development and testing.
Features
- Cloud SQL Integration: Uses the official Google Cloud SQL Python Connector for secure, simplified connections
- Multi-Database Support: Works with PostgreSQL, SQLite, and other databases for local development and testing
- Transaction Management: Built-in decorator for automatic commit/rollback handling
- Lazy Connection Refresh: Compatible with Cloud Run and other auto-scaling environments
Installation
# pip
pip install sqlmodel-gcp-postgres
# uv
uv add sqlmodel-gcp-postgres
# Poetry
poetry add sqlmodel-gcp-postgres
Note: This library requires the pg8000 database driver and is designed for use with PostgreSQL and Cloud SQL. It is included as a dependency in the cloud-sql-python-connector[pg8000] requirement.
Quick Start
Cloud SQL (Production)
Set environment variables:
USE_CLOUD_SQL=true
CLOUD_SQL_INSTANCE_CONNECTION_NAME=your-project:region:instance-name
DB_USER=postgres-user
DB_PASS=postgres-password
DB_NAME=database-name
Optional: Use private IP:
CLOUD_SQL_PRIVATE_IP=true
Local Development with PostgreSQL (Docker Compose)
services:
db:
image: postgres:17-alpine
environment:
POSTGRES_USER: dev_user
POSTGRES_PASSWORD: dev_password
POSTGRES_DB: dev_db
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dev_user"]
interval: 10s
timeout: 5s
retries: 5
app:
build: .
depends_on:
db:
condition: service_healthy
environment:
DB_URL: postgresql+pg8000://dev_user:dev_password@db:5432/dev_db
Testing with SQLite
import os
os.environ["DB_URL"] = "sqlite:///test.db"
Usage
from sqlmodel import Session, select
from sqlmodel_gcp_postgres import get_session, transactional
# Use in dependency injection (e.g., FastAPI)
@app.post("/api/users")
@transactional
async def create_user(user: User, session: Session = Depends(get_session)):
session.add(user)
return user # Auto-commits on return
# Or with context manager
def query_users():
session = get_session()
users = session.exec(select(User)).all()
return users
Configuration
The library automatically detects the database mode based on USE_CLOUD_SQL:
| Mode | Environment Variable | Required Vars |
|---|---|---|
| Cloud SQL | USE_CLOUD_SQL=true |
CLOUD_SQL_INSTANCE_CONNECTION_NAME, DB_USER, DB_PASS, DB_NAME |
| Standard | USE_CLOUD_SQL=false or unset |
DB_URL |
License
MIT License - see LICENSE file for details.
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 sqlmodel_gcp_postgres-1.0.1.tar.gz.
File metadata
- Download URL: sqlmodel_gcp_postgres-1.0.1.tar.gz
- Upload date:
- Size: 73.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7873ae4b1071ce16aa5478fe02d0c8d468ac535ea02b44302b9f34254a99ebf1
|
|
| MD5 |
19234dca8dca9f50ce99703e57dd8c62
|
|
| BLAKE2b-256 |
f2c89088b701ca745232b15c3f106e95966bab58774769119dc65446ba98d044
|
File details
Details for the file sqlmodel_gcp_postgres-1.0.1-py3-none-any.whl.
File metadata
- Download URL: sqlmodel_gcp_postgres-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43d3b3377942dd5a18cbde3d349c83963adfdfe8711c02c9796efdef21314723
|
|
| MD5 |
66e9e8c80806c50f9d5de28028fba147
|
|
| BLAKE2b-256 |
c986ca82343905c7f234db0cf54c8b28019ac16ccfe563b3ab48d71761ba307e
|