Skip to main content

SQLModel ORM integration for Google Cloud SQL PostgreSQL with multi-database support

Project description

sqlmodel-gcp-postgres

PyPI Python 3.12+ License: MIT

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


Download files

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

Source Distribution

sqlmodel_gcp_postgres-1.0.1.tar.gz (73.6 kB view details)

Uploaded Source

Built Distribution

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

sqlmodel_gcp_postgres-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlmodel_gcp_postgres-1.0.1.tar.gz.

File metadata

File hashes

Hashes for sqlmodel_gcp_postgres-1.0.1.tar.gz
Algorithm Hash digest
SHA256 7873ae4b1071ce16aa5478fe02d0c8d468ac535ea02b44302b9f34254a99ebf1
MD5 19234dca8dca9f50ce99703e57dd8c62
BLAKE2b-256 f2c89088b701ca745232b15c3f106e95966bab58774769119dc65446ba98d044

See more details on using hashes here.

File details

Details for the file sqlmodel_gcp_postgres-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlmodel_gcp_postgres-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 43d3b3377942dd5a18cbde3d349c83963adfdfe8711c02c9796efdef21314723
MD5 66e9e8c80806c50f9d5de28028fba147
BLAKE2b-256 c986ca82343905c7f234db0cf54c8b28019ac16ccfe563b3ab48d71761ba307e

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