Connect to Cloud SQL for PostgreSQL using the Cloud SQL Auth Proxy
This page shows you how to connect to Cloud SQL from a local test environment using the Cloud SQL Auth Proxy. The Cloud SQL Auth Proxy provides secure access to your Cloud SQL instance without the need for authorized networks or for configuring SSL. By using the Cloud SQL Auth Proxy, you can connect to your Cloud SQL instance securely.
The instructions on this page are for a test environment only and shouldn't be used for production environments. For more information on the configuration required for production environments, see Use the Cloud SQL Auth Proxy in a production environment and Connect using the Cloud SQL Auth Proxy.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the necessary Google Cloud APIs.
Console
In the Google Cloud console, go to the APIs page.
Enable the Cloud SQL Admin API.gcloud
Click the following button to open Cloud Shell, which provides command-line access to your Google Cloud resources directly from the browser. Cloud Shell can be used to run the
gcloud
commands presented throughout this quickstart.Run the
gcloud services enable
command as follows using Cloud Shell to enable the APIs required for this quickstart.:gcloud services enable sqladmin.googleapis.com
This command enables the following APIs:
- Cloud SQL Admin API
Create a Cloud SQL instance
In this quickstart, you use the Google Cloud console. To use the gcloud CLI, cURL, or PowerShell, see Create instances.
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Click Create instance.
- Click Choose PostgreSQL.
- In the Instance ID field, enter
myinstance
. - In the Password field, enter a password for the
postgres
user. Click Create instance.
You're returned to the instances list. You can click the new instance right away to see the details, but it won't be available for other operations until it initializes and starts.
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
- Download the PostgreSQL Core Distribution for your platform from the
PostgreSQL Downloads page.
The Core Distribution includes the psql client. - Install the PostgreSQL database, following the directions on the download page.
Install the Cloud SQL Auth Proxy client
The Cloud SQL Auth Proxy binary you download depends on your 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.
Linux 64-bit
- Download the Cloud SQL Auth Proxy:
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.linux.amd64
- Make the Cloud SQL Auth Proxy executable:
chmod +x cloud-sql-proxy
Linux 32-bit
- Download the Cloud SQL Auth Proxy:
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.linux.386
- If the
curl
command is not found, runsudo apt install curl
and repeat the download command. - Make the Cloud SQL Auth Proxy executable:
chmod +x cloud-sql-proxy
macOS 64-bit
- Download the Cloud SQL Auth Proxy:
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.darwin.amd64
- Make the Cloud SQL Auth Proxy executable:
chmod +x cloud-sql-proxy
Mac M1
- Download the Cloud SQL Auth Proxy:
curl -o cloud-sql-proxy https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.darwin.arm64
- 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.14.0/cloud-sql-proxy.x64.exe and select Save Link As to download the Cloud SQL Auth Proxy. Rename the file tocloud-sql-proxy.exe
.
Windows 32-bit
Right-click https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.14.0/cloud-sql-proxy.x86.exe and select Save Link As to download the Cloud SQL Auth Proxy. Rename the file tocloud-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.14.0
Other OS
For other operating systems not included here, you can compile the Cloud SQL Auth Proxy from source.Get the instance connection name
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Click the instance name to open its Overview page.
- In the Connect to this instance section, copy the Connection name. The
connection name is in the format
projectID:region:instanceID
.
Start the Cloud SQL Auth Proxy
Start the Cloud SQL Auth Proxy in its own terminal so you can monitor its output. Replace
INSTANCE_CONNECTION_NAME
with the instance connection name you copied
in the previous step.
For Linux environments, use this command to launch the Cloud SQL Auth Proxy:
./cloud-sql-proxy INSTANCE_CONNECTION_NAME
In PowerShell on Windows, use this command to launch the Cloud SQL Auth Proxy:
.\cloud-sql-proxy.exe INSTANCE_CONNECTION_NAME
A message similar to the following appears:
Listening on 127.0.0.1:5432 for INSTANCE_CONNECTION_NAME Ready for new connections
Run the following command after replacing DB_NAME with the name of the Cloud SQL database:
psql "host=127.0.0.1 port=5432 sslmode=disable dbname=DB_NAME user=postgres"
At the Enter password: prompt, enter the password of your PostgreSQL account.
Verify that the PostgreSQL prompt appears. You have connected to your database using the psql client.
Return to the terminal window where you started the Cloud SQL Auth Proxy. You should see a message similar to the following:
New connection for myInstance
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select the
myinstance
instance to open the Instance details page. - In the icon bar at the top of the page, click Delete.
- In the Delete instance window, type your instance's name and then click Delete.
Optional cleanup steps
If you're not using the APIs that were enabled as part of this quickstart, you can disable them.
- APIs that were enabled within this quickstart:
- Cloud SQL Admin API
In the Google Cloud console, go to the APIs page.
Select the Cloud SQL Admin API and then click the Disable API button.
What's next
See how to troubleshoot information for the Cloud SQL Auth Proxy.
Learn more about the Cloud SQL Auth Proxy.
- Learn more about other connection options.