Stay organized with collections Save and categorize content based on your preferences.
This page contains information and examples for connecting to a Cloud SQL instance from a service running in Cloud Run.
For step-by-step instructions on running a Cloud Run sample web application connected to Cloud SQL, see the quickstart for connecting from Cloud Run.
Cloud SQL is a fully-managed database service that helps you set up, maintain, manage, and administer your relational databases in the cloud.
Cloud Run is a managed compute platform that lets you run containers directly on top of Google Cloud infrastructure.
Set up a Cloud SQL instanceBy default, Cloud SQL assigns a public IP address to a new instance. You also have the option to assign a private IP address. For more information about the connectivity options for both, see the Connecting Overview page.
serverCaMode
for the instance. You must select the per-instance CA option (GOOGLE_MANAGED_INTERNAL_CA
) as the server CA mode for instances that you want to connect to from web applications.The steps to configure Cloud Run depend on the type of IP address that you assigned to your Cloud SQL instance. If you route all egress traffic through Direct VPC egress or a Serverless VPC Access connector, use a private IP address.
Compare the two network egress methods.
Public IP (default)gcloud sql instances describe
command:
gcloud sql instances describe INSTANCE_NAMEReplace INSTANCE_NAME with the name of your Cloud SQL instance.
gcloud run services describe
command in the project that's hosting the Cloud Run service:
gcloud run services describe CLOUD_RUN_SERVICE_NAME --region CLOUD_RUN_SERVICE_REGION --format="value(spec.template.spec.serviceAccountName)"Replace the following variables:
Cloud SQL Client
IAM role.If you're connecting to instances that are configured with the shared certificate authority (CA) (GOOGLE_MANAGED_CAS_CA
) option or the customer-managed CA (CUSTOMER_MANAGED_CAS_CA
) option as the server CA mode, then select the second generation execution environment when you select the execution environment for the service. Both server CA mode options require you to connect to the instance with the Cloud SQL Auth Proxy v2.
If your service runs in a first generation execution environment, then you can connect only to Cloud SQL instances that are configured with the per-instance certificate authority (CA) option (GOOGLE_MANAGED_INTERNAL_CA
) as the server CA mode. The first generation execution environment of Cloud Run embeds the Cloud SQL Auth Proxy v1. For more information about connection requirements to Cloud SQL for the Cloud SQL Auth Proxy, see Requirements for using the Cloud SQL Auth Proxy.
Like any configuration change, setting a new configuration for the Cloud SQL connection leads to the creation of a new Cloud Run revision. Subsequent revisions will also automatically get this Cloud SQL connection unless you make explicit updates to change it.
ConsoleStart configuring the service. To add Cloud SQL connections to an existing service, do the following:
Click Create or Deploy.
Before using any of the following commands, make the following replacements:
INSTANCE_CONNECTION_NAME with the instance connection name of your Cloud SQL instance, or a comma delimited list of connection names.
If you're deploying a new container, use the following command:
gcloud run deploy \ --image=IMAGE \ --add-cloudsql-instances=INSTANCE_CONNECTION_NAMEIf you're updating an existing service, use the following command:
gcloud run services update SERVICE_NAME \ --add-cloudsql-instances=INSTANCE_CONNECTION_NAME
The following code creates a base Cloud Run container, with a connected Cloud SQL instance.
terraform apply
.If the authorizing service account belongs to a different project than the one containing the Cloud SQL instance, do the following:
5432
.After you configure Cloud Run, you can connect to your Cloud SQL instance.
Public IP (default) Warning: If you're using a first generation execution environment for your Cloud Run service, then you can connect only to a Cloud SQL instance that's configured with the per-instance certificate authority (CA) option (GOOGLE_MANAGED_INTERNAL_CA
) as its server CA mode. The first generation execution environment of Cloud Run embeds the Cloud SQL Auth Proxy v1. For more information about connection requirements to Cloud SQL for the Cloud SQL Auth Proxy, see Requirements for using the Cloud SQL Auth Proxy.
For public IP paths, Cloud Run provides encryption and connects using the Cloud SQL Auth Proxy in two ways:
Once correctly configured, you can connect your service to your Cloud SQL instance's Unix domain socket accessed on the environment's filesystem at the following path: /cloudsql/INSTANCE_CONNECTION_NAME
.
The INSTANCE_CONNECTION_NAME uses the format project:region:instance-id
. You can find it on the Overview page for your instance in the Google Cloud console or by running the following command:
gcloud sql instances describe [INSTANCE_NAME]
These connections are automatically encrypted without any additional configuration.
The code samples shown below are extracts from more complete examples on the GitHub site. Click View on GitHub
to see more.
.s.PGSQL.5432
suffix in the socket path. Some libraries apply this suffix automatically, but others require you to specify the socket path as follows:
/cloudsql/INSTANCE_CONNECTION_NAME/.s.PGSQL.5432Warning: Linux-based operating systems have a maximum socket path length of 108 characters. If the total length of the path exceeds this length, you cannot connect with a socket from Cloud Run. Connect with Cloud SQL connectors
The Cloud SQL connectors are language specific libraries that provide encryption and IAM-based authorization when connecting to a Cloud SQL instance.
PythonTo see this snippet in the context of a web application, view the README on GitHub.
JavaTo see this snippet in the context of a web application, view the README on GitHub.
Note:
To see this snippet in the context of a web application, view the README on GitHub.
Node.jsTo see this snippet in the context of a web application, view the README on GitHub.
Use Secret ManagerGoogle recommends that you use Secret Manager to store sensitive information such as SQL credentials. You can pass secrets as environment variables or mount as a volume with Cloud Run.
After creating a secret in Secret Manager, update an existing service, with the following command:
Command linegcloud run services update SERVICE_NAME \ --add-cloudsql-instances=INSTANCE_CONNECTION_NAME --update-env-vars=INSTANCE_CONNECTION_NAME=INSTANCE_CONNECTION_NAME_SECRET \ --update-secrets=DB_USER=DB_USER_SECRET:latest \ --update-secrets=DB_PASS=DB_PASS_SECRET:latest \ --update-secrets=DB_NAME=DB_NAME_SECRET:latestTerraform
The following creates secret resources to securely hold the database user, password, and name values using google_secret_manager_secret
and google_secret_manager_secret_version
. Note that you must update the project compute service account to have access to each secret.
Update the main Cloud Run resource to include the new secrets.
Apply the changes by entering terraform apply
.
The example command uses the secret version, latest; however, Google recommends pinning the secret to a specific version, SECRET_NAME:v1.
Private IPFor private IP paths, your application connects directly to your instance through a VPC network. This method uses TCP to connect directly to the Cloud SQL instance without using the Cloud SQL Auth Proxy.
Connect with TCPConnect using the private IP address of your Cloud SQL instance as the host and port 5432
.
You can use the Cloud SQL Auth Proxy when testing your application locally. See the quickstart for using the Cloud SQL Auth Proxy for detailed instructions.
You can also test using the Cloud SQL Proxy via a docker container.
Connection PoolsConnections to underlying databases may be dropped, either by the database server itself, or by the platform infrastructure. We recommend using a client library that supports connection pools that automatically reconnect broken client connections. For more detailed examples on how to use connection pools, see the
Managing database connectionspage.
Connection LimitsBoth the MySQL and PostgreSQL editions of Cloud SQL impose a maximum limit on concurrent connections, and these limits may vary depending on the database engine chosen (see the
Cloud SQL Quotas and Limitspage).
Cloud Run container instances are limited to 100 connections to a Cloud SQL database. Each instance of a Cloud Run service or job can have 100 connections to the database, and as this service or job scales, the total number of connections per deployment can grow.
You can limit the maximum number of connections used per instance by using a connection pool. For more detailed examples on how to limit the number of connections, see the Managing database connections page.
API Quota LimitsCloud Run provides a mechanism that connects using the Cloud SQL Auth Proxy, which uses the Cloud SQL Admin API.
API quota limitsapply to the Cloud SQL Auth Proxy. The Cloud SQL Admin API quota used is approximately two times the number of Cloud SQL instances configured by the number of Cloud Run instances of a particular service deployed at any one time. You can
cap or increase the number of Cloud Run instancesto modify the expected API quota consumed.
What's nextExcept as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-02 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-02 UTC."],[],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4