launchflow.gcp.cloudsql
CloudSQLPostgres
1class CloudSQLPostgres(GCPResource[CloudSQLPostgresConnectionInfo],
2 PostgresClient)
A Postgres cluster running on Google Cloud SQL.
Example usage:
1from sqlalchemy import text
2import launchflow as lf
3
4postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
5
6# Quick utilities for connecting to SQLAlchemy, Django, and other ORMs
7engine = postgres.sqlalchemy_engine()
8
9with engine.connect() as connection:
10 print(connection.execute(text("SELECT 1")).fetchone()) # prints (1,)
__init__
1def __init__(
2 name: str,
3 *,
4 postgres_version: PostgresVersion = PostgresVersion.POSTGRES_15
5) -> None
Create a new Cloud SQL Postgres resource.
Args:
name
: The name of the Cloud SQL Postgres instance.postgres_version
: The version of Postgres to use. Defaults toPostgresVersion.POSTGRES_15
.
django_settings
1def django_settings()
Returns a Django settings dictionary for connecting to the Cloud SQL Postgres instance.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
4
5# settings.py
6DATABASES = {
7 # Connect Django's ORM to the Cloud SQL Postgres instance
8 "default": postgres.django_settings(),
9}
sqlalchemy_engine
1def sqlalchemy_engine(*, ip_type=None, **engine_kwargs)
Returns a SQLAlchemy engine for connecting to the Cloud SQL Postgres instance.
Args:
ip_type
: The IP type to use for the connection. If not provided will default to the most permisive IP address. For example if your Cloud SQL instance is provisioned with a public IP address, the default will beIPTypes.PUBLIC
. Otherwise it will default toIPTypes.PRIVATE
.**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
4
5# Creates a SQLAlchemy engine for connecting to the Cloud SQL Postgres instance
6engine = postgres.sqlalchemy_engine()
7
8with engine.connect() as connection:
9 print(connection.execute("SELECT 1").fetchone()) # prints (1,)
sqlalchemy_async_engine
1async def sqlalchemy_async_engine(*, ip_type=None, **engine_kwargs)
Returns an async SQLAlchemy engine for connecting to the Cloud SQL Postgres instance.
Args:
ip_type
: The IP type to use for the connection. If not provided will default to the most permisive IP address. For example if your Cloud SQL instance is provisioned with a public IP address, the default will beIPTypes.PUBLIC
. Otherwise it will default toIPTypes.PRIVATE
. -**engine_kwargs
: Additional keyword arguments to pass tocreate_async_engine
.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
4
5# Creates an async SQLAlchemy engine for connecting to the Cloud SQL Postgres instance
6engine = await postgres.sqlalchemy_async_engine()
7
8async with engine.begin() as connection:
9 result = await connection.execute("SELECT 1")
10 print(await result.fetchone())