### Credentials Management Example Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/types.md Demonstrates how to obtain and use service account or default application credentials with the Connector. ```python from google.cloud.sql.connector import Connector from google.oauth2 import service_account import google.auth # Service account credentials creds = service_account.Credentials.from_service_account_file( "/path/to/key.json" ) # Default (Application Default Credentials) creds, project = google.auth.default() # Use with Connector connector = Connector(credentials=creds) ``` -------------------------------- ### Database Connection Examples by Driver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/types.md Illustrates establishing database connections using different drivers (pymysql, pg8000, pytds) with the Connector. ```python from google.cloud.sql.connector import Connector connector = Connector() # Returns pymysql.connections.Connection mysql_conn = connector.connect( "my-project:us-central1:my-mysql-instance", "pymysql", user="user", password="pass", db="db" ) # Returns pg8000.dbapi.Connection pg_conn = connector.connect( "my-project:us-central1:my-pg-instance", "pg8000", user="user", password="pass", db="db" ) # Returns pytds.Connection mssql_conn = connector.connect( "my-project:us-central1:my-mssql-instance", "pytds", user="user", password="pass", db="db" ) import asyncio async def async_example(): # Returns asyncpg.Connection asyncpg_conn = await connector.connect_async( "my-project:us-central1:my-pg-instance", "asyncpg", user="user", password="pass", db="db" ) asyncio.run(async_example()) ``` -------------------------------- ### Install asyncpg driver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Install the Cloud SQL Python Connector with the asyncpg extra to support asynchronous PostgreSQL connections. ```bash pip install "cloud-sql-python-connector[asyncpg]" ``` -------------------------------- ### Install pytds driver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Install the Cloud SQL Python Connector with the pytds extra to support SQL Server connections. ```bash pip install "cloud-sql-python-connector[pytds]" ``` -------------------------------- ### Install pg8000 driver for Cloud SQL Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Installs the Cloud SQL Python Connector with pg8000 support. Use this command to add the necessary dependencies. ```bash pip install "cloud-sql-python-connector[pg8000]" ``` -------------------------------- ### Install Cloud SQL Python Connector and SQLAlchemy Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/sqlserver_python_connector.ipynb Installs the Cloud SQL Python Connector with pytds support, SQLAlchemy, and sqlalchemy-pytds. Recommended for connection pooling. ```python # install dependencies import sys !{sys.executable} -m pip install cloud-sql-python-connector["pytds"] SQLAlchemy==2.0.7 sqlalchemy-pytds==0.3.5 ``` -------------------------------- ### Example TXT Record Format Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/resolver.md Illustrates the required format for a TXT record when using private DNS with Cloud SQL instance connection names. ```text Name: prod-db.mycompany.com Type: TXT Value: my-project:us-central1:my-instance ``` -------------------------------- ### PyMySQL Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Example of common parameters for establishing a PyMySQL connection. Ensure correct user, password, and database name are provided. ```python connector.connect( "my-project:us-central1:my-instance", "pymysql", user="root", # MySQL username password="password", # MySQL password db="my_database", # Database name charset="utf8mb4", # Character encoding autocommit=False, # Auto-commit transactions connect_timeout=10, # Connection timeout in seconds port=3306 # Port (usually 3306) ) ``` -------------------------------- ### asyncpg Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Example of common parameters for establishing an asyncpg connection. This uses the asynchronous connector and supports command timeouts. ```python await connector.connect_async( "my-project:us-central1:my-instance", "asyncpg", user="postgres", # PostgreSQL user password="password", # PostgreSQL password db="my_database", # Database name ssl=True, # Enable SSL (set by connector) application_name="my-app", # Application name for logging command_timeout=10 # Query timeout in seconds ) ``` -------------------------------- ### Install PyMySQL driver for Cloud SQL Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Installs the Cloud SQL Python Connector with PyMySQL support. Use this command to add the necessary dependencies. ```bash pip install "cloud-sql-python-connector[pymysql]" ``` -------------------------------- ### Check Installed Connector Version Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/README.md Provides a simple Python snippet to print the installed version of the Cloud SQL Python Connector. ```python from google.cloud.sql.connector import __version__ print(__version__) ``` -------------------------------- ### Basic DNS Setup with Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/resolver.md Initializes the Connector with DnsResolver and uses a DNS domain name in connect() calls instead of a direct connection name. Requires a pre-configured DNS TXT record. ```python from google.cloud.sql.connector import Connector, DnsResolver import sqlalchemy # Configure DNS record for domain "prod-db.mycompany.com" # TXT record: "my-project:us-central1:my-instance" connector = Connector(resolver=DnsResolver) pool = sqlalchemy.create_engine( "mysql+pymysql://", creator=lambda: connector.connect( "prod-db.mycompany.com", # Use DNS domain instead of connection name "pymysql", user="my-user", password="my-password", db="my-database" ), ) with pool.connect() as db_conn: result = db_conn.execute(sqlalchemy.text("SELECT NOW()")) ``` -------------------------------- ### Connect to PostgreSQL with asyncpg Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Use `create_async_connector` to establish an asynchronous connection to a PostgreSQL instance. This example shows both direct connection and using an asyncpg pool. ```python import asyncio from google.cloud.sql.connector import create_async_connector import asyncpg async def main(): connector = await create_async_connector() # Using Connector.connect_async (recommended) conn = await connector.connect_async( "my-project:us-central1:my-postgres-instance", "asyncpg", user="my-user", password="my-password", db="my-database" ) # Query database result = await conn.fetch("SELECT NOW()") print(result) # Using asyncpg pool async def getconn(): return await connector.connect_async( "my-project:us-central1:my-postgres-instance", "asyncpg", user="my-user", password="my-password", db="my-database" ) pool = await asyncpg.create_pool( "my-project:us-central1:my-postgres-instance", connect=getconn, min_size=5, max_size=10 ) async with pool.acquire() as conn: result = await conn.fetch("SELECT NOW()") print(result) await connector.close_async() asyncio.run(main()) ``` -------------------------------- ### pytds Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Example of common parameters for establishing a pytds connection. Note the typical port for SQL Server connections. ```python connector.connect( "my-project:us-central1:my-instance", "pytds", user="sa", # SQL Server user password="password", # SQL Server password db="master", # Database name port=1433 # Port (usually 1433) ) ``` -------------------------------- ### Asynchronously Connect to Cloud SQL Instance Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connector.md This example shows how to use the `connect_async` method to establish an asynchronous connection to a Cloud SQL instance. This is suitable for use with async frameworks and drivers like `asyncpg`. ```python async def connect_async( instance_connection_string: str, driver: str, **kwargs: Any ) -> Any: # ... implementation details ... pass ``` -------------------------------- ### Authenticate User and Install Dependencies Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/postgres_python_connector.ipynb Authenticates the user for Google Cloud access and installs the Cloud SQL Python Connector with pg8000 support and SQLAlchemy. This sets up the environment for connecting to Cloud SQL. ```python from google.colab import auth auth.authenticate_user() ``` ```python # install dependencies import sys !{sys.executable} -m pip install cloud-sql-python-connector["pg8000"] SQLAlchemy==2.0.7 ``` -------------------------------- ### Create SQLAlchemy Async Engine using Async Context Manager Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/README.md Initialize a Connector as an async context manager for automatic resource cleanup when creating a SQLAlchemy asynchronous engine. This example demonstrates the setup with PostgreSQL. ```python import asyncio import asyncpg import sqlalchemy from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine from google.cloud.sql.connector import Connector async def main(): # initialize Connector object for connections to Cloud SQL loop = asyncio.get_running_loop() async with Connector(loop=loop) as connector: # The Cloud SQL Python Connector can be used along with SQLAlchemy using the # 'async_creator' argument to 'create_async_engine' pool = create_async_engine( "postgresql+asyncpg://", async_creator=lambda: connector.connect_async( "project:region:instance", # Cloud SQL instance connection name "asyncpg", user="my-user", password="my-password", db="my-db-name" # ... additional database driver args ), ) # example query async with pool.connect() as conn: await conn.execute(sqlalchemy.text("SELECT NOW()")) # dispose of connection pool await pool.dispose() ``` -------------------------------- ### Install Python Connector and SQLAlchemy Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/mysql_python_connector.ipynb Installs the Cloud SQL Python Connector and SQLAlchemy, a library for connection pooling, using pip. This is a prerequisite for connecting to Cloud SQL from Python. ```python # install dependencies import sys !{sys.executable} -m pip install cloud-sql-python-connector["pymysql"] SQLAlchemy==2.0.7 ``` -------------------------------- ### Configure Cloud SQL Python Connector with All Options Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md A comprehensive example showcasing all available configuration options for the Cloud SQL Python Connector, including network, authentication, connection, quota, API, refresh strategy, and DNS settings. ```python from google.cloud.sql.connector import ( Connector, DnsResolver, IPTypes, RefreshStrategy ) from google.oauth2 import service_account # Load custom credentials credentials = service_account.Credentials.from_service_account_file( "/path/to/service-account-key.json" ) # Create connector with all options configured connector = Connector( # Network configuration ip_type=IPTypes.PRIVATE, # Authentication enable_iam_auth=False, credentials=credentials, # Connection settings timeout=60, # Quota and billing quota_project="my-billing-project", # API configuration sqladmin_api_endpoint="https://sqladmin.googleapis.com", user_agent="my-app/1.0", universe_domain="googleapis.com", # Refresh strategy refresh_strategy=RefreshStrategy.BACKGROUND, # DNS resolution and failover resolver=DnsResolver, failover_period=30 ) # Use the configured connector conn = connector.connect( "prod-db.mycompany.com", "pymysql", user="my-user", password="my-password", db="my-database" ) # Cleanup connector.close() ``` -------------------------------- ### Inspect Parsed Connection Name Components Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_name.md Asynchronous example demonstrating how to resolve a connection name using DefaultResolver and access its parsed components (project, region, instance name). ```python import asyncio from google.cloud.sql.connector import Connector, DefaultResolver async def inspect_connection_name(): resolver = DefaultResolver() conn_name = await resolver.resolve("my-project:us-central1:my-instance") print(f"Project: {conn_name.project}") # my-project print(f"Region: {conn_name.region}") # us-central1 print(f"Instance: {conn_name.instance_name}") # my-instance print(f"Full: {str(conn_name)}") # my-project:us-central1:my-instance print(f"String: {conn_name.get_connection_string()}") # my-project:us-central1:my-instance asyncio.run(inspect_connection_name()) ``` -------------------------------- ### pg8000 Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Example of common parameters for establishing a pg8000 connection. SSL is enabled by default by the connector. ```python connector.connect( "my-project:us-central1:my-instance", "pg8000", user="postgres", # PostgreSQL user password="password", # PostgreSQL password db="my_database", # Database name ssl=True, # Enable SSL (set by connector) application_name="my-app", # Application name for logging connect_timeout=10 # Connection timeout in seconds ) ``` -------------------------------- ### Connect to SQL Server with pytds (Standard Auth) Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Establish a standard authenticated connection to a SQL Server instance using the pytds driver. Ensure the pytds module is installed. ```python from google.cloud.sql.connector import Connector connector = Connector() conn = connector.connect( "my-project:us-central1:my-sqlserver-instance", "pytds", user="my-user", password="my-password", db="my-database" ) # Execute query cursor = conn.cursor() cursor.execute("SELECT @@VERSION") print(cursor.fetchone()) ``` -------------------------------- ### PlatformNotSupportedError Example Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Demonstrates how to handle PlatformNotSupportedError when attempting Active Directory authentication on a non-Windows platform. It includes a check for the operating system and a try-except block to catch the specific exception. ```python from google.cloud.sql.connector import Connector from google.cloud.sql.connector.exceptions import PlatformNotSupportedError import platform # Active Directory authentication only works on Windows if platform.system() != "Windows": print("Active Directory authentication requires Windows") connector = Connector() try: # This fails on non-Windows platforms conn = connector.connect( "my-project:us-central1:my-sqlserver-instance", "pytds", db="my-database", active_directory_auth=True, server_name="public.my-instance.location.project.cloudsql.domain" ) except PlatformNotSupportedError as e: print(f"Error: {e}") # Active Directory authentication is currently only supported on Windows. ``` -------------------------------- ### Optional Type Examples Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/types.md Illustrates the use of Optional types for parameters that can accept None, such as Credentials, asyncio EventLoop, string values, and SSLContext. ```python # Any of these can be None Optional[Credentials] Optional[asyncio.AbstractEventLoop] Optional[str] Optional[ssl.SSLContext] ``` -------------------------------- ### Connect with DefaultResolver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_name.md Example of connecting to a Cloud SQL instance using the Connector with its default resolver. The connector automatically resolves the instance connection name. ```python from google.cloud.sql.connector import Connector connector = Connector() # The Connector resolves the connection name internally conn = connector.connect( "my-project:us-central1:my-instance", "pymysql", user="my-user", password="my-password", db="my-database" ) ``` -------------------------------- ### Connect to Cloud SQL with PyMySQL using Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Recommended method for establishing a PyMySQL connection to a Cloud SQL instance using the Connector. Handles SSL setup and parameter mapping. ```python from google.cloud.sql.connector import Connector import pymysql connector = Connector() # Using Connector.connect (recommended) conn = connector.connect( "my-project:us-central1:my-mysql-instance", "pymysql", user="my-user", password="my-password", db="my-database", charset="utf8mb4" ) ``` -------------------------------- ### Connect to SQL Server with Active Directory Authentication (Windows) Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/README.md Establish a connection to a SQL Server instance using Active Directory authentication on Windows. Requires prior setup of Managed AD and joining the Cloud SQL instance to the domain. ```python conn = connector.connect( "project:region:instance", "pytds", db="my-db-name", active_directory_auth=True, server_name="public.[instance].[location].[project].cloudsql.[domain]", ) ``` -------------------------------- ### Connect to Cloud SQL with pg8000 using Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Recommended method for establishing a pg8000 connection to a Cloud SQL instance using the Connector. Handles SSL setup and parameter mapping. ```python from google.cloud.sql.connector import Connector connector = Connector() # Using Connector.connect (recommended) conn = connector.connect( "my-project:us-central1:my-postgres-instance", "pg8000", user="my-user", password="my-password", db="my-database" ) ``` -------------------------------- ### Create SQLAlchemy Async Engine Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/README.md Use the Cloud SQL Python Connector with SQLAlchemy to create an asynchronous database engine. This example demonstrates initializing the connector and setting up the engine with a PostgreSQL connection. ```python import sqlalchemy from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine from google.cloud.sql.connector import Connector, create_async_connector async def main(): # initialize Connector object for connections to Cloud SQL connector = await create_async_connector() # The Cloud SQL Python Connector can be used along with SQLAlchemy using the # 'async_creator' argument to 'create_async_engine' pool = create_async_engine( "postgresql+asyncpg://", async_creator=lambda: connector.connect_async( "project:region:instance", # Cloud SQL instance connection name "asyncpg", user="my-user", password="my-password", db="my-db-name" # ... additional database driver args ), ) # example query async with pool.connect() as conn: await conn.execute(sqlalchemy.text("SELECT NOW()")) # close Connector await connector.close_async() # dispose of connection pool await pool.dispose() ``` -------------------------------- ### Create Asyncpg Connection Pool using Async Context Manager Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/README.md Initialize a Connector as an async context manager to automatically handle resource cleanup. This example shows how to create an asyncpg connection pool using this approach. ```python import asyncpg from google.cloud.sql.connector import Connector, create_async_connector import asyncio async def main(): # initialize Connector object for connections to Cloud SQL loop = asyncio.get_running_loop() async with Connector(loop=loop) as connector: # creation function to generate asyncpg connections as the 'connect' arg async def getconn(instance_connection_name, **kwargs) -> asyncpg.Connection: return await connector.connect_async( instance_connection_name, "asyncpg", user="my-user", password="my-password", db="my-db", **kwargs, # ... additional asyncpg args ) # create connection pool pool = await asyncpg.create_pool( "my-project:my-region:my-instance", connect=getconn ) # acquire connection and query Cloud SQL database async with pool.acquire() as conn: res = await conn.fetch("SELECT NOW()") ``` -------------------------------- ### Connect with DnsResolver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_name.md Example of connecting to a Cloud SQL instance using the Connector with a DnsResolver. This allows the connector to resolve a DNS domain name to an instance connection name, typically configured via DNS TXT records. ```python from google.cloud.sql.connector import Connector, DnsResolver # DNS TXT record: prod-db.mycompany.com -> my-project:us-central1:my-instance connector = Connector(resolver=DnsResolver) # The Connector resolves the DNS domain to a ConnectionName conn = connector.connect( "prod-db.mycompany.com", "pymysql", user="my-user", password="my-password", db="my-database" ) ``` -------------------------------- ### Connect to SQL Server with Active Directory Authentication (Windows, Private IP) Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/README.md Connect to a SQL Server instance using Active Directory authentication over a private IP connection on Windows. This also requires Managed AD setup and domain joining. ```python conn = connector.connect( "project:region:instance", "pytds", db="my-db-name", active_directory_auth=True, server_name="private.[instance].[location].[project].cloudsql.[domain]", ip_type="private" ) ``` -------------------------------- ### Creating and Using ConnectionInfo Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_info.md Demonstrates how to create a ConnectionInfo object with instance details and retrieve preferred IP addresses (public or private). ```python from google.cloud.sql.connector.enums import IPTypes from google.cloud.sql.connector.connection_info import ConnectionInfo from google.cloud.sql.connector.connection_name import ConnectionName import datetime conn_info = ConnectionInfo( conn_name=ConnectionName("my-project", "us-central1", "my-instance"), client_cert="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", server_ca_cert="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", private_key=b"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----", ip_addrs={ "PRIMARY": "192.0.2.1", # Public IP "PRIVATE": "10.0.0.1" # Private IP }, database_version="POSTGRES_16", expiration=datetime.datetime.utcnow() + datetime.timedelta(hours=1) ) # Get public IP address public_ip = conn_info.get_preferred_ip(IPTypes.PUBLIC) print(public_ip) # Output: 192.0.2.1 # Get private IP address private_ip = conn_info.get_preferred_ip(IPTypes.PRIVATE) print(private_ip) # Output: 10.0.0.1 # Attempt to get unavailable IP type try: psc_ip = conn_info.get_preferred_ip(IPTypes.PSC) except Exception as e: print(f"Error: {e}") # Cloud SQL instance does not have any IP addresses matching preference: PSC ``` -------------------------------- ### Initialize Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/sqlserver_python_connector.ipynb Sets up the necessary parameters for connecting to a Cloud SQL instance, including the instance connection name, database user, and database name. ```python # initialize parameters INSTANCE_CONNECTION_NAME = f"{project_id}:{region}:{instance_name}" # i.e demo-project:us-central1:demo-instance print(f"Your instance connection name is: {INSTANCE_CONNECTION_NAME}") DB_USER = "sqlserver" ``` -------------------------------- ### Create, Insert, and Query Table Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/mysql_python_connector.ipynb Demonstrates how to interact with the database using the established connection pool. This includes creating a table, inserting data, and querying the results. ```python # connect to connection pool with pool.connect() as db_conn: # create ratings table in our sandwiches database db_conn.execute( sqlalchemy.text( "CREATE TABLE IF NOT EXISTS ratings " "( id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, " "origin VARCHAR(255) NOT NULL, rating FLOAT NOT NULL, " "PRIMARY KEY (id));" ) ) # commit transaction (SQLAlchemy v2.X.X is commit as you go) db_conn.commit() # insert data into our ratings table insert_stmt = sqlalchemy.text( "INSERT INTO ratings (name, origin, rating) VALUES (:name, :origin, :rating)", ) # insert entries into table db_conn.execute(insert_stmt, parameters={\"name\": \"HOTDOG\", \"origin\": \"Germany\", \"rating\": 7.5}) db_conn.execute(insert_stmt, parameters={\"name\": \"BÀNH MÌ\", \"origin\": \"Vietnam\", \"rating\": 9.1}) db_conn.execute(insert_stmt, parameters={\"name\": \"CROQUE MADAME\", \"origin\": \"France\", \"rating\": 8.3}) # commit transactions db_conn.commit() # query and fetch ratings table results = db_conn.execute(sqlalchemy.text("SELECT * FROM ratings")).fetchall() # show results for row in results: print(row) ``` -------------------------------- ### Initialize Connector with Defaults Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Minimal configuration using default settings for the Cloud SQL Python Connector. ```python from google.cloud.sql.connector import Connector connector = Connector() ``` -------------------------------- ### Initialize Connection Parameters Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/notebooks/mysql_python_connector.ipynb Define the necessary parameters for connecting to your Cloud SQL instance, including instance connection name, user, password, and database name. ```python INSTANCE_CONNECTION_NAME = f"{project_id}:{region}:{instance_name}" # i.e demo-project:us-central1:demo-instance print(f"Your instance connection name is: {INSTANCE_CONNECTION_NAME}") DB_USER = "chef" DB_PASS = "food" DB_NAME = "sandwiches" ``` -------------------------------- ### Connector Universe Domain Configuration Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connector.md Demonstrates how to configure and access the universe domain for the Connector. Shows both explicit setting and default behavior. ```python from google.cloud.sql.connector import Connector connector = Connector(universe_domain="my-universe.com") print(connector.universe_domain) # Output: my-universe.com connector2 = Connector() print(connector2.universe_domain) # Output: googleapis.com ``` -------------------------------- ### Valid and Invalid Domain Names Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/resolver.md Provides examples of valid and invalid domain names according to RFC specifications for DNS validation. ```text - prod-db.mycompany.com ✓ - my_database.internal ✓ - database-1.example.com ✓ - _service._tcp.example ✓ Invalid: - database@example.com ✗ (contains @) - example ✗ (no TLD) - -database.com ✗ (starts with -) ``` -------------------------------- ### Load Custom Credentials from File Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Use custom Google credentials by loading them from a service account key file. Ensure the file path and scopes are correctly specified. ```python from google.cloud.sql.connector import Connector from google.oauth2 import service_account # Load credentials from file credentials = service_account.Credentials.from_service_account_file( "/path/to/service-account-key.json", scopes=["https://www.googleapis.com/auth/sqlservice.admin"] ) connector = Connector(credentials=credentials) ``` -------------------------------- ### Async Connector with Error Handling Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Demonstrates establishing an asynchronous connection and handling potential errors like ConnectorLoopError. ```python import asyncio from google.cloud.sql.connector import create_async_connector from google.cloud.sql.connector.exceptions import ConnectorLoopError async def main(): try: connector = await create_async_connector() conn = await connector.connect_async( "my-project:us-central1:my-instance", "asyncpg", user="my-user", password="my-password", db="my-database" ) # Use connection... except ConnectorLoopError as e: print(f"Event loop error: {e}") except Exception as e: print(f"Connection failed: {e}") finally: await connector.close_async() asyncio.run(main()) ``` -------------------------------- ### Basic Connector Usage Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Demonstrates the basic usage of the Connector class to establish a database connection and close it. ```python from google.cloud.sql.connector import Connector connector = Connector() conn1 = connector.connect( "my-project:us-central1:my-instance", "pymysql", user="my-user", password="my-password", db="my-database" ) connector.close() ``` ```python connector = Connector() conn2 = connector.connect( "my-project:us-central1:my-instance", "pymysql", user="my-user", password="my-password", db="my-database" ) ``` -------------------------------- ### Build Docker Image for Cloud Run Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/samples/cloudrun/README.md Build a Docker image for your Cloud Run application. Replace 'mysql' with 'postgres' or 'sqlserver' depending on your database choice. ```bash docker build -t REGION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME mysql ``` -------------------------------- ### Get Standard Connection String Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_name.md Illustrates using the get_connection_string method to retrieve the instance connection name in the standard format, excluding any domain name. ```python from google.cloud.sql.connector.connection_name import ConnectionName conn_name = ConnectionName("my-project", "us-central1", "my-instance") print(conn_name.get_connection_string()) # Output: my-project:us-central1:my-instance conn_name_with_dns = ConnectionName( "my-project", "us-central1", "my-instance", "prod-db.mycompany.com" ) print(conn_name_with_dns.get_connection_string()) # Output: my-project:us-central1:my-instance ``` -------------------------------- ### Initialize Connector with DnsResolver Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/resolver.md Instantiate the Connector class using the DnsResolver for DNS-based instance name resolution. ```python from google.cloud.sql.connector import Connector, DnsResolver # Use DNS-based resolution connector = Connector(resolver=DnsResolver) ``` -------------------------------- ### AutoIAMAuthNotSupported Example Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Illustrates how to handle AutoIAMAuthNotSupported when IAM authentication is requested for an unsupported database engine version. It uses a try-except block to catch the exception and prints an error message. ```python from google.cloud.sql.connector import Connector from google.cloud.sql.connector.exceptions import AutoIAMAuthNotSupported connector = Connector(enable_iam_auth=True) try: conn = connector.connect( "my-project:us-central1:my-instance", "pymysql", user="iam-user@gmail.com", db="my-database", enable_iam_auth=True ) except AutoIAMAuthNotSupported as e: print(f"Error: {e}") ``` -------------------------------- ### Set Quota Project via Environment Variable Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Configure the quota project for billing by setting the `GOOGLE_CLOUD_QUOTA_PROJECT` environment variable. ```bash export GOOGLE_CLOUD_QUOTA_PROJECT=my-billing-project ``` -------------------------------- ### Configure Connector with Lazy Refresh and Private IP Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Use this configuration for private IP within a VPC and lazy refresh to optimize for cold starts. A higher timeout is recommended. ```python from google.cloud.sql.connector import Connector, IPTypes connector = Connector( ip_type=IPTypes.PRIVATE, # Private IP within VPC refresh_strategy=RefreshStrategy.LAZY, # Lazy refresh timeout=60 # Higher timeout for cold starts ) def handle_request(request): # Create connection on-demand conn = connector.connect( "my-project:us-central1:my-instance", "pymysql", user="my-user", password="my-password", db="my-database" ) # Use connection... return response ``` -------------------------------- ### Connect to Different Database Engines Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Shows how to use the correct Python driver for PostgreSQL, MySQL, and SQL Server instances when connecting via the Cloud SQL Python Connector. ```python from google.cloud.sql.connector import Connector connector = Connector() # PostgreSQL instance - use pg8000 or asyncpg pg_conn = connector.connect( "my-project:us-central1:my-postgres-instance", "pg8000", # or "asyncpg" user="my-user", password="my-password", db="my-database" ) # MySQL instance - use pymysql mysql_conn = connector.connect( "my-project:us-central1:my-mysql-instance", "pymysql", user="my-user", password="my-password", db="my-database" ) # SQL Server instance - use pytds mssql_conn = connector.connect( "my-project:us-central1:my-sqlserver-instance", "pytds", user="my-user", password="my-password", db="my-database" ) ``` -------------------------------- ### DnsResolver for Automatic Failover Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/resolver.md Using DnsResolver for Automatic Failover enables the Connector to detect DNS record changes and automatically failover to a new instance, useful for high-availability setups. ```APIDOC ## Using DnsResolver for Automatic Failover When using `DnsResolver`, the Connector can detect changes to DNS records and automatically failover to a new instance. This is useful for implementing high-availability setups with Advanced Disaster Recovery. ### Setup 1. Create a DNS TXT record mapping a domain name to an instance connection name 2. Initialize the Connector with `resolver=DnsResolver` 3. Use the DNS domain name in `connect()` calls ### Example: Basic DNS Setup ```python from google.cloud.sql.connector import Connector, DnsResolver import sqlalchemy # Configure DNS record for domain "prod-db.mycompany.com" # TXT record: "my-project:us-central1:my-instance" connector = Connector(resolver=DnsResolver) pool = sqlalchemy.create_engine( "mysql+pymysql://", creator=lambda: connector.connect( "prod-db.mycompany.com", # Use DNS domain instead of connection name "pymysql", user="my-user", password="my-password", db="my-database" ), ) with pool.connect() as db_conn: result = db_conn.execute(sqlalchemy.text("SELECT NOW()")) ``` ### Example: Failover Scenario ```python from google.cloud.sql.connector import Connector, DnsResolver # Initial setup # DNS: prod-db.mycompany.com -> my-project:us-central1:primary-instance connector = Connector(resolver=DnsResolver, failover_period=30) # Application starts and connects conn = connector.connect( "prod-db.mycompany.com", "pymysql", user="my-user", password="my-password", db="my-database" ) # Connected to primary-instance # Failover happens: Update DNS record # DNS: prod-db.mycompany.com -> my-project:us-central1:replica-instance # Connector detects change within failover_period (30 seconds) # Existing connections to primary-instance are closed # New connections use replica-instance new_conn = connector.connect( "prod-db.mycompany.com", "pymysql", user="my-user", password="my-password", db="my-database" ) # Connected to replica-instance ``` ### Failover Configuration The failover check frequency can be configured via the `failover_period` parameter: ```python from google.cloud.sql.connector import Connector, DnsResolver # Check for failover every 60 seconds connector = Connector( resolver=DnsResolver, failover_period=60 # Default is 30 seconds ) # Disable periodic failover checks (only check on new connections) connector = Connector( resolver=DnsResolver, failover_period=0 ) ``` ``` -------------------------------- ### Configure Connector for High Availability with DNS Failover Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Enable DNS resolution for high-availability setups. The connector automatically detects DNS changes and handles failover. Background refresh is recommended. ```python from google.cloud.sql.connector import Connector, DnsResolver # Setup DNS TXT records for instances: # prod-db.mycompany.com -> my-project:us-central1:primary-instance # The DNS record can be updated to point to a replica instance connector = Connector( resolver=DnsResolver, # Enable DNS resolution failover_period=30, # Check every 30 seconds refresh_strategy="BACKGROUND" # Background refresh ) conn = connector.connect( "prod-db.mycompany.com", "pymysql", user="my-user", password="my-password", db="my-database" ) # Connector automatically detects DNS changes and failover ``` -------------------------------- ### ConnectionInfo Usage Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/connection_info.md Demonstrates how to create and use a `ConnectionInfo` object to retrieve public and private IP addresses for a Cloud SQL instance. ```APIDOC ## ConnectionInfo Usage ### Description Demonstrates how to create and use a `ConnectionInfo` object to retrieve public and private IP addresses for a Cloud SQL instance. ### Code Example ```python from google.cloud.sql.connector.enums import IPTypes from google.cloud.sql.connector.connection_info import ConnectionInfo from google.cloud.sql.connector.connection_name import ConnectionName import datetime conn_info = ConnectionInfo( conn_name=ConnectionName("my-project", "us-central1", "my-instance"), client_cert="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", server_ca_cert="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----", private_key=b"-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----", ip_addrs={ "PRIMARY": "192.0.2.1", # Public IP "PRIVATE": "10.0.0.1" # Private IP }, database_version="POSTGRES_16", expiration=datetime.datetime.utcnow() + datetime.timedelta(hours=1) ) # Get public IP address public_ip = conn_info.get_preferred_ip(IPTypes.PUBLIC) print(public_ip) # Output: 192.0.2.1 # Get private IP address private_ip = conn_info.get_preferred_ip(IPTypes.PRIVATE) print(private_ip) # Output: 10.0.0.1 # Attempt to get unavailable IP type try: psc_ip = conn_info.get_preferred_ip(IPTypes.PSC) except Exception as e: print(f"Error: {e}") # Cloud SQL instance does not have any IP addresses matching preference: PSC ``` ``` -------------------------------- ### Override Connector Settings Per Connection Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Demonstrates how to use global connector defaults and then override specific settings like IP type, IAM authentication, and timeout for individual connections. ```python from google.cloud.sql.connector import Connector, IPTypes # Global defaults connector = Connector( ip_type=IPTypes.PUBLIC, enable_iam_auth=False, timeout=30 ) # Connection 1: Use global defaults conn1 = connector.connect( "my-project:us-central1:instance1", "pymysql", user="user1", password="pass1", db="db1" ) # Connection 2: Override multiple settings conn2 = connector.connect( "my-project:us-central1:instance2", "pg8000", user="postgres-iam-user@gmail.com", db="db2", ip_type=IPTypes.PRIVATE, enable_iam_auth=True, timeout=60 ) # Connection 3: Override specific settings conn3 = connector.connect( "my-project:us-central1:instance3", "asyncpg", user="user3", password="pass3", db="db3", timeout=120, enable_iam_auth=False ) ``` -------------------------------- ### Use create_async_connector for Automatic Event Loop Detection Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Use the `create_async_connector()` function to automatically detect and use the current running event loop for async operations. This simplifies setup in async contexts. ```python import asyncio from google.cloud.sql.connector import create_async_connector async def main(): connector = await create_async_connector() # Connector automatically uses current running event loop asyncio.run(main()) ``` -------------------------------- ### asyncpg Driver Connect Function Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Async helper function to create an asyncpg connection object using a pre-configured SSL context. It handles socket/SSL context setup and driver-specific parameter formatting. ```APIDOC ## asyncpg Driver Connect Function ### Description Async helper function to create an asyncpg connection object using a pre-configured SSL context. It handles socket/SSL context setup and driver-specific parameter formatting. ### Method connect ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **ip_address** (str) - Required - IP address of the Cloud SQL instance. - **ctx** (ssl.SSLContext) - Required - Pre-configured SSL context for secure connections. - **kwargs** (Any) - Optional - Additional asyncpg-specific parameters. Common parameters: `user`, `password`, `db`. ### Returns `asyncpg.Connection`: An asyncpg connection object to the Cloud SQL instance. ### Raises None explicitly documented for this function signature. ### Special Handling None explicitly documented for this function signature. ### Example No direct example provided for the standalone `connect` function, but it's used internally by the `Connector.connect` method. ### Installation ```bash pip install "cloud-sql-python-connector[asyncpg]" ``` ``` -------------------------------- ### Set Environment Variables for Connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Configure project ID for quota and billing, universe domain, and service account key path using environment variables. These can be overridden by constructor parameters. ```bash # Linux/macOS export GOOGLE_CLOUD_QUOTA_PROJECT=my-billing-project export GOOGLE_CLOUD_UNIVERSE_DOMAIN=my-universe.com export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account-key.json # Windows (PowerShell) $env:GOOGLE_CLOUD_QUOTA_PROJECT = "my-billing-project" $env:GOOGLE_CLOUD_UNIVERSE_DOMAIN = "my-universe.com" $env:GOOGLE_APPLICATION_CREDENTIALS = "C:\path\to\service-account-key.json" ``` -------------------------------- ### Load Custom Credentials from Environment Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/configuration.md Use custom Google credentials by loading them from the environment. This method relies on `google-auth` to find and apply credentials automatically. ```python from google.cloud.sql.connector import Connector import google.auth # Load credentials from environment credentials, project = google.auth.default( scopes=["https://www.googleapis.com/auth/sqlservice.admin"] ) connector = Connector(credentials=credentials) ``` -------------------------------- ### pg8000 Driver Connect Function Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Helper function to create a pg8000 DB-API connection object using a pre-configured SSL socket. It handles socket/SSL context setup and driver-specific parameter formatting. ```APIDOC ## pg8000 Driver Connect Function ### Description Helper function to create a pg8000 DB-API connection object using a pre-configured SSL socket. It handles socket/SSL context setup and driver-specific parameter formatting. ### Method connect ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **ip_address** (str) - Required - IP address of the Cloud SQL instance. - **sock** (ssl.SSLSocket) - Required - Pre-configured SSL socket for the connection. - **kwargs** (Any) - Optional - Additional pg8000-specific parameters. Common parameters: `user`, `password`, `db`. ### Returns `pg8000.dbapi.Connection`: A pg8000 connection object to the Cloud SQL instance. ### Raises - **ImportError**: If the pg8000 module is not installed. ### Special Handling - `user` parameter is extracted and passed separately (required) - `db` parameter is renamed to `database` for pg8000 compatibility - `password` parameter defaults to None if not provided (supports passwordless connections for IAM authentication) - Socket is passed directly for TLS/SSL connections ### Example ```python from google.cloud.sql.connector import Connector connector = Connector() # Using Connector.connect (recommended) conn = connector.connect( "my-project:us-central1:my-postgres-instance", "pg8000", user="my-user", password="my-password", db="my-database" ) # Using IAM authentication (no password needed) conn = connector.connect( "my-project:us-central1:my-postgres-instance", "pg8000", user="postgres-iam-user@gmail.com", db="my-database", enable_iam_auth=True ) ``` ### Installation ```bash pip install "cloud-sql-python-connector[pg8000]" ``` ``` -------------------------------- ### PyMySQL Driver Connect Function Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/drivers.md Helper function to create a PyMySQL DB-API connection object using a pre-configured SSL socket. It handles socket/SSL context setup and driver-specific parameter formatting. ```APIDOC ## PyMySQL Driver Connect Function ### Description Helper function to create a PyMySQL DB-API connection object using a pre-configured SSL socket. It handles socket/SSL context setup and driver-specific parameter formatting. ### Method connect ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **ip_address** (str) - Required - IP address of the Cloud SQL instance. - **sock** (ssl.SSLSocket) - Required - Pre-configured SSL socket for the connection. - **kwargs** (Any) - Optional - Additional PyMySQL-specific parameters. Common parameters: `user`, `password`, `db`, `connect_timeout`. ### Returns `pymysql.connections.Connection`: A PyMySQL connection object to the Cloud SQL instance. ### Raises - **ImportError**: If the pymysql module is not installed. ### Special Handling - `password` parameter defaults to None if not provided (supports passwordless connections for IAM authentication) - `timeout` parameter is renamed to `connect_timeout` for PyMySQL compatibility - Socket is passed directly for TLS/SSL connections ### Example ```python from google.cloud.sql.connector import Connector import pymysql connector = Connector() # Using Connector.connect (recommended) conn = connector.connect( "my-project:us-central1:my-mysql-instance", "pymysql", user="my-user", password="my-password", db="my-database", charset="utf8mb4" ) # Using the pymysql module directly import ssl from google.cloud.sql.connector.pymysql import connect as pymysql_connect ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) # ... configure context with certificates ... sock = ctx.wrap_socket( socket.create_connection(("192.0.2.1", 3307)), server_hostname="192.0.2.1" ) conn = pymysql_connect( "192.0.2.1", sock, user="my-user", password="my-password", db="my-database" ) ``` ### Installation ```bash pip install "cloud-sql-python-connector[pymysql]" ``` ``` -------------------------------- ### Correct Connector Initialization with create_async_connector Source: https://github.com/googlecloudplatform/cloud-sql-python-connector/blob/main/_autodocs/api-reference/exceptions.md Shows the correct way to initialize a Connector for asynchronous operations by using create_async_connector, which ensures the Connector is created with the current running event loop, avoiding ConnectorLoopError. ```Python import asyncio from google.cloud.sql.connector import create_async_connector async def correct_loop(): # Connector is created with the running event loop connector = await create_async_connector() try: conn = await connector.connect_async( "my-project:us-central1:my-instance", "asyncpg", user="my-user", password="my-password", db="my-database" ) # Success! finally: await connector.close_async() asyncio.run(correct_loop()) ```