Skip to main content

A simple connector for GCP databases

Project description

Low Overhead GCP Database Connector.

Low-overhead connections to Google Cloud SQL databases, optionally supporting IAM Authentication.

Prerequisites

  • Google Cloud Project: A GCP project with Cloud SQL enabled.
  • Cloud SQL Instance: A PostgreSQL instance configured for IAM authentication.

IAM Authentication (Optional)

  • IAM User: A database user mapped to a Google Cloud IAM service account or user.
  • Authentication:
    • Locally: gcloud auth application-default login
    • Production: Service Account attached to the environment (VM, GKE, Cloud Run).

Poastgres

1. Psycopg3 Connection Pool

Example using psycopg (version 3) to connect to Cloud SQL. It can automatically resolve the instance IP and inject the IAM token.

Dependencies:

  • psycopg 3.3.0 or higher
  • google-auth
import psycopg
from psycopg_pool import ConnectionPool
from simple_gcp_connector.psycopg import GoogleCloudConnInfoProvider

# Use the instance connection name (project:region:instance).
INSTANCE_CONNECTION_NAME = "my-project:us-central1:my-instance"
DB_USER = "your-sa-email@your-project.iam" # note there is no .gserviceaccount.com
DB_NAME = "your-database-name" # likely postgres
DATABASE_URL = f"postgresql://{DB_USER}@IGNORED-HOST/{DB_NAME}?sslmode=require"

def create_pool():
    get_conninfo = GoogleCloudConnInfoProvider(
        DATABASE_URL,
        instance_connection_name=INSTANCE_CONNECTION_NAME
    )
    return ConnectionPool(
        conninfo=get_conninfo,
        min_size=1,
        max_size=5
    )

# Usage
with create_pool() as pool:
    with pool.connection() as conn:
        print("Connected!")
        # ... execute queries ...

2. SQLAlchemy

Use sqlalchemy to connect to Cloud SQL. If you are using the instance connection name, it will automatically resolve the instance IP and inject the IAM token unless enable_iam_auth is set to false (it's True by default).

Dependencies:

  • sqlalchemy
  • psycopg (or pg8000)
  • google-auth
import sqlalchemy
from sqlalchemy import create_engine
from simple_gcp_connector.sqlalchemy import register_connector

# Configuration
INSTANCE_CONNECTION_NAME = "my-project:us-central1:my-instance"
DB_USER = "your-sa-email@your-project.iam"
DB_NAME = "your-database-name"

engine = create_engine(
    f"postgresql+psycopg://{DB_USER}@/{DB_NAME}",
    connect_args={"sslmode": "require"}
)

register_connector(engine, instance_connection_name=INSTANCE_CONNECTION_NAME)

# Usage
with engine.connect() as connection:
    result = connection.execute(sqlalchemy.text("SELECT 1"))
    print(result.fetchone())

Note on Cloud SQL Proxy

If you are using the Cloud SQL Auth Proxy (sidecar or local process), you don't need instance_connection_name. You can simply point to 127.0.0.1.

# Psycopg w/ Proxy
get_conninfo = GoogleCloudConnInfoProvider("postgresql://user@127.0.0.1:5432/db")

# SQLAlchemy w/ Proxy
engine = create_engine("postgresql+psycopg://user@127.0.0.1:5432/db")
register_connector(engine)

Setup Notes

  • Ensure the Service Account (or user) has the Cloud SQL Client role and is added as an IAM user in the Cloud SQL instance.
  • If youa enable_iam_authentication flag must be set to on for your Cloud SQL instance.

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

simple_gcp_connector-0.1.5.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

simple_gcp_connector-0.1.5-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file simple_gcp_connector-0.1.5.tar.gz.

File metadata

  • Download URL: simple_gcp_connector-0.1.5.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for simple_gcp_connector-0.1.5.tar.gz
Algorithm Hash digest
SHA256 78b81f9fc811e0f918ee80d990ea06cf065dce6fa43074b6a0440b58caa2140c
MD5 c89bd7c78d4610481d27569f48b6487c
BLAKE2b-256 3279addbd77e4db0f71c34bc5ccf3c08b08101ddfcd63b084799ba24b6b3c6d5

See more details on using hashes here.

File details

Details for the file simple_gcp_connector-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: simple_gcp_connector-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for simple_gcp_connector-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 68b55fc1e12492c727e793b1da219824183df0b729c3aa9238bdcc9adb89b4a6
MD5 b5718d8e453eed1299bc0b4a04338f07
BLAKE2b-256 0ab0449c3d2eb908b4bb3fdbbf3e6150ffe4554d1cc5fae10c13d1d2522a8fe2

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