The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
Project description
Cloud SQL Connector for Python Drivers
Warning: This project is currently in beta. Please open an issue if you would like to report a bug or documentation issue, request a feature, or have a question.
The Cloud SQL Python Connector is a library that can be used alongside a database driver to allow users with sufficient permissions to connect to a Cloud SQL database without having to manually allowlist IPs or manage SSL certificates.
Currently supported drivers are
Supported Python Versions
Currently Python versions >= 3.6 are supported.
Authentication
This library uses the Application Default Credentials to authenticate the connection to the Cloud SQL server. For more details, see the previously mentioned link.
To activate credentials locally, use the following gcloud
command:
gcloud auth application-default login
How to install this connector
Install latest release from PyPI
Upgrade to the latest version of pip
, then run the following command, replacing driver
with one of the driver names listed above.
pip install cloud-sql-python-connector[driver]
For example, to use the Python connector with pymysql
, run pip install cloud-sql-python-connector[pymysql]
Install dev version
Clone this repo, cd
into the cloud-sql-python-connector
directory then run the following command to install the package:
pip install .
Conversely, install straight from Github using pip
:
pip install git+https://github.com/GoogleCloudPlatform/cloud-sql-python-connector
How to use this connector
To use the connector: import the connector by including the following statement at the top of your Python file:
from google.cloud.sql.connector import connector
Use the connector to create a connection object by calling the connect method. Input your connection string as the first positional argument and the name of the database driver for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database. You can also set the optional timeout
or ip_type
keyword arguments.
conn = connector.connect(
"project:region:instance",
"pymysql",
user="root",
password="shhh",
db="your-db-name"
... insert other kwargs ...
)
The returned DB-API 2.0 compliant connection object can then be used to query and modify the database:
# Execute a query
cursor = conn.cursor()
cursor.execute("SELECT * from my_table")
# Fetch the results
result = cursor.fetchall()
# Do something with the results
for row in result:
print(row)
To use this connector with SQLAlchemy, use the creator
argument for sqlalchemy.create_engine
:
def getconn() -> pymysql.connections.Connection:
conn: pymysql.connections.Connection = connector.connect(
"project:region:instance",
"pymysql",
user="root",
password="shhh",
db="your-db-name"
)
return conn
engine = sqlalchemy.create_engine(
"mysql+pymysql://",
creator=getconn,
)
Note for SQL Server users: If your SQL Server instance requires SSL, you need to download the CA certificate for your instance and include cafile={path to downloaded certificate}
and validate_host=False
. This is a workaround for a known issue.
Specifying Public or Private IP
The Cloud SQL Connector for Python can be used to connect to Cloud SQL instances using both public and private IP addresses. To specify which IP address to use to connect, set the ip_type
keyword argument Possible values are IPTypes.PUBLIC
and IPTypes.PRIVATE
.
Example:
connector.connect(
"project:region:instance",
"pymysql",
ip_types=IPTypes.PRIVATE # Prefer private IP
... insert other kwargs ...
)
Note: If specifying Private IP, your application must already be in the same VPC network as your Cloud SQL Instance.
IAM Authentication
Connections using Automatic IAM database authentication are supported when using the Postgres driver. This feature is unsupported for other drivers. If automatic IAM authentication is not supported for your driver, you can use Manual IAM database authentication to connect.
First, make sure to configure your Cloud SQL Instance to allow IAM authentication and add an IAM database user.
Now, you can connect using user or service account credentials instead of a password.
In the call to connect, set the enable_iam_auth
keyword argument to true and user
to the email address associated with your IAM user.
Example:
connector.connect(
"project:region:instance",
"pg8000",
user="postgres-iam-user@gmail.com",
db="my_database",
enable_iam_auth=True,
)
SQL Server Active Directory Authentication
Active Directory authentication for SQL Server instances is currently only supported on Windows. First, make sure to follow these steps to set up a Managed AD domain and join your Cloud SQL instance to the domain. See here for more info on Cloud SQL Active Directory integration.
Once you have followed the steps linked above, you can run the following code to return a connection object:
connector.connect(
"project:region:instance",
"pytds",
db="my_database",
active_directory_auth=True,
server_name="public.[instance].[location].[project].cloudsql.[domain]",
)
Or, if using Private IP:
connector.connect(
"project:region:instance",
"pytds",
db="my_database",
active_directory_auth=True,
server_name="private.[instance].[location].[project].cloudsql.[domain]",
ip_types=IPTypes.PRIVATE
)
Support policy
Major version lifecycle
This project uses semantic versioning, and uses the following lifecycle regarding support for a major version:
Active - Active versions get all new features and security fixes (that wouldn’t otherwise introduce a breaking change). New major versions are guaranteed to be "active" for a minimum of 1 year. Deprecated - Deprecated versions continue to receive security and critical bug fixes, but do not receive new features. Deprecated versions will be publicly supported for 1 year. Unsupported - Any major version that has been deprecated for >=1 year is considered publicly unsupported.
Release cadence
This project aims for a minimum monthly release cadence. If no new features or fixes have been added, a new PATCH version with the latest dependencies is released.
Contributing
We welcome outside contributions. Please see our Contributing Guide for details on how best to contribute.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file cloud-sql-python-connector-0.4.1.tar.gz
.
File metadata
- Download URL: cloud-sql-python-connector-0.4.1.tar.gz
- Upload date:
- Size: 19.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fe793e5866df39552a6e4d15ed9428a6f736a0b30c624d6adfe51f258b1b935 |
|
MD5 | ee7d4e1389ff9c3c1664108dc91e5477 |
|
BLAKE2b-256 | d6b5c483536853bbcb89b58c9d6c65c20b8ec7d7f419c9bebb3d85d7cdc916eb |
Provenance
File details
Details for the file cloud_sql_python_connector-0.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: cloud_sql_python_connector-0.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf68d04abae6238f8d559b4afc27c706c2f750c52b68f3214233c64d7f2256a6 |
|
MD5 | eb8b72f5654b4dd6e7bbd1ef7607b8b1 |
|
BLAKE2b-256 | e2f5544d8376c9f3bd3c4d0e3c9096c07ef6c93af829118a0eab7af4f7f8a997 |