Skip to main content

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 alpha, and releases may contain breaking API changes.

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

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.

connector.connect(
    "your:connection:string:", 
    "pymysql",
    user="root",
    password="shhh",
    db="your-db-name"
... insert other kwargs ...
)

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(
    "your:connection:string:", 
    "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.

Setup for development

Tests can be run with nox. Change directory into the cloud-sql-python-connector and just run nox to run the tests.

  1. Create a MySQL instance on Google Cloud SQL. Make sure to note your root password when creating the MySQL instance.
  2. When the MySQL instance has finished creating, go to the overview page and set the instance’s connection string to the environment variable INSTANCE_CONNECTION_NAME using the following command:
export INSTANCE_CONNECTION_NAME=your:connection:string
  1. Enable SSL for your Cloud SQL instance by following these instructions.
  2. Create a service account with Cloud SQL Admin and Cloud SQL Client roles, then download the key and save it in a safe location. Set the path to the json file to the environment variable GOOGLE_APPLICATION_CREDENTIALS using the following command:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/auth/./json
  1. Enable the SQL Admin API.
  2. Clone the repository.
  3. Create a virtual environment and change directory into the cloud-sql-python-connector folder.
  4. Install the package by running the following command:
pip install .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloud-sql-python-connector-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

cloud_sql_python_connector-0.1.0-py2.py3-none-any.whl (19.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cloud-sql-python-connector-0.1.0.tar.gz.

File metadata

  • Download URL: cloud-sql-python-connector-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for cloud-sql-python-connector-0.1.0.tar.gz
Algorithm Hash digest
SHA256 09b71044bd73cc2a0c21cb3ba6fe680fb869ff203e48cbf3d122a294dee69a07
MD5 7ae68569d892ecf6e9874076a9671a62
BLAKE2b-256 7a2ff49ed2747e34f11fe74ae8e3e78f1dd96c0c9dd81c6d855bb1b4d2a48ce8

See more details on using hashes here.

Provenance

File details

Details for the file cloud_sql_python_connector-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cloud_sql_python_connector-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for cloud_sql_python_connector-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6d80ba04239818d1cffd803a7acc46c875e576e3e545f7b9abe84c3fe88df256
MD5 a6ef8cc6577413008c1490a3a898d5d6
BLAKE2b-256 4716bd2dea4ed3ac3fa6ebee78309ec2797c04de012d7bff63ac903b3e5b58f5

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page