A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cloud.google.com/sql/docs/postgres/connect-auth-proxy below:

Connect using the Cloud SQL Auth Proxy | Cloud SQL for PostgreSQL

This page describes how to connect to your Cloud SQL instance using the Cloud SQL Auth Proxy.

For more information about how the Cloud SQL Auth Proxy works, see About the Cloud SQL Auth Proxy.

Overview

Using the Cloud SQL Auth Proxy is the recommended method for connecting to a Cloud SQL instance. The Cloud SQL Auth Proxy:

Some Google Cloud services and applications use the Cloud SQL Auth Proxy to provide connections for public IP paths with encryption and authorization, including:

Applications running in Google Kubernetes Engine can connect using the Cloud SQL Auth Proxy.

Note: For connecting from Google Kubernetes Engine, we recommend running the Cloud SQL Auth Proxy in a sidecar pattern, as an additional container that shares a pod with your application. Also see the related quickstart.

See the Quickstart for using the Cloud SQL Auth Proxy for a basic introduction to its usage.

You can also connect, with or without the Cloud SQL Auth Proxy, using a psql client from a local machine or Compute Engine.

Before you begin

Before you can connect to a Cloud SQL instance, do the following:

  1. Enable the Cloud SQL Admin API.

    Enable the API

  2. Install and initialize the gcloud CLI.
  3. Optional. Install the Cloud SQL Auth Proxy Docker client.
Download the Cloud SQL Auth Proxy Linux 64-bit
  1. Download the Cloud SQL Auth Proxy:
    curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.linux.amd64
  2. Make the Cloud SQL Auth Proxy executable:
    chmod +x cloud-sql-proxy
Linux 32-bit
  1. Download the Cloud SQL Auth Proxy:
    curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.linux.386
  2. If the curl command is not found, run sudo apt install curl and repeat the download command.
  3. Make the Cloud SQL Auth Proxy executable:
    chmod +x cloud-sql-proxy
macOS 64-bit
  1. Download the Cloud SQL Auth Proxy:
    curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.darwin.amd64
  2. Make the Cloud SQL Auth Proxy executable:
    chmod +x cloud-sql-proxy
Mac M1
  1. Download the Cloud SQL Auth Proxy:
      curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.darwin.arm64
      
  2. Make the Cloud SQL Auth Proxy executable:
      chmod +x cloud-sql-proxy
      
Windows 64-bit Right-click https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.x64.exe and select Save Link As to download the Cloud SQL Auth Proxy. Rename the file to cloud-sql-proxy.exe. Windows 32-bit Right-click https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.17.1/cloud-sql-proxy.x86.exe and select Save Link As to download the Cloud SQL Auth Proxy. Rename the file to cloud-sql-proxy.exe. Cloud SQL Auth Proxy Docker image

The Cloud SQL Auth Proxy has different container images, such as distroless, alpine, and buster. The default Cloud SQL Auth Proxy container image uses distroless, which contains no shell. If you need a shell or related tools, then download an image based on alpine or buster. For more information, see Cloud SQL Auth Proxy Container Images.

You can pull the latest image to your local machine using Docker by using the following command:

docker pull gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1
Note: The Cloud SQL Auth Proxy uses a repository that supports the gcr.io domain but serves images from Artifact Registry. For more information, see Transition from Container Registry. Other OS For other operating systems not included here, you can compile the Cloud SQL Auth Proxy from source. Start the Cloud SQL Auth Proxy

Depending on your language and environment, you can start the Cloud SQL Auth Proxy using TCP sockets, Unix sockets, or the Cloud SQL Auth Proxy Docker image. The Cloud SQL Auth Proxy binary connects to one or more Cloud SQL instances specified on the command line, and opens a local connection as either TCP or a Unix socket. Other applications and services, such as your application code or database management client tools, can connect to Cloud SQL instances through those TCP or Unix socket connections.

Warning: Be careful when binding the Cloud SQL Auth Proxy to an external interface. Anyone with access to that interface/port will be authorized to connect to your instance. TCP sockets

For TCP connections, the Cloud SQL Auth Proxy listens on localhost(127.0.0.1) by default. So, when you specify --port PORT_NUMBER for an instance, the local connection is at 127.0.0.1:PORT_NUMBER.

Alternatively, you can specify a different address for the local connection. For example, here's how to make the Cloud SQL Auth Proxy listen at 0.0.0.0:1234 for the local connection:

