Configure SSL/TLS certificates Stay organized with collections Save and categorize content based on your preferences.
This page describes how to enforce SSL/TLS encryption for an instance to ensurethat all connections are encrypted. You can alsolearn more abouthow Cloud SQL uses self-managed SSL/TLS certificatesto connect to Cloud SQL instances securely.
Overview
Cloud SQL creates a server certificateautomatically when you create your instance. We recommend that youenforce all connections to use SSL/TLS.
SQL Server only performs certificate verification when the client requestexplicitly specifies that it requires an encrypted connection. In this case theserver certificate must be installed on theclient machine. Otherwise, clients are able to freely connect with no additionalchanges to their connection strings or certificates, even if you configure theinstance withsslMode
set toENCRYPTED_ONLY
.For more information, see theEnable encrypted connections to the Database Enginesection in the SQL Server documentation.
If you enforce SSL for an instance, then the instance requires a restart.A restart might also be required after you change SSL/TLS certificates.When a restart is required, Cloud SQL automatically restarts the instance for you.The restart of an instance can incur downtime.Enforce SSL/TLS encryption
You can use theSSL modesetting to enforce SSL encryption in the following ways:
Allow both non-SSL/non-TLSand SSL/TLS connections. This is the default.
Only allow connections encrypted with SSL/TLS.
If you selectAllow non-SSL/non-TLS and SSL/TLS connections for yourCloud SQL instance, SSL/TLS connections areaccepted, as well as unencrypted and unsecure connections.If you do not require SSL/TLS for all connections, unencryptedconnections are still allowed. For this reason, if you are accessingyour instance using public IP, we strongly recommend that you enforce SSLfor all connections.
You can connect either directly to instances by using SSL/TLS certificates, oryou can connect by using theCloud SQL Auth ProxyorCloud SQL Connectors.If you connect by using Cloud SQL Auth Proxy or Cloud SQL Connectors, then the connectionsare automatically encrypted with SSL/TLS. With Cloud SQL Auth Proxy and Cloud SQL Connectors,client and server identities are also automatically verified regardless of theSSL mode setting.
Note: We recommend that you update instances usingSSL mode instead of using the legacyrequire-ssl
parameter.If you update the SSL enforcement configuration on an existing instance,make sure that the values don't conflict with therequire-ssl
parameter.Enforcing SSL ensures that all connections are encrypted.Note: The Cloud SQL Auth Proxy or Cloud SQL Connectors add anotherlayer of encryption besides SQL Server's built-in encryption.In other words, the connection between theProxy Client/Cloud SQL Connectors and the Proxy Server would be double-encrypted.Double encryption can negatively impact SQL Server performance. Wesuggest that customers consider carefully before enforcing SSL and usingCloud SQL Auth Proxy or Cloud SQL Connectors at the same time.To enable requiring SSL/TLS, do the following:
Console
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- ClickConnections from the SQL navigation menu.
- Select theSecurity tab.
- Select one of the following:
- Allow unencrypted network traffic (not recommended)
- Allow only SSL connections. This option only allows connections using SSL/TLS encryption.
gcloud
gcloud sql instances patchINSTANCE_NAME \ --ssl-mode=SSL_ENFORCEMENT_MODE
ReplaceSSL_ENFORCEMENT_MODE with one of the following options:
ALLOW_UNENCRYPTED_AND_ENCRYPTED
allows non-SSL/non-TLS and SSL/TLS connections. This is the default value.ENCRYPTED_ONLY
only allows connections encrypted with SSL/TLS.
Terraform
To enforce SSL/TLS encryption, use aTerraform resource:
resource "google_sql_database_instance" "sqlserver_instance" { name = "sqlserver-instance" region = "asia-northeast1" database_version = "SQLSERVER_2019_STANDARD" root_password = "INSERT-PASSWORD-HERE" settings { tier = "db-custom-2-7680" ip_configuration { ssl_mode = "ENCRYPTED_ONLY" } } # set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by # use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level. deletion_protection = false}
Apply the changes
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud Shell
- LaunchCloud Shell.
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Prepare the directory
Each Terraform configuration file must have its own directory (alsocalled aroot module).
- InCloud Shell, create a directory and a new file within that directory. The filename must have the
.tf
extension—for examplemain.tf
. In this tutorial, the file is referred to asmain.tf
.mkdirDIRECTORY && cdDIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created
main.tf
.Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
- Initialize Terraform. You only need to do this once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgrade
option:terraform init -upgrade
Apply the changes
- Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
terraform plan
Make corrections to the configuration as necessary.
- Apply the Terraform configuration by running the following command and entering
yes
at the prompt:terraform apply
Wait until Terraform displays the "Apply complete!" message.
- Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
Delete the changes
To delete your changes, do the following:
- To disable deletion protection, in your Terraform configuration file set the
deletion_protection
argument tofalse
.deletion_protection = "false"
- Apply the updated Terraform configuration by running the following command and entering
yes
at the prompt:terraform apply
Remove resources previously applied with your Terraform configuration by running the following command and entering
yes
at the prompt:terraform destroy
REST v1
Before using any of the request data, make the following replacements:
- PROJECT_ID: The project ID
- SSL_ENFORCEMENT_MODE: Use one of the following options:
ALLOW_UNENCRYPTED_AND_ENCRYPTED
: allows non-SSL/non-TLS and SSL/TLS connections.ENCRYPTED_ONLY
: only allows connections encrypted with SSL/TLS.
- INSTANCE_ID: The instance ID
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID
Request JSON body:
{ "settings": { "ipConfiguration": {"sslMode": "SSL_ENFORCEMENT_MODE"} }}
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Note: The following command assumes that you have logged in to thegcloud
CLI with your user account by runninggcloud init
orgcloud auth login
, or by usingCloud Shell, which automatically logs you into thegcloud
CLI . You can check the currently active account by runninggcloud auth list
.Save the request body in a file named
request.json
, and execute the following command:curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID"PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud
CLI with your user account by runninggcloud init
orgcloud auth login
. You can check the currently active account by runninggcloud auth list
.Save the request body in a file named
request.json
, and execute the following command:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID" | Select-Object -Expand ContentYou should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-20T21:30:35.667Z", "operationType": "UPDATE", "name": "OPERATION_ID", "targetId": "INSTANCE_ID", "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID", "targetProject": "PROJECT_ID"}
REST v1beta4
Before using any of the request data, make the following replacements:
- PROJECT_ID: The project ID
- SSL_ENFORCEMENT_MODE: Use one of the following options:
ALLOW_UNENCRYPTED_AND_ENCRYPTED
: allows non-SSL/non-TLS and SSL/TLS connections.ENCRYPTED_ONLY
: only allows connections encrypted with SSL/TLS.
- INSTANCE_ID: The instance ID
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID
Request JSON body:
{ "settings": { "ipConfiguration": {"sslMode": "SSL_ENFORCEMENT_MODE"} }}
To send your request, expand one of these options:
curl (Linux, macOS, or Cloud Shell)
Note: The following command assumes that you have logged in to thegcloud
CLI with your user account by runninggcloud init
orgcloud auth login
, or by usingCloud Shell, which automatically logs you into thegcloud
CLI . You can check the currently active account by runninggcloud auth list
.Save the request body in a file named
request.json
, and execute the following command:curl -X PATCH \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID"PowerShell (Windows)
Note: The following command assumes that you have logged in to thegcloud
CLI with your user account by runninggcloud init
orgcloud auth login
. You can check the currently active account by runninggcloud auth list
.Save the request body in a file named
request.json
, and execute the following command:$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method PATCH `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID" | Select-Object -Expand ContentYou should receive a JSON response similar to the following:
Response
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/INSTANCE_ID", "status": "PENDING", "user": "user@example.com", "insertTime": "2020-01-20T21:30:35.667Z", "operationType": "UPDATE", "name": "OPERATION_ID", "targetId": "INSTANCE_ID", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/operations/OPERATION_ID", "targetProject": "PROJECT_ID"}
Server certificates
Cloud SQL creates a server certificate automatically when you create yourinstance. As long as the server certificate is valid, you don't needto activelymanage your server certificate.Cloud SQL lets you select between three differentcertificate authority (CA) hierarchies.The CA hierarchy that you select becomes the server CA modeof the instance. If you're using per-instance CA as the server CA mode for yourinstance, then the server certificates have an expiration date of 10 years.If you're using shared CA or customer-managed CA as the server CA mode ofyour instance, then the server certificate has an expiration date of 1 year*.After the expiration date, the server certificate is no longer valid, and clientscan no longer establish a secure connection to your instance using that certificate.If a client is configured to verify the CA or verify the hostname in the servercertificate, then that client's connections to Cloud SQL instances withexpired server certificates will fail. To prevent disruption to client connections,rotate the server certificate before the certificate expires.You're periodically notified that the server certificate is nearing expiration.The notifications are sent the following number of days before the expirationdate: 90, 30, 10, 2, and 1.
* For customer-managed CA, the expiration date of your servercertificate might be shorter than 1 year if you selected ashorter expiration date for the validity period of your CA.
List and create server certificates
To view the details of your server certificates in the Google Cloud console,go to theConnections page and click theSecurity tab.
In the certificates table, you can see the following details:
- Certificate Status:Upcoming, Active, or Previous
- Upcoming: The certificate is available for use, but isn't active. To make the certificate active, use the rotate procedure.
- Active: The certificate is in use.
- Previous: The certificate is no longer in use. To make the certificate active, use the rollback procedure.
- Created: The date and time the certificate was created
- Expires: The date and time the certificate expires
Before the active certificate expires, you can create a new certificate manually.
Console
For instances that use self-signed server certificates (per-instance CA):
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- ClickConnections from the SQL navigation menu.
- Select theSecurity tab.
- Go to theManage server CA certificates section.
- Click to expandManage certificates.
- ClickCreate new CA certificate.
The new server CA certificate appears in theUpcoming slot. If you want to rotate to the new server CA certificate immediately, proceed withserver CA certificate rotation by updating your clients and completing the rotation.
For instances that use server certificates issued by a shared CA:
In the Google Cloud console, go to theCloud SQL Instances page.
- To open theOverview page of an instance, click the instance name.
- ClickConnections from the SQL navigation menu.
- Select theSecurity tab.
- Go to theManage server certificates section.
- Click to expandManage certificates.
- ClickCreate server certificate.
The new server certificate appears in theUpcoming slot. If you want to use the new server certificate immediately, proceed withserver certificate rotation by updating your clients and completing the rotation.
gcloud
For instances that use self-signed server certificates (per-instance CA):
- To get information about the server certificate, use thesql ssl server-ca-certs list command:
gcloudsqlsslserver-ca-certslist\--instance=INSTANCE_NAME
- To create a server certificate, use thesql ssl server-ca-certs create command:
gcloudsqlsslserver-ca-certscreate\--instance=INSTANCE_NAME
- Download the certificate information to a local PEM file:
gcloudsqlsslserver-ca-certslist\--format="value(cert)"\--instance=INSTANCE_NAME>\FILE_PATH/FILE_NAME.pem
- Update all of your clients to use the new information by copying the downloaded file to your client host machines, replacing the existing
server-ca.pem
files.
For instances that use server certificates issued by a shared CA:
- To get information about the server certificate, use thesql ssl server-certs list command:
gcloudsqlsslserver-certslist\--instance=INSTANCE_NAME
- To create a server certificate, use thesql ssl server-certs create command:
gcloudsqlsslserver-certscreate\--instance=INSTANCE_NAME
- Download the certificate information to a local PEM file:
gcloudsqlsslserver-certslist\--format="value(ca_cert.cert)"\--instance=INSTANCE_NAME>\FILE_PATH/FILE_NAME.pem
- Update all of your clients to use the new information by copying the downloaded file to your client host machines, replacing the existing
server-ca.pem
files.
Terraform
To provide server certificate information as an output, use aTerraform data source:
- Add the following to your Terraform configuration file:
data "google_sql_ca_certs" "ca_certs" { instance = google_sql_database_instance.default.name } locals { furthest_expiration_time = reverse(sort([for k, v in data.google_sql_ca_certs.ca_certs.certs : v.expiration_time]))[0] latest_ca_cert = [for v in data.google_sql_ca_certs.ca_certs.certs : v.cert if v.expiration_time == local.furthest_expiration_time] } output "db_latest_ca_cert" { description = "Latest CA certificate used by the primary database server" value = local.latest_ca_cert sensitive = true }
- To create the
server-ca.pem
file, run the following command:terraform output db_latest_ca_cert > server-ca.pem
Use encrypted connections
Learn more abouthow SQL Server uses encrypted connections.
Server identity verification
Server identity verification depends on the servercertificate authority (CA) hierarchyconfiguration of your Cloud SQL instance.
For instances that use a per-instance CA, verifying the CA alsoverifies the server identity since each instance has a unique CA.For instances that use a shared CA,verifying the hostname along with verifying the CA is required for serveridentity verification since server CAs are shared across instances.
If you have per-instance CA, then you can perform DNS name-based serveridentity verification only for instances that are configured withPrivate Service Connect.If you have a shared CA, then you canperform DNS name-based server identity verification for all types of instances,namelyPrivate Service Connect,private service access,and public IP instances.
If you're using a customer-managed CA, then you can verify theCA trust chain and perform DNS name-based server identity verificationfor any type of instance that usescustomer-managed CA for itsserverCAmode
.
When you select the customer-managed CA option for your instance, youcan insert custom DNS names in the SAN field ofthe server certificate. For more information,seeEdit a custom SAN field.
You can view which CA hierarchy is configured for a Cloud SQL instance byviewing instance details. For more information, seeView instance information.
Enable server identity verification
If you select shared CA as the server CA mode of your Cloud SQLinstance or if youset up custom DNS names using custom SAN values, then we recommend thatyou also enable server identity verification.
Instances that use shared CA asthe server CA mode contain the instance DNS name in theSubject Alternative Name (SAN) field of the server certificate. You can get thisDNS name by using the instance lookup API and using the response as a hostnamefor server identity verification. You need to set up DNS resolution for the DNSname.
To enable server identity verification for an instance that uses a shared CA,complete the following steps:
Retrieve the DNS name.
To view summary information about a Cloud SQL instance,including the DNS name of the instance, use the
gcloud sql instances describe
command:gcloudsqlinstancesdescribeINSTANCE_NAME\--project=PROJECT_ID
Make the following replacements:
- INSTANCE_NAME: the name of the Cloud SQL instance
- PROJECT_ID: the ID orproject numberof the Google Cloud project that contains the instance
In the response, look for the
dnsNames:
field.This field can return multiple DNS names, which have the following formats:Network configuration DNS name format Name level Private Service Connect orPublic IP address INSTANCE_UID.PROJECT_DNS_LABEL.REGION_NAME.sql.goog.
Example:1a23b4cd5e67.1a2b345c6d27.us-central1.sql.goog.
Instance Private services access INSTANCE_UID.PROJECT_DNS_LABEL.REGION_NAME.sql-psa.goog.
Example:1a23b4cd5e67.1a2b345c6d27.us-central1.sql-psa.goog.
Instance
Create the DNS record in a DNS zone.If you are connecting privately, thencreate the DNS record in aprivate DNS zonein the corresponding Virtual Private Cloud (VPC) network.
When you connect to the Cloud SQL for SQL Server instance, configure the DNS nameor IP address as the hostname. Then enable server identityverification by specifying the
-N
flag forsqlcmd
or by selecting theEncrypt Connection/Encryption option of SSMS.Other SQL Server drivers have similar flags or configurations.
What's next
- Manage SSL/TLS certificates on your Cloud SQL instance.
- Learn more abouthow encryption is handled in Google Cloud.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-07-18 UTC.