简体   繁体   中英

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?

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 and Go ).

How is a connector different from the other methods?

The Cloud SQL connector libraries provide the following benefits :

  • IAM Authorization : the connectors use IAM permissions to control who and what can connect to your Cloud SQL instances.
  • Improved Security : the connectors use robust, updated TLS 1.3 encryption and identity verification between the client connector and the server-side proxy, independent of the database protocol.
  • Convenience : the connectors remove the requirement to use and distribute SSL certificates, manage firewalls or source/destination IP addresses.
  • IAM Database Authentication (optional): the connectors provide support for Cloud SQL's automatic IAM database authentication feature .

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

# Python Connector database creator function
def getconn():
    with Connector() as connector:
        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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM