Cloud SQL Resources
Resources for Google Cloud SQL. Available resources include:
CloudSQLPostgres
: A Postgres cluster running on Google Cloud SQL.CloudSQLUser
: A user for a Cloud SQL Postgres instance.CloudSQLDatabase
: A database for a Cloud SQL Postgres instance.
Example Usage
Create a Cloud SQL Postgres instance
1from sqlalchemy import text
2import launchflow as lf
3
4# Automatically creates / connects to a Cloud SQL Postgres cluster in your GCP project
5postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
6
7# Quick utilities for connecting to SQLAlchemy, Django, and other ORMs
8engine = postgres.sqlalchemy_engine()
9
10with engine.connect() as connection:
11 print(connection.execute(text("SELECT 1")).fetchone()) # prints (1,)
Create a Cloud SQL Postgres instance with a custom database
1import launchflow as lf
2
3postgres_instance = lf.gcp.CloudSQLPostgres("my-pg-db", include_default_db=False)
4postgres_db = lf.gcp.CloudSQLDatabase("my-pg-db", cloud_sql_instance=postgres_instance)
5
6postgres_db.query("SELECT 1")
Create a Cloud SQL Postgres instance with a custom user and database
1import launchflow as lf
2
3postgres_instance = lf.gcp.CloudSQLPostgres("my-pg-db", include_default_user=False, include_default_db=False)
4postgres_user = lf.gcp.CloudSQLUser("my-pg-user", cloud_sql_instance=postgres_instance)
5postgres_db = lf.gcp.CloudSQLDatabase("my-pg-db", cloud_sql_instance=postgres_instance)
6
7postgres_db.query("SELECT 1", user=postgres_user)
CloudSQLPostgres
A Postgres cluster running on Google Cloud SQL.
Like all Resources, this class configures itself across multiple Environments.
For more information see the official documentation.
Example Usage
1from sqlalchemy import text
2import launchflow as lf
3
4# Automatically creates / connects to a Cloud SQL Postgres cluster in your GCP project
5postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
6
7# Quick utilities for connecting to SQLAlchemy, Django, and other ORMs
8engine = postgres.sqlalchemy_engine()
9
10with engine.connect() as connection:
11 print(connection.execute(text("SELECT 1")).fetchone()) # prints (1,)
initialization
Create a new Cloud SQL Postgres resource.
Args:
name (str)
: The name of the Cloud SQL Postgres instance.disk_size_gb (int)
: The size of the disk in GB. Defaults to10
.postgres_version (PostgresVersion)
: The version of Postgres to use. Defaults toPostgresVersion.POSTGRES_15
.include_default_db (bool)
: Whether to include a default database. Defaults toTrue
.include_default_user (bool)
: Whether to include a default user. Defaults toTrue
.delete_protection (bool)
: Whether to enable deletion protection. Defaults toFalse
.allow_public_access (bool)
: Whether to allow public access. Default toTrue
for development environments andFalse
for production environments.edition (Literal["ENTERPRISE_PLUS", "ENTERPRISE"])
: The edition of the Cloud SQL Postgres instance. Defaults to"ENTERPRISE"
.availability_type (Literal["REGIONAL", "ZONAL"])
: The availability type of the Cloud SQL Postgres instance. Defaults to"ZONAL"
for developments environments and"REGIONAL"
for production environments.database_tier (Literal["BASIC", "STANDARD_HA", "ENTERPRISE"])
: The tier of the Cloud SQL Postgres instance. Defaults to"db-f1-micro"
for development environments and"db-custom-1-3840"
for production environments.database_flags (Dict[Any, Any])
: Additional database flags to pass to your database instance. See: https://cloud.google.com/sql/docs/postgres/flags
query
1CloudSQLPostgres.query(query: str, user: Optional["CloudSQLUser"] = None)
Executes a query on the Cloud SQL Postgres instance.
Args:
query (str)
: The SQL query to execute.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- The results of the query.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
4
5# Executes a query on the Cloud SQL Postgres instance
6postgres.query("SELECT 1")
NOTE: This method is not recommended for production use. Use sqlalchemy_engine
instead.
django_settings
1CloudSQLPostgres.django_settings(user: Optional["CloudSQLUser"] = None)
Returns a Django settings dictionary for connecting to the Cloud SQL Postgres instance.
Args:
user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- A dictionary of Django settings 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_options
1CloudSQLPostgres.sqlalchemy_engine_options(*, ip_type=None, user: Optional["CloudSQLUser"] = None)
Get the SQLAlchemy engine options 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- The SQLAlchemy engine options.
sqlalchemy_async_engine_options
1async CloudSQLPostgres.sqlalchemy_async_engine_options(ip_type=None, user: Optional["CloudSQLUser"] = None)
Get the async SQLAlchemy engine options 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- The async SQLAlchemy engine options.
sqlalchemy_engine
1CloudSQLPostgres.sqlalchemy_engine(*, ip_type=None, user: Optional["CloudSQLUser"] = 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Returns:
- The SQLAlchemy 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 CloudSQLPostgres.sqlalchemy_async_engine(*, ip_type=None, user: Optional["CloudSQLUser"] = 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Returns:
- The async SQLAlchemy 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())
CloudSQLUser
initialization
Create a new Cloud SQL User resource.
Args:
name (str)
: The name of the Cloud SQL User.cloud_sql_instance (CloudSQLPostgres)
: The Cloud SQL Postgres instance.password (Optional[str])
: The password for the Cloud SQL User. Defaults toNone
.
CloudSQLDatabase
initialization
Create a new Cloud SQL Database resource.
Args:
name (str)
: The name of the Cloud SQL Database.cloud_sql_instance (CloudSQLPostgres)
: The Cloud SQL Postgres instance.
query
1CloudSQLDatabase.query(query: str, user: Optional["CloudSQLUser"] = None)
Executes a query on the Cloud SQL Database instance.
Args:
query (str)
: The SQL query to execute.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Example usage:
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLDatabase("my-pg-db")
4
5# Executes a query on the Cloud SQL Database instance
6postgres.query("SELECT 1")
NOTE: This method is not recommended for production use. Use sqlalchemy_engine
instead.
django_settings
1CloudSQLDatabase.django_settings(user: Optional[CloudSQLUser] = None)
Returns a Django settings dictionary for connecting to the Cloud SQL Postgres instance.
Args:
user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- A dictionary of Django settings 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_options
1CloudSQLDatabase.sqlalchemy_engine_options(*, ip_type=None, user: Optional[CloudSQLUser] = None)
Get the SQLAlchemy engine options 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- The SQLAlchemy engine options.
sqlalchemy_async_engine_options
1async CloudSQLDatabase.sqlalchemy_async_engine_options(ip_type=None, user: Optional[CloudSQLUser] = None)
Get the async SQLAlchemy engine options 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.
Returns:
- The async SQLAlchemy engine options.
sqlalchemy_engine
1CloudSQLDatabase.sqlalchemy_engine(*, ip_type=None, user: Optional[CloudSQLUser] = 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Returns:
- The SQLAlchemy 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 CloudSQLDatabase.sqlalchemy_async_engine(*, ip_type=None, user: Optional["CloudSQLUser"] = 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
.user (CloudSQLUser)
: TheCloudSQLUser
to authenticate as. If not provided the default user for the instance will be used.**engine_kwargs
: Additional keyword arguments to pass tosqlalchemy.create_engine
.
Returns:
- The async SQLAlchemy 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())