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.
Metadata
Release files for sqlmodel-gcp-postgres 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sqlmodel_gcp_postgres-1.0.2.tar.gz | 73.7 kB | Without provenance | Details for sqlmodel_gcp_postgres-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl |
Total release size: 79.6 kB
Release files / sqlmodel_gcp_postgres-1.0.2.tar.gz
| Download URL | sqlmodel_gcp_postgres-1.0.2.tar.gz |
|---|---|
| Size | 73.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8231ae39be758924bfcb8f6222be19ee13d0eff67d131b5c02ff8460563ba581
|
|
BLAKE2b-256 checksum How to use checksums |
3958dee329a72640a7f72fd43e1f5f2a6e3d5ceb5c8563a7fd85555261d206e6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl
| Download URL | sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
45d4189b4e62067104eadb0a7bcf1a6470ab6a620f9840293cecab572293a34b
|
|
BLAKE2b-256 checksum How to use checksums |
97a57bb68183379b871373e590e2b82a4ff71ebd4a00e2ff14a7af7c156f972c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for sqlmodel-gcp-postgres 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.