Stay organized with collections Save and categorize content based on your preferences.
This page describes how to use Private Service Connect to connect to a Cloud SQL instance.
You can use Private Service Connect to connect to either a primary Cloud SQL instance or any of its read replicas from multiple Virtual Private Cloud (VPC) networks that belong to different groups, teams, projects, or organizations.
Note: If a Cloud SQL instance has Private Service Connect enabled, then thepostgres_fdw
, dblink
, Pl/Proxy
, and pglogical
PostgreSQL extensions can't be used with the instance. For more information about these extensions, see Configure PostgreSQL extensions. Before you begin
Support for using Private Service Connect with a Cloud SQL instance is available for gcloud CLI
versions 416.0.0 and later.
The following table provides information about the roles required to use Private Service Connect with a Cloud SQL instance:
Create a Private Service Connect endpointPrivate Service Connect endpoints are internal IP addresses in a consumer VPC network, which clients in that network can access directly. Clients can use these endpoints to connect to Cloud SQL instances.
You can either have Cloud SQL create a Private Service Connect endpoint automatically in your VPC or you can create the endpoint manually.
To have Cloud SQL create the Private Service Connect endpoint automatically, do the following:
To create the Private Service Connect endpoint manually, do the following:
Preview
This product is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of the Service Specific Terms. Pre-GA products are available "as is" and might have limited support. For more information, see the launch stage descriptions.
The next few sections explain how to configure your instance to let Cloud SQL create the Private Service Connect endpoint automatically.
Create a service connection policyA service connection policy lets you authorize a specified service class to create a Private Service Connect endpoint in your consumer VPC network. You can use the service connection policy to let Cloud SQL create Private Service Connect endpoints automatically.
You can create a service connection policy by using the Google Cloud console, gcloud CLI, or the API.
ConsoleIn the Google Cloud console, go to the Private Service Connect page.
Click the Connection Policies tab.
Click Create connection policy.
Enter a Name for the connection policy.
Specify the service class by doing the following:
google-cloud-sql
because Cloud SQL is the managed service for the connection policy.In the Target endpoints scope section, select a Network and Region to which this policy applies.
In the Policy section, select one or more subnets from the Subnetworks menu. The subnets are used to allocate IP addresses for endpoints.
Optional: Specify a Connection limit for the policy. The limit determines how many endpoints can be created by using this connection policy. If you don't specify a connection limit, then there's no limit.
Click Create policy.
To create a service connection policy, use the service-connection-policies create
command.
gcloud network-connectivity service-connection-policies create POLICY_NAME \ --network=NETWORK \ --project=PROJECT_ID \ --region=REGION \ --service-class=SERVICE_CLASS \ --subnets=https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/subnetworks/SUBNETS \ --psc-connection-limit=LIMIT \ --description="DESCRIPTION" \ --producer-instance-location=PRODUCER_INSTANCE_LOCATION \ --allowed-google-producers-resource-hierarchy-level=RESOURCE_HIERARCHY_LEVEL
Replace the following:
POLICY_NAME
: the name of your service connection policy.NETWORK
: the network to which this policy applies.PROJECT_ID
: the project ID or number of the VPC network's project. For Shared VPC networks, you must deploy service connection policies in the host project because these policies aren't supported in service projects.REGION
: the region to which this policy applies. The same policy must exist for every region in which you want to automate service connectivity.SERVICE_CLASS
: the producer-supplied resource identifier of the service class. For Cloud SQL, the service class is google-cloud-sql
.SUBNETS
: one or more regular consumer subnets that are used to allocate IP addresses for Private Service Connect endpoints. These IP addresses are allocated automatically and returned to the subnet's pool as managed service instances are created and deleted. The subnets must be in the same region as the service connection policy. If multiple connection policies share the same region, then you can reuse the same subnetwork in these policies. You can enter multiple subnets in a comma-separated list.LIMIT
: the maximum number of endpoints that you can create by using this policy. If you don't specify a limit, then there's no limit.DESCRIPTION
: an optional description of the service connection policy.PRODUCER_INSTANCE_LOCATION
: use this optional flag to specify whether to authorize a custom hierarchy of the locations for a Cloud SQL instance. You can set the value of PRODUCER_INSTANCE_LOCATION
to only one of the following:
custom-resource-hierarchy-levels
: the instance must be located in one of the projects, folders, or organizations that you provide as a value for the allowed-google-producers-resource-hierarchy-level
parameter.none
: the instance is in the same project as the service connection policy.RESOURCE_HIERARCHY_LEVEL
: a list of projects, folders, or organizations where the instance is located. This list is in the form of projects/PROJECT_ID
, folders/FOLDER_ID
, or organizations/ORGANIZATION_ID
.
For example, the following command creates a service connection policy for the google-cloud-sql
service class that allocates IP addresses from the managed-services
subnet. A maximum of 10 Private Service Connect endpoints can be created by using this policy. The endpoints must be created in projects that are in the same organization as the managed service instance. The Cloud SQL instance is located in the myproject
project.
gcloud network-connectivity service-connection-policies create cloud-sql-policy \ --network=default \ --project=my-project \ --region=us-central1 \ --service-class=google-cloud-sql \ --subnets=managed-service-subnet \ --psc-connection-limit=10 \ --producer-instance-location=custom-resource-hierarchy-levels \ --allowed-google-producers-resource-hierarchy-level=projects/myprojectREST
Before using any of the request data, make the following replacements:
PROJECT_ID
: the ID of your project.REGION
: the region of your service connection policy.POLICY_NAME
: the name of your service connection policy.DESCRIPTION
: an optional description of your service connection policy.NETWORK
: the network of your service connection policy.LIMIT
: the maximum number of endpoints that you can create by using this policy. If you don't specify a limit, then there's no limit.SUBNETS
: one or more regular consumer subnets that are used to allocate IP addresses for Private Service Connect endpoints. These IP addresses are allocated automatically and returned to the subnet's pool as managed service instances are created and deleted. The subnets must be in the same region as the service connection policy. If multiple connection policies share the same region, then you can reuse the same subnetwork in these policies. You can enter multiple subnets in a comma-separated list.SERVICE_CLASS
: the producer-supplied resource identifier of the service class.HTTP method and URL:
POST https://networkconnectivity.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/serviceConnectionPolicies?serviceConnectionPolicyId=POLICY_NAME
Request JSON body:
{ "description": "DESCRIPTION", "network": "projects/PROJECT_ID/global/networks/NETWORK", "pscConfig": { "limit": "LIMIT", "subnetworks": [ "projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET" ] }, "serviceClass": "SERVICE_CLASS" }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://networkconnectivity.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/serviceConnectionPolicies?serviceConnectionPolicyId=POLICY_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://networkconnectivity.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/serviceConnectionPolicies?serviceConnectionPolicyId=POLICY_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_ID/locations/REGION/operations/OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.networkconnectivity.v1.OperationMetadata", "createTime": "2023-08-15T16:59:29.236110917Z", "target": "projects/PROJECT_ID/locations/REGION/serviceConnectionPolicies/POLICY_NAME", "verb": "create", "requestedCancellation": false, "apiVersion": "v1" }, "done": false }Create a Cloud SQL instance
You can create an instance with Private Service Connect enabled for the instance and configure the instance to create endpoints automatically by using gcloud CLI or the API.
Note: After you create the instance, a Private Service Connect endpoint is automatically created in the VPC networks that you specify. However, the endpoint might not be created because of reasons such as the specified network doesn't exist, there's no valid service connection policy, or there aren't any available IP addresses. If this occurs, then the endpoint won't be created. Optionally, you can create Private Service Connect endpoints manually. gcloudTo create an instance with Private Service Connect enabled for the instance, use the gcloud sql instances create
command:
gcloud sql instances create INSTANCE_NAME \ --project=PROJECT_ID \ --region=REGION_NAME \ --enable-private-service-connect \ --allowed-psc-projects=ALLOWED_PROJECTS \ --availability-type=AVAILABILITY_TYPE \ --no-assign-ip \ --tier=MACHINE_TYPE \ --database-version=DATABASE_VERSION \ --psc-auto-connections=network=CONSUMER_NETWORK,project=CONSUMER_PROJECT
Make the following replacements:
ALLOWED_PROJECTS: a comma-separated list of allowed project IDs or numbers from where Private Service Connect endpoints can connect to Cloud SQL instances.
If a project isn't contained in this list, then you can't create Private Service Connect endpoints in the project to connect to the instance.
REGIONAL
: enables high availability and is recommended for production instances. The instance fails over to another zone within your selected region.ZONAL
: provides no failover capability. This is the default value.For more information about setting and removing high availability for instances, see Configure an existing instance for high availability and Deactivate high availability for an instance.
POSTGRES_13
).projects/my-host-project/global/networks/default
.CONSUMER_PROJECT: the project where the Private Service Connect endpoint is created. If you're using a Shared VPC network, then this can be either the host project or the service project.
Any projects that you specify in the auto-connection parameters are added to your allowed projects automatically. Optionally, for any projects where you want to create Private Service Connect endpoints manually, you can add these projects to your list of allowed projects.
Before using any of the request data, make the following replacements:
REGIONAL
: enables high availability and is recommended for production instances. The instance fails over to another zone within your selected region.ZONAL
: provides no failover capability. This is the default value.For more information about setting and removing high availability for instances, see Configure an existing instance for high availability and Deactivate high availability for an instance.
ALLOWED_PROJECTS: a comma-separated list of allowed project IDs or numbers from where Private Service Connect endpoints can connect to Cloud SQL instances.
If a project isn't contained in this list, then you can't create Private Service Connect endpoints in the project to connect to the instance.
CONSUMER_PROJECT: Optional. Only specify this if CONSUMER_NETWORK is a Shared VPC network and you want to allow automatic creation of Private Service Connect endpoints in a service project.
Any projects that you specify in the auto-connection parameters are added to your allowed projects automatically. Optionally, for any projects where you want to create Private Service Connect endpoints manually, you can add these projects to your list of allowed projects.
HTTP method and URL:
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances
Request JSON body:
{ "name": "INSTANCE_NAME", "project": PROJECT_ID", "region": "REGION_NAME", "databaseVersion": "POSTGRES_13", "kind": "sql#instance", "settings": { "availabilityType": "AVAILABILITY_TYPE", "ipConfiguration": { "ipv4Enabled": false, "pscConfig": { "allowedConsumerProjects": [ "ALLOWED_PROJECTS" ], "pscAutoConnections": [ { "consumerProject":"CONSUMER_PROJECT", "consumerNetwork":"projects/PARENT_PROJECT/global/networks/CONSUMER_NETWORK" } ], "pscEnabled": true } }, "kind": "sql#settings", "pricingPlan": "PER_USE", "replicationType": "SYNCHRONOUS", "tier": "MACHINE_TYPE" } }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-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"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME", "status": "RUNNING", "user": "user@example.com", "insertTime": "2020-01-16T02:32:12.281Z", "startTime": "2023-06-14T18:48:35.499Z", "operationType": "CREATE", "name": "OPERATION_ID", "targetId": "INSTANCE_NAME", "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID", "targetProject": "PROJECT_ID" }Retrieve the endpoint
By retrieving the internal IP address, which is the Private Service Connect endpoint for an instance, you can use this endpoint to connect to the instance.
gcloudTo view information about an instance, including the IP address that's the Private Service Connect endpoint for the instance, use the gcloud sql instances describe
command:
gcloud sql instances describe INSTANCE_NAME \ --project=PROJECT_ID \ --format='json(settings.ipConfiguration.pscConfig.pscAutoConnections)'
Make the following replacements:
In the response, note the value that appears next to the pscConfig:pscAutoConnections:ipAddress
field. This value is the internal IP address that's also the Private Service Connect endpoint for the instance.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "sql#instance", "state": "RUNNABLE", "databaseVersion": "POSTGRES_13", "settings": { "authorizedGaeApplications": [], "tier": "db-custom-2-7680", "kind": "sql#settings", "availabilityType": "REGIONAL", "pricingPlan": "PER_USE", "replicationType": "SYNCHRONOUS", "activationPolicy": "ALWAYS", "ipConfiguration": { "authorizedNetworks": [], "pscConfig": { "allowedConsumerProjects": [ "ALLOWED_PROJECTS" ], "pscAutoConnections": { consumerNetwork:"projects/PARENT_PROJECT/global/networks/CONSUMER_NETWORK", consumerNetworkStatus:"CONSUMER_NETWORK_STATUS", consumerProject:"CONSUMER_PROJECT", ipAddress:"IP_ADDRESS", status:"STATUS" }, "pscEnabled": true }, "ipv4Enabled": false }, }
The following fields exist for instances that have Private Service Connect enabled for them:
allowedConsumerProjects
: a list of the allowed projects for the instance. You can create Private Service Connect endpoints from any VPC networks in these projects to the service attachment of the instance.pscAutoConnections
: the allowed VPC network, the status of the service connection policy, and the status of the IP address that's the endpoint for the instance.pscEnabled
: whether an instance has Private Service Connect enabled for it.To see how to construct the underlying REST API request for this task, see the instances:get page.
Create the endpoint manuallyThe next few sections explain how to create a Private Service Connect endpoint manually.
Create a Cloud SQL instanceYou can create an instance with Private Service Connect enabled for the instance by using gcloud CLI, Terraform, or the API.
Note: You can now create an instance that supports both private services access and Private Service Connect. By using Private Service Connect, you can connect to either a primary instance or any of its read replicas from multiple VPC networks. For more information, see Configure both private services access and Private Service Connect.
gcloudTo create an instance with Private Service Connect enabled for the instance, use the gcloud sql instances create
command:
gcloud sql instances create INSTANCE_NAME \ --project=PROJECT_ID \ --region=REGION_NAME \ --enable-private-service-connect \ --allowed-psc-projects=ALLOWED_PROJECTS \ --availability-type=AVAILABILITY_TYPE \ --no-assign-ip \ --tier=MACHINE_TYPE \ --database-version=DATABASE_VERSION
Make the following replacements:
ALLOWED_PROJECTS: a comma-separated list of allowed project IDs or numbers from where Private Service Connect endpoints can connect to Cloud SQL instances.
If a project isn't contained in this list, then you can't create Private Service Connect endpoints in the project to connect to the instance.
REGIONAL
: enables high availability and is recommended for production instances. The instance fails over to another zone within your selected region.ZONAL
: provides no failover capability. This is the default value.For more information about setting and removing high availability for instances, see Configure an existing instance for high availability and Deactivate high availability for an instance.
POSTGRES_13
).To create an instance with Private Service Connect enabled for the instance, use the google_sql_database_instance
Terraform resource.
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud ShellSet 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.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
.
mkdir DIRECTORY && cd DIRECTORY && 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.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt:
terraform apply
Wait until Terraform displays the "Apply complete!" message.
Before using any of the request data, make the following replacements:
REGIONAL
: enables high availability and is recommended for production instances. The instance fails over to another zone within your selected region.ZONAL
: provides no failover capability. This is the default value.For more information about setting and removing high availability for instances, see Configure an existing instance for high availability and Deactivate high availability for an instance.
ALLOWED_PROJECTS: a comma-separated list of allowed project IDs or numbers from where Private Service Connect endpoints can connect to Cloud SQL instances.
If a project isn't contained in this list, then you can't create Private Service Connect endpoints in the project to connect to the instance.
HTTP method and URL:
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances
Request JSON body:
{ "name": "INSTANCE_NAME", "project": PROJECT_ID", "region": "REGION_NAME", "databaseVersion": "POSTGRES_13", "kind": "sql#instance", "settings": { "availabilityType": "AVAILABILITY_TYPE", "ipConfiguration": { "ipv4Enabled": false, "pscConfig": { "allowedConsumerProjects": [ "ALLOWED_PROJECTS" ], "pscEnabled": true } }, "kind": "sql#settings", "pricingPlan": "PER_USE", "replicationType": "SYNCHRONOUS", "tier": "MACHINE_TYPE" } }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-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"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_NAME", "status": "RUNNING", "user": "user@example.com", "insertTime": "2020-01-16T02:32:12.281Z", "startTime": "2023-06-14T18:48:35.499Z", "operationType": "CREATE", "name": "OPERATION_ID", "targetId": "INSTANCE_NAME", "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID", "targetProject": "PROJECT_ID" }Get the service attachment
After creating a Cloud SQL instance with Private Service Connect enabled, get the service attachment URI and use it to create the Private Service Connect endpoint.
gcloudTo view summary information about an instance with Private Service Connect enabled, such as the pscServiceAttachmentLink
field which displays the URI that points to the service attachment of the instance, use the gcloud sql instances describe
command:
gcloud sql instances describe INSTANCE_NAME \ --project=PROJECT_ID
Note: The service attachment URI is used to create the Private Service Connect endpoint.
Make the following replacements:
The following example shows a sample output for this command:
gcloud sql instances describe myinstance \ --project=12345 ... pscServiceAttachmentLink: projects/45678/regions/myregion/serviceAttachments/myserviceattachmentTerraform
To get the service attachment URI, use the google_compute_address
Terraform resource.
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud ShellSet 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.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
.
mkdir DIRECTORY && cd DIRECTORY && 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.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt:
terraform apply
Wait until Terraform displays the "Apply complete!" message.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ ... pscServiceAttachmentLink: "projects/PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME" }
The pscServiceAttachmentLink
field displays the URI that points to the service attachment of the instance.
You can reserve an internal IP address for the Private Service Connect endpoint and create an endpoint with that address. To create the endpoint, you need the service attachment URI and the projects that are allowed for the instance.
gcloudTo reserve an internal IP address for the Private Service Connect endpoint, use the gcloud compute addresses create
command:
gcloud compute addresses create ADDRESS_NAME \ --project=PROJECT_ID \ --region=REGION_NAME \ --subnet=SUBNET_URI \ --addresses=INTERNAL_IP_ADDRESS
Make the following replacements:
projects/SUBNET_PROJECT_ID/regions/REGION_NAME/subnetworks/SUBNET_NAME
. If the subnet is in a Shared VPC network, then we recommend that the SUBNET_PROJECT_ID is the host project.To verify that the IP address is reserved, use the gcloud compute addresses list
command:
gcloud compute addresses list ADDRESS_NAME \ --project=PROJECT_ID
In the response, verify that a RESERVED
status appears for the IP address.
To create the Private Service Connect endpoint and point it to the Cloud SQL service attachment, use the gcloud compute forwarding-rules create
command:
gcloud compute forwarding-rules create ENDPOINT_NAME \ --address=ADDRESS_NAME \ --project=PROJECT_ID \ --region=REGION_NAME \ --network=NETWORK_URI \ --target-service-attachment=SERVICE_ATTACHMENT_URI \ --allow-psc-global-access
Make the following replacements:
projects/NETWORK_PROJECT_ID/global/networks/NETWORK_NAME
. If you want to use a Shared VPC network, then specify the host project as the NETWORK_PROJECT_ID.--allow-psc-global-access
parameter, clients from all regions can access this forwarding rule.To verify that the service attachment accepts the endpoint, use the gcloud compute forwarding-rules describe
command:
gcloud compute forwarding-rules describe ENDPOINT_NAME \ --project=PROJECT_ID \ --region=REGION_NAME
In the response, verify that an ACCEPTED
status appears for the pscConnectionStatus
field. The endpoint can connect to the service attachment.
To create a Private Service Connect endpoint, use the google_sql_database_instance
Terraform resource.
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud ShellSet 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.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
.
mkdir DIRECTORY && cd DIRECTORY && 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.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt:
terraform apply
Wait until Terraform displays the "Apply complete!" message.
Reserve an internal IP address for the Private Service Connect endpoint.
Note: You can't use the API to reserve an internal IP address for the endpoint. To reserve this address, use thegcloud compute addresses create
command.Verify that the IP address is reserved.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "compute#address", "id": "ADDRESS_ID", "creationTimestamp": "2024-05-09T11:20:50.114-07:00", "name": "ADDRESS_NAME", "description": "This is the name of the internal IP address.", "address": "IP_ADDRESS", "status": "RESERVED", "region": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME", "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME", "networkTier": "PREMIUM", "labelFingerprint": "LABEL_FINGERPRINT_ID", "addressType": "EXTERNAL" }
In the response, verify that a RESERVED
status appears for the IP address.
Create the Private Service Connect endpoint and point it to the Cloud SQL service attachment.
Note: You can't use the API to create the Private Service Connect endpoint. To create this endpoint, use thegcloud compute forwarding-rules create
command.Verify that the service attachment accepts the endpoint.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "compute#forwardingRule", "id": "ENDPOINT_ID", "creationTimestamp": "2024-05-09T12:03:21.383-07:00", "name": "ENDPOINT_NAME", "region": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME", "IPAddress": "IP_ADDRESS", "target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME", "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME", "network": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/default", "serviceDirectoryRegistrations": [ { "namespace": "goog-psc-default" } ], "networkTier": "PREMIUM", "labelFingerprint": "LABEL_FINGERPRINT_ID", "fingerprint": "FINGERPRINT_ID", "pscConnectionId": "CONNECTION_ID", "pscConnectionStatus": "ACCEPTED", "allowPscGlobalAccess": true }
In the response, verify that an ACCEPTED
status appears for the pscConnectionStatus
field. The endpoint can connect to the service attachment.
You can connect to a Cloud SQL instance with Private Service Connect enabled by using an internal IP address, a DNS record, the Cloud SQL Auth Proxy, the Cloud SQL Language Connectors, or other Google Cloud applications.
Configure a DNS managed zone and a DNS recordCloud SQL doesn't create DNS records automatically. Instead, the instance lookup API response provides a suggested DNS name. We recommend that you create the DNS record in a private DNS zone in the corresponding VPC network. This provides a consistent way of using the Cloud SQL Auth Proxy to connect from different networks.
Important: If you're using the Cloud SQL Auth Proxy or the Cloud SQL Language Connectors, then set up a DNS record which matches the recommended DNS name provided for the instance. For more information, see Connect using the Cloud SQL Auth Proxy.
For more information about best practices for private DNS zones, including how to allow on-premises hosts to query DNS records that are hosted in these zones, see Best practices for Cloud DNS private zones.
gcloudTo view summary information about a Cloud SQL instance, including the DNS name of the instance, use the gcloud sql instances describe
command:
gcloud sql instances describe INSTANCE_NAME \ --project=PROJECT_ID
Make the following replacements:
In the response, verify that the DNS name appears. This name has the following pattern: INSTANCE_UID.PROJECT_DNS_LABEL.REGION_NAME.sql.goog.
. For example: 1a23b4cd5e67.1a2b345c6d27.us-central1.sql.goog.
.
Note: DNS names always end with a period (.
).
If you want to use a custom DNS name to connect to a Cloud SQL instance instead of using the predefined DNS name in this section, then configure the custom subject alternative name (SAN) setting while creating the instance. The custom DNS name that you insert into the custom SAN setting is added to the SAN field of the server certificate of the instance. This lets you use the custom DNS name with hostname validation securely.
For more information about configuring the custom SAN setting, see Create instances.
To create a private DNS zone, use the gcloud dns managed-zones create
command. This zone is associated with the VPC network that's used to connect to the Cloud SQL instance through the Private Service Connect endpoint.
Note: For each VPC network, create a DNS zone.
gcloud dns managed-zones create ZONE_NAME \ --project=PROJECT_ID \ --description="DESCRIPTION" \ --dns-name=DNS_NAME \ --networks=NETWORK_NAME \ --visibility=private
Make the following replacements:
REGION_NAME.sql.goog.
(where REGION_NAME is the region name for the zone)After you create the Private Service Connect endpoint, to create a DNS record in the zone, use the gcloud dns record-sets create
command:
gcloud dns record-sets create DNS_RECORD \ --project=PROJECT_ID \ --type=RRSET_TYPE \ --rrdatas=RR_DATA \ --zone=ZONE_NAME
Make the following replacements:
1a23b4cd5e67.1a2b345c6d27.us-central1.sql.goog.
).A
).198.51.100.5
). You can also enter multiple values such as rrdata1 rrdata2 rrdata3
(for example, 10.1.2.3 10.2.3.4 10.3.4.5
).Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ ... "dnsName": "INSTANCE_ID.PROJECT_DNS_LABEL.REGION_NAME.sql.goog." }
The dnsName
field displays the DNS name of the Cloud SQL instance. DNS names always end with a period (.
).
Note: For each VPC network, create a DNS zone.
Before using any of the request data, make the following replacements:
REGION_NAME.sql.goog.
(where REGION_NAME is the region name for the zone)HTTP method and URL:
POST https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones
Request JSON body:
{ "name": "ZONE_NAME", "description": "DESCRIPTION", "dnsName": "DNS_NAME", "visibility": "private", "privateVisibilityConfig": { "kind": "dns#managedZonePrivateVisibilityConfig", "networks": [ { "kind": "dns#managedZonePrivateVisibilityConfigNetwork", "networkUrl": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/NETWORK_NAME" } ] } }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "name": "ZONE_NAME", "dnsName": "DNS_NAME", "description": "DESCRIPTION", "id": "ID", "nameServers": [ "ns-gcp-private.googledomains.com." ], "creationTime": "2024-05-10T17:05:34.607Z", "visibility": "private", "privateVisibilityConfig": { "networks": [ { "networkUrl": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/NETWORK_NAME", "kind": "dns#managedZonePrivateVisibilityConfigNetwork" } ], "gkeClusters": [], "kind": "dns#managedZonePrivateVisibilityConfig" }, "cloudLoggingConfig": { "kind": "dns#managedZoneCloudLoggingConfig" }, "kind": "dns#managedZone" }
Before using any of the request data, make the following replacements:
1a23b4cd5e67.1a2b345c6d27.us-central1.sql.goog.
).A
).300
).198.51.100.5
). You can also enter multiple values such as rrdata1 rrdata2 rrdata3
(for example, 10.1.2.3 10.2.3.4 10.3.4.5
).HTTP method and URL:
POST https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones/ZONE_NAME
Request JSON body:
{ "deletions": [] "additions": [ { "name": "DNS_RECORD", "type": "RRSET_TYPE", "ttl": TTL, "rrdatas": [ "RR_DATA" ] } ] }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones/ZONE_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://dns.googleapis.com/dns/v1/projects/PROJECT_ID/managedZones/ZONE_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "additions": [ { "name": "DNS_RECORD", "type": "RRSET_TYPE", "ttl": TTL, "rrdatas": [ "RR_DATA" ], "signatureRrdatas": [], "kind": "dns#resourceRecordSet" } ], "deletions": [], "startTime": "2024-05-10T17:29:44.375Z", "id": "CHANGE_ID", "status": "pending", "kind": "dns#change" }Note: If you're using this DNS record to connect to the Cloud SQL instance, then make a note of the CHANGE_ID value.
Before connecting to a Cloud SQL instance using a DNS record, do the following:
ACCEPTED
, check the status.After you meet these conditions, use the DNS record to connect to the instance from any VPC network where you created the endpoint.
psql "sslmode=disable dbname=DATABASE_NAME user=USERNAME host=DNS_RECORD"
Make the following replacements:
Before connecting to a Cloud SQL instance with Private Service Connect enabled, do the following:
ACCEPTED
, check the status.After you meet these conditions, use the endpoint's IP address to access the instance from any VPC network where you created the endpoint.
Retrieve the internal IP address of the Private Service Connect endpoint using the name of the endpoint's IP address.
Note: You can use the commands in this section to retrieve the internal IP addresses of all endpoints that are created both manaully and automatically. To retrieve the internal IP addresses of endpoints that are created automatically, see Retrieve the endpoint. gcloudTo retrieve the IP address, use the gcloud compute addresses describe
command:
gcloud compute addresses describe ADDRESS_NAME \ --project=PROJECT_ID \ --region=REGION_NAME
Make the following replacements:
In the response, verify that an IP address appears for the address
field. This is the internal IP address.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "compute#address", "id": "ADDRESS_ID", "creationTimestamp": "2024-05-09T11:20:50.114-07:00", "name": "ADDRESS_NAME", "description": "This is the name of the internal IP address.", "address": "IP_ADDRESS", "status": "RESERVED", "region": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME", "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/addresses/ADDRESS_NAME", "networkTier": "PREMIUM", "labelFingerprint": "LABEL_FINGERPRINT_ID", "addressType": "EXTERNAL" }
The internal IP address is the value that's associated with the address
field.
Alternatively, retrieve the internal IP address of the Private Service Connect endpoint using the service attachment of the Cloud SQL instance.
gcloudTo retrieve the IP address, use the gcloud compute forwarding-rules list
command:
gcloud compute forwarding-rules list \ --filter="TARGET:REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME" \ --project=PROJECT_ID
Make the following replacements:
In the response, verify that an IP address appears. This is the internal IP address.
The following is a sample response:
NAME
REGION
IP_ADDRESS
TARGET
myInstance
us-central1
10.10.10.10
us-central1/serviceAttachments/a-123456789e0a-psc-service-attachment-abc123d4e5f67gh8
REST
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules?target="https://www.googleapis.com/compute/v1/projects/SERVICE_ATTACHMENT_PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME"
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules?target="https://www.googleapis.com/compute/v1/projects/SERVICE_ATTACHMENT_PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME""
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules?target="https://www.googleapis.com/compute/v1/projects/SERVICE_ATTACHMENT_PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME"" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "compute#forwardingRuleList", "id": "projects/PROJECT_ID/regions/REGION_NAME/forwardingRules", "items": [ { "kind": "compute#forwardingRule", "id": "FORWARDING_RULE_ID", "creationTimestamp": "2023-10-31T13:04:37.168-07:00", "name": "FORWARDING_RULE_NAME", "region": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME", "IPAddress": "IP_ADDRESS", "target": "https://www.googleapis.com/compute/v1/projects/SERVICE_ATTACHMENT_PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME", "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/FORWARDING_RULE_NAME", "network": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/VPC_NETWORK_NAME", "serviceDirectoryRegistrations": [ { "namespace": "goog-psc-default" } ], "networkTier": "PREMIUM", "labelFingerprint": "LABEL_FINGERPRINT_ID", "fingerprint": "FINGERPRINT_ID", "pscConnectionId": "PSC_CONNECTION_ID", "pscConnectionStatus": "CLOSED", "allowPscGlobalAccess": true } ], "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules" }
The internal IP address is the value that's associated with the IPAddress
field.
To connect to the Cloud SQL instance, use the internal IP address.
psql "sslmode=disable dbname=DATABASE_NAME user=USERNAME hostaddr=IP_ADDRESS"
Make the following replacements:
The Cloud SQL Auth Proxy is a connector that provides secure access to an instance with Private Service Connect enabled without a need for authorized networks or for configuring SSL.
To allow Cloud SQL Auth Proxy client connections, set up a DNS record which matches the recommended DNS name that's provided for the instance. The DNS record is a mapping between a DNS resource and a domain name.
If you're connecting through Private Service Connect, then Cloud SQL Auth Proxy version v2.5.0 or later is required.
Download and install the Cloud SQL Auth ProxyTo connect to instances with Private Service Connect enabled, you must download and install the binary for the Cloud SQL Auth Proxy. The binary that you download depends on the operating system, and whether it uses a 32-bit or 64-bit kernel. Most newer hardware uses a 64-bit kernel.
If you're unsure whether your machine is running a 32-bit or 64-bit kernel, then use the uname -a
command for Linux or macOS. For Windows, see the Windows documentation.
The Cloud SQL Auth Proxy supports connections to instances with Private Service Connect enabled. For more information, see Start the Cloud SQL Auth Proxy.
To view summary information about a Cloud SQL instance, use the gcloud sql instances describe
command.
gcloud sql instances describe INSTANCE_NAME \ --project=PROJECT_ID \ --format='value(connectionName)'
Make the following replacements:
The connection name is in the format of PROJECT_ID:REGION_NAME:INSTANCE_NAME
.
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://sqladmin.googleapis.com/sql/v1/projects/PROJECT_ID/instances/INSTANCE_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ ... "connectionName": "PROJECT_ID:REGION_NAME:INSTANCE_NAME" }
The connection name is in the format of PROJECT_ID:REGION_NAME:INSTANCE_NAME
.
Launch the Cloud SQL Auth Proxy:
./cloud-sql-proxy INSTANCE_CONNECTION_NAME --psc
Replace INSTANCE_CONNECTION_NAME with the instance connection name that you copied in the previous step.
Note: Use the psc
flag to start the Cloud SQL Auth Proxy to connect to instances with Private Service Connect enabled.
The Cloud SQL Language Connectors are libraries that provide secure access to a Cloud SQL instance with Private Service Connect enabled without a need for authorized networks or for configuring SSL.
To allow connections with Cloud SQL Language Connectors, set up a DNS record which matches the recommended DNS name that's provided for the instance. The DNS record is a mapping between a DNS resource and a domain name.
The Cloud SQL Language Connectors support Private Service Connect connections through the PSC
IP type within their respective libraries.
To connect to Cloud SQL instances with Private Service Connect enabled, you can use App Engine Standard or Cloud Run.
In these supported serverless environments, both the Cloud SQL Language Connectors and direct TCP connections by using an IP address and port number are supported. For direct TCP connections, this is the IP address that you reserve when you create the Private Service Connect endpoint. You can specify the IP address as the address for the database host.
If you create a DNS record for the endpoint, then you can specify this record for the host.
Connect from BigQueryTo access data in Cloud SQL and make queries against this data over an internal IP connection, use the --enable-google-private-path
parameter . This parameter is valid only if:
--no-assign-ip
parameter.--network
parameter to specify the name of the VPC network that you want to use to create an internal connection.To test inbound connectivity to a Cloud SQL instance with Private Service Connect enabled, set the IP address of the Private Service Connect endpoint to be the destination IP address.
gcloudTo create a connectivity test for a Cloud SQL instance with Private Service Connect enabled, use the gcloud network-management connectivity-tests create
command:
gcloud network-management connectivity-tests create CONNECTIVITY_TEST_NAME \ --source-instance=SOURCE_INSTANCE \ --destination-cloud-sql-instance=DESTINATION_CLOUD_SQL_INSTANCE \ --destination-network=DESTINATION_NETWORK \ --destination-port=DESTINATION_PORT \ --protocol=tcp
Make the following replacements:
projects/myproject/zones/myzone/instances/myinstance
).projects/myproject/instances/myinstance
).projects/myproject/global/networks/mynetwork
).5432
.Before using any of the request data, make the following replacements:
projects/myproject/zones/myzone/instances/myinstance
).projects/myproject/global/networks/mynetwork
).5432
.projects/myproject/global/networks/mynetwork
).HTTP method and URL:
POST https://networkmanagement.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/connectivityTests?testId=CONNECTIVITY_TEST_NAME
Request JSON body:
{ "source": { "ipAddress": "SOURCE_IP_ADDRESS", "instance": "SOURCE_INSTANCE", "network": "SOURCE_NETWORK" }, "destination": { "ipAddress": "DESTINATION_IP_ADDRESS", "port": DESTINATION_PORT, "network": "DESTINATION_NETWORK", "projectId": "PROJECT_ID" }, "protocol": "TCP" }
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Save the request body in a file named request.json
, and execute the following command:
curl -X POST \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://networkmanagement.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/connectivityTests?testId=CONNECTIVITY_TEST_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud 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 POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://networkmanagement.googleapis.com/v1beta/projects/PROJECT_ID/locations/global/connectivityTests?testId=CONNECTIVITY_TEST_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "name": "projects/PROJECT_ID/locations/global/operations/operation-OPERATION_ID", "metadata": { "@type": "type.googleapis.com/google.cloud.networkmanagement.v1.OperationMetadata", "createTime": "2024-05-23T16:43:49.313981473Z", "target": "projects/PROJECT_ID/locations/global/connectivityTests/CONNECTIVITY_TEST_NAME", "verb": "create", "cancelRequested": false, "apiVersion": "v1" }, "done": false }Note: In addition to testing connectivity, you can restrict connectivity to Private Service Connect endpoints from service consumers in a VPC network. To do this, use the
gcloud compute firewall-rules create
command to create a network egress firewall rule. This rule applies to the IP address of an endpoint. For the rule, define the source to be all VMs in the VPC network and specify a tag or service account. Limitations
--no-assign-ip:
use this flag because instances with Private Service Connect enabled aren't supported to use other connectivity types such as external IP connections--authorized-networks:
you can't use this flag to add authorized networks--network:
you can't use this flag because it's associated with private services access--allocated-ip-range-name:
you can't use this flag because allowed IP range names aren't supportedgcloud sql connect
command, Cloud Shell, Cloud Build, or Datastream to connect to Cloud SQL instances with Private Service Connect enabled.pglogical
, pl/proxy
, dblink
, and postgres_fdw
extensions aren't supported.This section contains information about issues associated with Cloud SQL instances with Private Service Connect enabled along with steps for troubleshooting the issues.
Issue Troubleshooting The service attachment of the instance doesn't accept the Private Service Connect endpoint.To check the status, use the gcloud compute forwarding-rules describe
command.
gcloud compute forwarding-rules describe ENDPOINT_NAME \ --project=PROJECT_ID \ --region=REGION_NAME \ | grep pscConnectionStatus
Make the following replacements:
Before using any of the request data, make the following replacements:
HTTP method and URL:
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME
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 running gcloud init
or gcloud auth login
, or by using Cloud Shell, which automatically logs you into the gcloud
CLI . You can check the currently active account by running gcloud auth list
.
Execute the following command:
curl -X GET \PowerShell (Windows) Note: The following command assumes that you have logged in to the
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME"
gcloud
CLI with your user account by running gcloud init
or gcloud auth login
. You can check the currently active account by running gcloud auth list
.
Execute the following command:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME" | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "kind": "compute#forwardingRule", "id": "ENDPOINT_ID", "creationTimestamp": "2024-05-09T12:03:21.383-07:00", "name": "ENDPOINT_NAME", "region": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME", "IPAddress": "IP_ADDRESS", "target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/serviceAttachments/SERVICE_ATTACHMENT_NAME", "selfLink": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION_NAME/forwardingRules/ENDPOINT_NAME", "network": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/default", "serviceDirectoryRegistrations": [ { "namespace": "goog-psc-default" } ], "networkTier": "PREMIUM", "labelFingerprint": "LABEL_FINGERPRINT_ID", "fingerprint": "FINGERPRINT_ID", "pscConnectionId": "CONNECTION_ID", "pscConnectionStatus": "ACCEPTED", "allowPscGlobalAccess": true }
ACCEPTED
. If the status is PENDING
, then the instance isn't allowing the Google Cloud project that contains the endpoint. Make sure that the network project in which the endpoint is created is allowed. For more information, see Edit an instance with Private Service Connect enabled.ERROR: (gcloud.compute.forwarding-rules.create) Could not fetch resource: The resource 'projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME' was not found
This error message can occur when reserving a static internal IP address for the Private Service Connect endpoint. Make sure the subnet specified exists in the project specified by the URI. If you want to create an endpoint in a service project but use a subnet from a Shared VPC network, you need to specify the subnet by its URI and use the host project's project ID in the URI. For more information, see Create the endpoint manually. ERROR: (gcloud.compute.forwarding-rules.create) Could not fetch resource: - The resource 'projects/PROJECT_ID/global/networks/NETWORK_NAME' was not found
This error message can occur when you create a Private Service Connect endpoint manually. Make sure the network specified exists in the project specified by the URI. If you want to create an endpoint in a service project but use a Shared VPC network, you need to specify the network by its URI and use the host project's project ID in the URI. For more information, see Create the endpoint manually. What's next
Except 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-09 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-09 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