import sqlalchemy
from google.cloud.alloydbconnector import Connector
INSTANCE_URI = "projects/MY_PROJECT/locations/MY_REGION/clusters/MY_CLUSTER/instances/MY_INSTANCE"
with Connector() as connector:
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=lambda: connector.connect(
INSTANCE_URI,
"pg8000",
user="my-user",
password="my-password",
db="my-db",
),
)
with pool.connect() as conn:
result = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone()
print(result)
import sqlalchemy
from google.cloud.alloydbconnector import Connector
INSTANCE_URI = "projects/MY_PROJECT/locations/MY_REGION/clusters/MY_CLUSTER/instances/MY_INSTANCE"
with Connector() as connector:
pool = sqlalchemy.create_engine(
"postgresql+psycopg://",
creator=lambda: connector.connect(
INSTANCE_URI,
"psycopg",
user="my-user",
password="my-password",
db="my-db",
),
)
with pool.connect() as conn:
result = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone()
print(result)
Note: By default the connector uses private IP. Run your code from an
environment connected to the VPC that hosts your AlloyDB instance, or see
Configuring AlloyDB Connectivity for other options.
Configuration
Connector Lifecycle
Create one connector per application and reuse it for the lifetime of the
process. Each connector maintains a background refresh cycle that keeps
connection credentials warm, so creating one per request would waste resources
and cause unnecessary latency.
The recommended approach is a context manager — close() is called
automatically, even if an exception is raised:
# Sync
with Connector() as connector:
...
# Async
async with AsyncConnector() as connector:
...
For long-lived applications (e.g. a web server) where the connector outlives
any single block, call close() explicitly at shutdown:
Connect over private IP (default), public IP, or Private Service Connect (PSC):
# At the Connector level (applies to all connections)
connector = Connector(ip_type="PUBLIC")
# Or per connection
connector.connect(INSTANCE_URI, "pg8000", ..., ip_type="PSC")
For IAM user accounts, use the full email address as user.
Lazy Refresh (Cloud Run, Cloud Functions)
In serverless environments where CPU may be throttled between requests, use
refresh_strategy="lazy" to fetch connection info on demand instead of
running a background refresh cycle:
Active — receives all new features and security fixes. New major versions are guaranteed active for a minimum of 1 year.
Deprecated — receives security and critical bug fixes only, for 1 year after deprecation.
Unsupported — any major version deprecated for ≥1 year.
Supported Python Versions
Follows the Python Version Support Policy used by Google Cloud
Libraries for Python. Changes in supported Python versions are treated as
minor changes and listed in the release notes.
Release Cadence
This project targets a minimum monthly release cadence. If no new features or
fixes have been added, a new PATCH version with the latest dependencies is
released.
Contributing
We welcome outside contributions. Please see our
Contributing Guide for details on how best to contribute.
Works well with
Use together with google-cloud-alloydb-connector: extensions, drivers, servers and companion tools.