This page shows you how to connect to Cloud SQL from a local test environment using the Cloud SQL Proxy. Connecting through the proxy enables you to test an App Engine application in your local environment, or establish a secure connection for database administration.
Do not use these instructions to set up the proxy for a production environment. For more connection options, see Connecting using the Cloud SQL Proxy.
Before you begin
Before performing the steps in this quickstart, complete the following tasks:
- Create a Google Cloud project and a Cloud SQL instance.
- If you are using a pre-existing project, ensure that your Google Cloud
user is an owner of your project, or has an IAM role other than
Cloud SQL Viewer
. Otherwise, set up the proxy using the instructions outlined in Connecting using the Cloud SQL Proxy. -
The client enables you to test your connection to your instance. The
psql
client can be installed with the server package or alone. See the PostgreSQL installation instructions. Install and authenticate the gcloud command-line tool.
- If you haven't already, install the
gcloud
command-line tool. See thegcloud
installation instructions. - Initialize the
gcloud
tool:gcloud init
- Authenticate the
gcloud
tool:gcloud auth login
- If you haven't already, install the
Install the Cloud SQL Proxy client on your local machine
The 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 are unsure whether your
machine is running a 64-bit kernel, use the
uname -a
command for Linux or macOS, or click Computer > Properties in
the Start Menu for Windows.
Linux 64-bit
- Download the proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
- Make the proxy executable:
chmod +x cloud_sql_proxy
Linux 32-bit
- Download the proxy:
wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.386 -O cloud_sql_proxy
- Make the proxy executable:
chmod +x cloud_sql_proxy
macOS 64-bit
- Download the proxy:
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.amd64
- Make the proxy executable:
chmod +x cloud_sql_proxy
macOS 32-bit
- Download the proxy:
curl -o cloud_sql_proxy https://dl.google.com/cloudsql/cloud_sql_proxy.darwin.386
- Make the proxy executable:
chmod +x cloud_sql_proxy
Windows 64-bit
Right-click https://dl.google.com/cloudsql/cloud_sql_proxy_x64.exe and select Save Link As to download the proxy. Rename the file tocloud_sql_proxy.exe
.
Windows 32-bit
Right-click https://dl.google.com/cloudsql/cloud_sql_proxy_x86.exe and select Save Link As to download the proxy. Rename the file tocloud_sql_proxy.exe
.
Docker proxy image
For convenience, the Cloud SQL team maintains several container images that contain the Cloud SQL Proxy for use by our customers. For more information about these images, see the Cloud SQL Proxy repo on GitHub. You can pull the latest image to your local machine using Docker with the following command:docker pull gcr.io/cloudsql-docker/gce-proxy:1.19.1
Other OS
For other operating systems not included here, you can compile the proxy from source.Get the instance connection name
Go to the Cloud SQL Instances page in the Google Cloud Console.
Click the instance name to open its Instance details page.
Under Connect to this instance, note the Instance connection name.
Start the proxy
Start the 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.
./cloud_sql_proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:5432
A message similar to the following appears:
Listening on 127.0.0.1:5432 for myproject:us-central1:myinstance". Ready for new connections
Connect to your database using the psql client
This section is optional, but is recommended for testing your connection. See the PostgreSQL installation instructions.
In a different terminal window from where you started the proxy, run the following command, replacing<USERNAME>
with your PostgreSQL username,
and <DB_NAME>
with the name of the database to connect to.
psql "host=127.0.0.1 port=5432 sslmode=disable dbname=<DB_NAME> user=<USER_NAME>"
The psql prompt appears, and a message similar to the following is in the proxy terminal:
New connection for "myproject:us-central1:myinstance"
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.
- Go to the Cloud SQL Instances page 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
myinstance
, then click Delete to delete the instance.You cannot reuse an instance name for about 7 days after an instance is deleted.