./cloud-sql-proxy --address 0.0.0.0 --port 1234 INSTANCE_CONNECTION_NAME
  1. Copy your INSTANCE_CONNECTION_NAME. This can be found on the Overview page for your instance in the Google Cloud console or by running the following command:

        gcloud sql instances describe INSTANCE_NAME --format='value(connectionName)'

    For example: myproject:myregion:myinstance.

  2. If the instance has both public and private IP configured, and you want the Cloud SQL Auth Proxy to use the private IP address, you must provide the following option when you start the Cloud SQL Auth Proxy:
    --private-ip
  3. If you are using a service account to authenticate the Cloud SQL Auth Proxy, note the location on your client machine of the private key file that was created when you created the service account.
  4. Start the Cloud SQL Auth Proxy.

    Some possible Cloud SQL Auth Proxy invocation strings:

    For more information about Cloud SQL Auth Proxy options, see Options for authenticating the Cloud SQL Auth Proxy.

Unix sockets Note: The Cloud SQL Auth Proxy currently does not support Unix sockets on Windows, so this option is only available for Linux and macOS platforms.

The Cloud SQL Auth Proxy can listen on a Unix socket, which is a Posix standard mechanism for using a folder to manage communication between two processes running on the same host. Advantages to using Unix sockets are improved security and lower latency, however, you cannot access a Unix socket from an external machine.

