The Cloud SQL connectors are libraries that provide encryption and IAM-based authorization when connecting to a Cloud SQL instance. They can't provide a network path to a Cloud SQL instance if one is not already present.
Other ways to connect to a Cloud SQL instance include using a database client or the Cloud SQL Auth proxy. See the About connection options page for more information about connecting to a Cloud SQL instance.
This page discusses the following Cloud SQL connectors:
- The Cloud SQL Java connector
- The Cloud SQL Python connector (Open in Colab)
- The Cloud SQL Go connector
Benefits
Using a Cloud SQL connector provides the following benefits:
- IAM authorization: Uses identity and access management (IAM) permissions to control who or what can connect to your Cloud SQL instances.
- Convenience: Removes the requirement to manage SSL certificates, configure firewall rules, or enable authorized networks.
Before you begin
- Enable the Cloud SQL Admin API.
- Create a Cloud SQL instance, including configuring the default user.
For more information about creating instances, see Create instances.
For more information about configuring the default user, see Set the password for the default user account.
- Configure the roles and permissions required to connect to a Cloud SQL instance.
Setup
Java
The Cloud SQL Java connector is a library that provides IAM-based authorization and encryption when connecting to a Cloud SQL instance. It can not provide a network path to a Cloud SQL instance if one is not already present.
Installation
For instructions on building and using the drivers for JDBC and R2DBC with the Cloud SQL Java connector, see the following links:
JDBC: Connecting to SQL Server using JDBC.
R2DBC: Connecting to SQL Server using R2DBC.
For examples of this library being used in the context of an application, check out these sample applications.
Authentication
This library uses Application Default Credentials to authenticate the connection to the Cloud SQL server.
To activate credentials locally, use the following gcloud command:
gcloud auth application-default login
Connect with Intellij
In order to connect IntelliJ to your Cloud SQL instance, you will need to add the library as a jar with dependencies in the Additional Files section on the driver settings page. For example, prebuilt fat jars can be found on the Cloud SQL Java connector Releases page for this purpose.
Python
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.
For interactive examples of using the Cloud SQL Python connector, open the Cloud SQL Python connector notebook.
The currently supported driver for SQL Server is pytds .
Installation
To install the latest release, follow these instructions.
Authentication
This library uses Application Default Credentials to authenticate the connection to the Cloud SQL server.
To activate credentials locally, use the following gcloud command:
gcloud auth application-default login
Go
The Cloud SQL Go connector is a Cloud SQL connector designed for use with the Go language. For improved security, this connector uses robust, manually authenticated TLS 1.3 encryption between the client connector and the server-side proxy, independent of the database protocol.
Installation
You can install this repo with go get
:
go get cloud.google.com/go/cloudsqlconn
Usage
Java
To see this snippet in the context of a web application, view the README on GitHub.
Python
See How to use this Connector for detailed instructions on using the library. View example connection test code on GitHub.
Go
See Usage for detailed instructions on using the library. View example connection test code on GitHub.
Troubleshoot
Driver versions
Make sure you are using the latest version of the Cloud SQL connectors and your database driver to avoid incompatibilities. Some older versions of drivers are not supported
Connection paths
The Cloud SQL connectors provide authorization for connections, but they don't provide new paths to connectivity. For example, in order to connect to a Cloud SQL instance using a Private IP address, your application must already have VPC access.
Debug connection issues
For additional help with connection issues, see the Troubleshoot and Debug connection issues pages.
What's next
- See the Cloud SQL Java connector home page on GitHub.
- See the Cloud SQL Python connector home page on GitHub.
- Learn more about the Cloud SQL Auth proxy.
- Learn more about Identity and Access Management (IAM).
- Learn about options for support.