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.2.tar.gz (73.7 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.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlmodel_gcp_postgres-1.0.2.tar.gz
  • Upload date:
  • Size: 73.7 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for sqlmodel_gcp_postgres-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8231ae39be758924bfcb8f6222be19ee13d0eff67d131b5c02ff8460563ba581
MD5 9b0b79be50289c12cd1335d56929d261
BLAKE2b-256 3958dee329a72640a7f72fd43e1f5f2a6e3d5ceb5c8563a7fd85555261d206e6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for sqlmodel_gcp_postgres-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 45d4189b4e62067104eadb0a7bcf1a6470ab6a620f9840293cecab572293a34b
MD5 87799e253fce000961a6cccf0288adee
BLAKE2b-256 97a57bb68183379b871373e590e2b82a4ff71ebd4a00e2ff14a7af7c156f972c

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