Note: The PostgreSQL standard requires a `.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.5432
  

To create and use a Unix socket, the target directory must exist and both the Cloud SQL Auth Proxy and application must have read and write access to it.

  1. Copy your INSTANCE_CONNECTION_NAME. This can be found on the Overview page for your instance in the Google Cloud console or by running the following command:

        gcloud sql instances describe INSTANCE_NAME --format='value(connectionName)'

    For example: myproject:myregion:myinstance.

  2. Create the directory where the Cloud SQL Auth Proxy sockets will live:
    sudo mkdir /cloudsql; sudo chmod 777 /cloudsql
  3. If you are using a service account to authenticate the Cloud SQL Auth Proxy, note the location on your client machine of the private key file that was created when you created the service account.
  4. Open a new Cloud Shell terminal window and start the Cloud SQL Auth Proxy.

    Some possible Cloud SQL Auth Proxy invocation strings:

    Start the Cloud SQL Auth Proxy in its own Cloud Shell terminal so you can monitor its output without it mixing with the output from other programs.

    For more information about Cloud SQL Auth Proxy options, see Options for authenticating the Cloud SQL Auth Proxy.

    NOTE: When using a unix socket to connect to Cloud SQL using the Cloud SQL Auth Proxy, make sure the socket filename's length does not surpass the system's limit. It depends on the system, but it's usually between 91-108 characters. On Linux, the length is usually defined as 108, and you can use the following command to check:
    cat /usr/include/linux/un.h | grep "define UNIX_PATH_MAX"
Docker

To run the Cloud SQL Auth Proxy in a Docker container, use the Cloud SQL Auth Proxy Docker image available from the Google Container Registry.

You can start the Cloud SQL Auth Proxy using either TCP sockets or Unix sockets, with the commands shown below. The options use an INSTANCE_CONNECTION_NAME as the connection string to identify a Cloud SQL instance. You can find the INSTANCE_CONNECTION_NAME on the Overview page for your instance in the Google Cloud console. or by running the following command:

gcloud sql instances describe INSTANCE_NAME
.

For example: myproject:myregion:myinstance.

Note: The Cloud SQL Auth Proxy doesn't support listening on Unix domain sockets on Windows.

Depending on your language and environment, you can start the Cloud SQL Auth Proxy using either TCP sockets or Unix sockets. Unix sockets are not supported for applications written in the Java programming language or for the Windows environment.

Using TCP sockets
docker run -d \\
  -v PATH_TO_KEY_FILE:/path/to/service-account-key.json \\
  -p 127.0.0.1:5432:5432 \\
  gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1 \\
  --address 0.0.0.0 --port 5432 \\
  --credentials-file /path/to/service-account-key.json INSTANCE_CONNECTION_NAME

If you're using the credentials provided by your Compute Engine instance, don't include the --credentials-file parameter and the -v PATH_TO_KEY_FILE:/path/to/service-account-key.json line.

Always specify 127.0.0.1 prefix in -p so that the Cloud SQL Auth Proxy is not exposed outside the local host. The "0.0.0.0" in the instances parameter is required to make the port accessible from outside of the Docker container.

Using Unix sockets
docker run -d -v /cloudsql:/cloudsql \\
  -v PATH_TO_KEY_FILE:/path/to/service-account-key.json \\
  gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1 --unix-socket=/cloudsql \\
  --credentials-file /path/to/service-account-key.json INSTANCE_CONNECTION_NAME

If you're using the credentials provided by your Compute Engine instance, don't include the --credentials-file parameter and the -v PATH_TO_KEY_FILE:/path/to/service-account-key.json line.

If you are using a container optimized image, use a writeable directory in place of /cloudsql, for example:

-v /mnt/stateful_partition/cloudsql:/cloudsql

You can specify more than one instance, separated by commas. You can also use Compute Engine metadata to dynamically determine the instances to connect to. Learn more about the Cloud SQL Auth Proxy parameters.

Connect with the psql client Debian/Ubuntu

Install the psql client from the package manager:

sudo apt-get update
sudo apt-get install postgresql-client
CentOS/RHEL

Install the psql client from the package manager:

sudo yum install postgresql
openSUSE

Install the psql client from the package manager:

sudo zypper install postgresql
Other platforms
  1. Download the PostgreSQL Core Distribution for your platform from the PostgreSQL Downloads page.
    The Core Distribution includes the psql client.
  2. Install the PostgreSQL database, following the directions on the download page.

The connection string you use depends on whether you started the Cloud SQL Auth Proxy using a TCP socket or a UNIX socket or Docker.

TCP sockets
  1. Start the psql client:
    psql "host=127.0.0.1 sslmode=disable dbname=DB_NAME user=USERNAME"

    Even though the sslmode parameter is set to disable, the Cloud SQL Auth Proxy does provide an encrypted connection.

    When you connect using TCP sockets, the Cloud SQL Auth Proxy is accessed through 127.0.0.1.

  2. If prompted, enter the password.
  3. The psql prompt appears.
Using Unix sockets
  1. Start the psql client:
    psql "sslmode=disable host=/cloudsql/INSTANCE_CONNECTION_NAME dbname=DB_NAME user=USERNAME"

    Even though the sslmode parameter is set to disable, the Cloud SQL Auth Proxy does provide an encrypted connection.

  2. Enter the password.
  3. The psql prompt appears.

Need help? For help troubleshooting the proxy, see Troubleshooting Cloud SQL Auth Proxy connections, or see our Cloud SQL Support page.

Connect with an application

You can connect to the Cloud SQL Auth Proxy from any language that enables you to connect to a Unix or TCP socket. Below are some code snippets from complete examples on GitHub to help you understand how they work together in your application.

Connecting with TCP

Cloud SQL Auth Proxy invocation statement:

./cloud-sql-proxy INSTANCE_CONNECTION_NAME &
Connecting with Unix sockets

Cloud SQL Auth Proxy invocation statement:

./cloud-sql-proxy --unix-socket /cloudsql INSTANCE_CONNECTION_NAME &
Additional topics Cloud SQL Auth Proxy command-line arguments

The examples above cover the most common use cases, but the Cloud SQL Auth Proxy also has other configuration options that can be set with command-line arguments. For help on command-line arguments, use the --help flag to view the latest documentation:

./cloud-sql-proxy --help

See the README on the Cloud SQL Auth Proxy GitHub repository for additional examples of how to use Cloud SQL Auth Proxy command-line options.

Options for authenticating the Cloud SQL Auth Proxy When you authenticate the Cloud SQL Auth Proxy, you enable it to access Google Cloud on behalf of your application, using a set of Google credentials. This is separate from database user authentication. IAM user database authentication is integrated with the Cloud SQL Auth Proxy.

All of these options use an INSTANCE_CONNECTION_NAME as the connection string to identify a Cloud SQL instance. You can find the INSTANCE_CONNECTION_NAME on the Overview page for your instance in the Google Cloud console. or by running the following command:

gcloud sql instances describe --project PROJECT_ID INSTANCE_CONNECTION_NAME.

For example: gcloud sql instances describe --project myproject myinstance .

Some of these options use a JSON credentials file that includes the RSA private key for the account. For instructions on creating a JSON credentials file for a service account, see Creating a service account.

The Cloud SQL Auth Proxy provides several alternatives for authentication, depending on your environment. The Cloud SQL Auth Proxy checks for each of the following items, in the following order, using the first one it finds to attempt to authenticate:

  1. Credentials supplied by the credentials-file flag. Use a service account to create and download the associated JSON file, and set the --credentials-file flag to the path of the file when you start the Cloud SQL Auth Proxy. The service account must have the required permissions for the Cloud SQL instance.

    To use this option on the command-line, invoke the cloud-sql-proxy command with the --credentials-file flag set to the path and filename of a JSON credential file. The path can be absolute, or relative to the current working directory. For example:

    ./cloud-sql-proxy --credentials-file PATH_TO_KEY_FILE \
    INSTANCE_CONNECTION_NAME
      

    For detailed instructions about adding IAM roles to a service account, see Granting Roles to Service Accounts.

    For more information about the roles Cloud SQL supports, see IAM roles for Cloud SQL.

  2. Credentials supplied by an access token.Create an access token and invoke the cloud-sql-proxy command with the --token flag set to an OAuth 2.0 access token. For example:
    ./cloud-sql-proxy --token ACCESS_TOKEN \
    INSTANCE_CONNECTION_NAME
      
  3. Credentials supplied by an environment variable. This option is similar to using the --credentials-file flag, except you specify the JSON credential file you set in the GOOGLE_APPLICATION_CREDENTIALS environment variable instead of using the --credentials-file command-line argument.
  4. Credentials from an authenticated gcloud CLI client.

    If you have installed the gcloud CLI and have authenticated with your personal account, the Cloud SQL Auth Proxy can use the same account credentials. This method is especially helpful for getting a development environment up and running.

    To enable the Cloud SQL Auth Proxy to use your gcloud CLI credentials, use the following command to authenticate the gcloud CLI:

    gcloud auth application-default login
  5. Credentials associated with the Compute Engine instance. If you are connecting to Cloud SQL from a Compute Engine instance, the Cloud SQL Auth Proxy can use the service account associated with the Compute Engine instance. If the service account has the required permissions for the Cloud SQL instance, the Cloud SQL Auth Proxy authenticates successfully. If the Compute Engine instance is in the same project as the Cloud SQL instance, the default service account for the Compute Engine instance has the necessary permissions for authenticating the Cloud SQL Auth Proxy. If the two instances are in different projects, you must add the Compute Engine instance's service account to the project containing the Cloud SQL instance.
  6. Environment's default service account If the Cloud SQL Auth Proxy cannot find credentials in any of the places covered earlier, it follows the logic documented in Setting Up Authentication for Server to Server Production Applications. Some environment (such as Compute Engine, App Engine, and others) provide a default service account that your application can use to authenticate by default. If you use a default service account, it must have the permissions outlined in roles and permissions For more information about Google Cloud's approach to authentication, see Authentication overview.
Create a service account If you are connecting from Compute Engine, make sure your VM has the proper scope to connect using the Cloud SQL Admin API.

Configure the service account to have either of the following access scopes:

  1. In the Google Cloud console, go to the Service accounts page.

    Go to Service accounts

  2. Select the project that contains your Cloud SQL instance.
  3. Click Create service account.
  4. In the Service account name field, enter a descriptive name for the service account.
  5. Change the Service account ID to a unique, recognizable value and then click Create and continue.
  6. Click the Select a role field and select one of the following roles:
  7. Click Done to finish creating the service account.
  8. Click the action menu for your new service account and then select Manage keys.
  9. Click the Add key drop-down menu and then click Create new key.
  10. Confirm that the key type is JSON and then click Create.

    The private key file is downloaded to your machine. You can move it to another location. Keep the key file secure.

Use the Cloud SQL Auth Proxy with private IP

To connect to a Cloud SQL instance using private IP, the Cloud SQL Auth Proxy must be on a resource with access to the same VPC network as the instance.

The Cloud SQL Auth Proxy uses IP to establish a connection with your Cloud SQL instance. By default, the Cloud SQL Auth Proxy attempts to connect using a public IPv4 address.

If your Cloud SQL instance has only private IP or the instance has both public and private IP configured, and you want the Cloud SQL Auth Proxy to use the private IP address, then you must provide the following option when you start the Cloud SQL Auth Proxy:

--private-ip
Use the Cloud SQL Auth Proxy with instances that have Private Service Connect enabled

You can use the Cloud SQL Auth Proxy to connect to a Cloud SQL instance with Private Service Connect enabled.

The Cloud SQL Auth Proxy is a connector that provides secure access to this instance without a need for authorized networks or for configuring SSL.

To allow Cloud SQL Auth Proxy client connections, you must 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.

For more information about using the Cloud SQL Auth Proxy to connect to instances with Private Service Connect enabled, see Connect using the Cloud SQL Auth Proxy.

Run the Cloud SQL Auth Proxy in a separate process

Running the Cloud SQL Auth Proxy in a separate Cloud Shell terminal process can be useful, to avoid mixing its console output with output from other programs. Use the syntax shown below to invoke the Cloud SQL Auth Proxy in a separate process.

Linux

On Linux or macOS, use a trailing & on the command line to launch the Cloud SQL Auth Proxy in a separate process:

./cloud-sql-proxy INSTANCE_CONNECTION_NAME
  --credentials-file PATH_TO_KEY_FILE &
Windows

In Windows PowerShell, use the Start-Process command to launch the Cloud SQL Auth Proxy in a separate process:

Start-Process --filepath "cloud-sql-proxy.exe"
  --ArgumentList "
  --credentials-file PATH_TO_KEY_FILEINSTANCE_CONNECTION_NAME"
Run the Cloud SQL Auth Proxy in a Docker container

To run the Cloud SQL Auth Proxy in a Docker container, use the Cloud SQL Auth Proxy Docker image available from the Google Container Registry. You can install the Cloud SQL Auth Proxy Docker image using the following command:

docker pull gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1

You can start the Cloud SQL Auth Proxy using either TCP sockets or Unix sockets, with the commands shown below.

Note: Unix sockets are not supported for applications written in the Java programming language or for the Windows environment. TCP sockets
    docker run -d \
      -v PATH_TO_KEY_FILE:/path/to/service-account-key.json \
      -p 127.0.0.1:5432:5432 \
      gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1 \
      --address 0.0.0.0 \
      --credentials-file /path/to/service-account-key.json \
      INSTANCE_CONNECTION_NAME
Unix sockets
    docker run -d \
      -v /PATH_TO_HOST_TARGET:/PATH_TO_GUEST_TARGET \
      -v PATH_TO_KEY_FILE:/path/to/service-account-key.json \
      gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.17.1 --unix-socket /cloudsql \
      --credentials-file /path/to/service-account-key.json/PATH_TO_KEY_FILE \
      INSTANCE_CONNECTION_NAME

If you are using a container optimized image, use a writeable directory in place of /cloudsql, for example:

v /mnt/stateful_partition/cloudsql:/cloudsql

If you are using the credentials provided by your Compute Engine instance, do not include the credential_file parameter and the -v PATH_TO_KEY_FILE:/path/to/service-account-key.json line.

Running the Cloud SQL Auth Proxy as a service

Running the Cloud SQL Auth Proxy as a background service is an option for local development and production workloads. In development, when you need to access your Cloud SQL instance, you can start the service in the background and stop it when you're finished.

For production workloads, the Cloud SQL Auth Proxy doesn't currently provide built-in support for running as a Windows service, but third-party service managers can be used to run it as a service. For example, you can use NSSM to configure the Cloud SQL Auth Proxy as a Windows service, and NSSM monitors the Cloud SQL Auth Proxy and restarts it automatically if it stops responding. See the NSSM documentation for more information.

Note: If you run the Cloud SQL Auth Proxy as a service, keep in mind that it uses a secure connection to communicate with Cloud SQL instances, but connections from your application to the Cloud SQL Auth Proxy are not encrypted. For this reason, ensure that only trusted users are able to access the address and port that the Cloud SQL Auth Proxy is listening on. Enforce the use of the Cloud SQL Auth Proxy

Enable the use of the Cloud SQL Auth Proxy in Cloud SQL using ConnectorEnforcement.

If you're using a Private Service Connect-enabled instance, then there's a limitation. If the instance has connector enforcement enabled, then you can't create read replicas for the instance. Similarly, if the instance has read replicas, then you can't enable connector enforcement for the instance.

gcloud

The following command enforces the use of Cloud SQL connectors.

    gcloud sql instances patch INSTANCE_NAME \
    --connector-enforcement REQUIRED
  

To disable the enforcement, use the following line of code: --connector-enforcement NOT_REQUIRED The update doesn't trigger a restart.

REST v1

The following command enforces the use of Cloud SQL connectors

Before using any of the request data, make the following replacements:

HTTP method and URL:

PATCH https://sqladmin.googleapis.com/v1/projects/project-id/instances/instance-id

Request JSON body:

{
  "settings": {                     
    "connectorEnforcement": "REQUIRED"    
  }                                             
}   

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 the gcloud 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 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 the 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 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 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-id",
  "status": "PENDING",
  "user": "user@example.com",
  "insertTime": "2020-01-16T02:32:12.281Z",
  "operationType": "UPDATE",
  "name": "operation-id",
  "targetId": "instance-id",
  "selfLink": "https://sqladmin.googleapis.com/v1/projects/project-id/operations/operation-id",
  "targetProject": "project-id"
}

To disable the enforcement, use "connectorEnforcement": "NOT_REQUIRED" instead. The update does not trigger a restart.

REST v1beta4

The following command enforces the use of Cloud SQL connectors.

Before using any of the request data, make the following replacements:

HTTP method and URL:

PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

Request JSON body:

{
  "settings": {
    "connectorEnforcement": "REQUIRED"
  }
}

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 the gcloud 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 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 the 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 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 Content

You should receive a JSON response similar to the following:

{
  "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-16T02:32:12.281Z",
  "operationType": "UPDATE",
  "name": "operation-id",
  "targetId": "instance-id",
  "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id",
  "targetProject": "project-id"
}

To disable the enforcement, use "connectorEnforcement": "NOT_REQUIRED" instead. The update does not trigger a restart.

Tips for working with Cloud SQL Auth Proxy Invoke the Cloud SQL Auth Proxy

All of the sample proxy invocations start the Cloud SQL Auth Proxy in the background, so a prompt is returned. Reserve that Cloud Shell terminal for the Cloud SQL Auth Proxy, to avoid having its output mixed with the output from other programs. Also, the output from the Cloud SQL Auth Proxy can help you diagnose connection problems, so it can be helpful to capture in a log file. If you do not start the Cloud SQL Auth Proxy in the background, the output goes to stdout unless redirected.

You do not have to use /cloudsql as the directory for the Cloud SQL Auth Proxy sockets. (That directory name was chosen to minimize differences with App Engine connection strings.) If you change the directory name, however, keep the overall length to a minimum; it is incorporated in a longer string that has a length limit imposed by the operating system. It depends on the system, but it's usually between 91-108 characters. On Linux, the length is usually defined as 108, and you can use the following command to check:

cat /usr/include/linux/un.h | grep "define UNIX_PATH_MAX"
Use the Cloud SQL Auth Proxy to connect to multiple instances

You can use one local Cloud SQL Auth Proxy client to connect to multiple Cloud SQL instances. The way you do this depends on whether you are using Unix sockets or TCP.

Unix sockets

To connect the Cloud SQL Auth Proxy to multiple instances, you provide each instance connection name as an argument to the Cloud SQL Auth Proxy, in a space-separated list. The Cloud SQL Auth Proxy connects to each instance when it starts.

You connect to each instance using its socket, in the specified directory.

For example:

      ./cloud-sql-proxy --unix-socket /cloudsql \
      myProject:us-central1:myInstance myProject:us-central1:myInstance2 &
      psql -U myUser -h /cloudsql/myProject:us-central1:myInstance2
  
TCP sockets

When you connect using TCP, you specify a port on your machine for the Cloud SQL Auth Proxy to listen on for each Cloud SQL instance. When connecting to multiple Cloud SQL instances, each port specified must be unique and available for use on your machine.

For example:

    # Start the Cloud SQL Auth Proxy to connect to two different Cloud SQL instances
    # Give the Cloud SQL Auth Proxy a unique port on your machine to use for each Cloud SQL instance.

    ./cloud-sql-proxy "myProject:us-central1:myInstance?port=5432" \
    "myProject:us-central1:myInstance2?port=1234"

    # Connect to "myInstance" using port 5432 on your machine:
    psql -U myUser -h 127.0.0.1  --port 5432

    # Connect to "myInstance2" using port 1234 on your machine:
    psql -U myUser -h 127.0.0.1  --port 1234
  
Troubleshoot Cloud SQL Auth Proxy connections

The Cloud SQL Auth Proxy Docker image is based on a specific version of the Cloud SQL Auth Proxy. When a new version of the Cloud SQL Auth Proxy becomes available, pull the new version of the Cloud SQL Auth Proxy Docker image to keep your environment up to date. You can see the current version of the Cloud SQL Auth Proxy by checking the Cloud SQL Auth Proxy GitHub releases page.

If you are having trouble connecting to your Cloud SQL instance using the Cloud SQL Auth Proxy, here are a few things to try to find what's causing the problem.

What's next

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