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:
- 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
# 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.