google-cloud-platformgoogle-cloud-sqlcloud-sql-proxy

How to Connect to Cloud SQL using Python?


Connecting to Cloud SQL using Python is not always straightforward.

Depending on the context, sometimes you have to connect to a Unix domain socket, allow-list IP addresses for TCP connections, run the Cloud SQL Auth proxy locally. Making these connections secure is yet another challenge: you might have to manage SSL certificates, firewalls rules, IP addresses, etc.

Is there a recommended way to Connect to Cloud SQL in a secure and easy way using Python?


Solution

  • Yes there indeed is, the Cloud SQL Python Connector, a Python package that makes connecting to Cloud SQL both easy and secure for all three supported database engines (Postgres, MySQL, and SQL Server), from anywhere (local machine, Cloud Run, App Engine, Cloud Functions, etc.)

    The Python Connector is one of the Cloud SQL connector libraries (also available in Java, Go, and Node).

    How is a connector different from the other methods?

    The Cloud SQL connector libraries provide the following benefits:

    How do I use the Python Connector ... what does the code look like?

    Basic Usage (using SQLAlchemy)

    from google.cloud.sql.connector import Connector, IPTypes
    import sqlalchemy
    
    # initialize Cloud SQL Connector
    connector = Connector()
    
    # SQLAlchemy database connection creator function
    def getconn():
        conn = connector.connect(
            "project:region:instance-name", # Cloud SQL Instance Connection Name
            "pg8000",
            user="my-user",
            password="my-password",
            db="my-db-name",
            ip_type=IPTypes.PUBLIC # IPTypes.PRIVATE for private IP
        )
        return conn
    
    # create SQLAlchemy connection pool
    pool = sqlalchemy.create_engine(
        "postgresql+pg8000://",
        creator=getconn,
    )
    
    # interact with Cloud SQL database using connection pool
    with pool.connect() as db_conn:
        # query database
        result = db_conn.execute("SELECT * from my_table").fetchall()
    
        # Do something with the results
        for row in result:
            print(row)
    
    # close Cloud SQL Connector
    connector.close()
    

    There are interactive "Getting Started" Colab notebooks that show you how to use the Cloud SQL Python Connector – all without needing to write a single line of code yourself! The notebooks will automatically use a supported database driver based on the database engine you are using with Cloud SQL.

    Does it work with popular web frameworks?

    Yes, the Python Connector can easily be used in web frameworks such as Flask-SQLAlchemy (and Flask), FastAPI, etc.