Registry /
gcp / google-cloud-alloydb-connector
Install & Compatibility
Where this runs
tested against v1.13.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.960 runs
installs and imports cleanly · install 0.0s · import 3.144s · 84.7MB
glibcpy 3.10–3.960 runs
installs and imports cleanly · install 6.0s · import 1.947s · 85MB
86MB installed
● package 86MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Connector
✓ from google.cloud.alloydb.connector import Connector
IPTypes
✓ from google.cloud.alloydb.connector import IPTypes
Useful for specifying public or private IP connection.
This quickstart demonstrates how to establish a secure connection to a Google Cloud AlloyDB instance using `google-cloud-alloydb-connector` and the `psycopg2` driver. It assumes you have `psycopg2-binary` installed (e.g., via `pip install google-cloud-alloydb-connector[psycopg2]`). Remember to replace placeholder values or set environment variables for your project, instance, and database credentials.
import os
import psycopg2
from google.cloud.alloydb.connector import Connector
# --- Environment Variables (Recommended for Production) ---
# Replace with your actual AlloyDB instance connection details or set as ENV vars.
# Ensure the service account running this code has 'AlloyDB Connection User' role.
PROJECT_ID = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-project-id')
REGION = os.environ.get('ALLOYDB_REGION', 'us-central1')
CLUSTER_ID = os.environ.get('ALLOYDB_CLUSTER_ID', 'your-cluster-id')
INSTANCE_ID = os.environ.get('ALLOYDB_INSTANCE_ID', 'your-instance-id')
DB_USER = os.environ.get('ALLOYDB_DB_USER', 'postgres')
DB_PASS = os.environ.get('ALLOYDB_DB_PASS', 'your-db-password') # Use Secret Manager in production
DB_NAME = os.environ.get('ALLOYDB_DB_NAME', 'postgres')
# Full instance connection name format: projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE>
INSTANCE_CONNECTION_NAME = (
f"projects/{PROJECT_ID}/locations/{REGION}/clusters/{CLUSTER_ID}/instances/{INSTANCE_ID}"
)
def main():
connector = None
conn = None
try:
# 1. Initialize the AlloyDB connector. It uses google.auth.default() for credentials.
connector = Connector()
print(f"Attempting to connect to AlloyDB instance: {INSTANCE_CONNECTION_NAME}")
# 2. Connect to the AlloyDB instance using psycopg2.
# The connector automatically handles the secure proxy connection.
conn: psycopg2.Connection = connector.connect(
INSTANCE_CONNECTION_NAME,
"psycopg2", # Specify the database driver
user=DB_USER,
password=DB_PASS,
db_name=DB_NAME,
)
# 3. Execute a simple query to verify the connection.
with conn.cursor() as cursor:
cursor.execute("SELECT version();")
version = cursor.fetchone()[0]
print(f"Successfully connected to AlloyDB. PostgreSQL version: {version}")
except ImportError:
print("ERROR: PostgreSQL driver 'psycopg2-binary' not found. Please install with: ")
print(" pip install google-cloud-alloydb-connector[psycopg2]")
except Exception as e:
print(f"An error occurred during connection: {e}")
# Common errors:
if "PERMISSION_DENIED" in str(e):
print("Hint: Ensure the service account has 'AlloyDB Connection User' role.")
elif "Invalid instance connection name" in str(e):
print("Hint: Check the format of INSTANCE_CONNECTION_NAME.")
finally:
# 4. Clean up resources.
if conn:
conn.close()
if connector:
connector.close() # Important: close the connector to release resources
if __name__ == "__main__":
main()
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'google.cloud.alloydb.connector'
The `google-cloud-alloydb-connector` Python package is not installed in the current environment.
fixpip install google-cloud-alloydb-connector
ModuleNotFoundError: No module named 'psycopg2'
The database driver required by your application (e.g., `psycopg2` for PostgreSQL or `mysqlclient` for MySQL) is not installed; the AlloyDB Connector provides the secure tunnel but not the database driver itself.
fixpip install psycopg2-binary
google.api_core.exceptions.PermissionDenied: 403 Permission 'alloydb.instances.get' denied
The service account or user running the application lacks the necessary IAM permissions (e.g., `AlloyDB Client` role) to access the AlloyDB instance's metadata.
fixGrant the `AlloyDB Client` role (or a custom role with `alloydb.instances.get` and `alloydb.clusters.get`) to the service account or user account used for authentication.
google.api_core.exceptions.NotFound: 404 Requested entity was not found.
The `instance_connection_name` provided to the connector is incorrect, malformed, or refers to an AlloyDB instance that does not exist in the specified project and region.
fixVerify the `instance_connection_name` against your AlloyDB instance details in the Google Cloud Console, ensuring it follows the format `projects/<PROJECT_ID>/locations/<REGION>/clusters/<CLUSTER_ID>/instances/<INSTANCE_ID>`.
Upgrade
Version history
1.13.1latest on PyPI · released Jun 8, 2026
Audit
Dependencies
psycopg2-binaryoptionalRequired for PostgreSQL database interaction if using the psycopg2 driver. Not included by default.
asyncpgoptionalRequired for asynchronous PostgreSQL database interaction if using the asyncpg driver. Not included by default.