Connect Local Environment With Cloud SQL Using Cloud SQL Auth Proxy

Edwin Tantawi
3 min readApr 18, 2023

--

Hi everyone, in this article I want to share my experience learning and using Google Cloud, especially with Cloud SQL services.

Now we will explore how to connect our Local Environment with Cloud SQL using Cloud SQL Auth Proxy. The Cloud SQL Auth proxy is a Cloud SQL connector that provides secure access to your instances without a need for Authorized networks or for configuring SSL.

For more information about Cloud SQL Auth Proxy, visit the actual documentation: https://cloud.google.com/sql/docs/postgres/sql-proxy

Let's get started to connect our local environment/machine to Cloud SQL using Cloud SQL Proxy.

Note: All of this article using Linux environment

Download and install the Cloud SQL Auth proxy

For downloading & installing the Cloud SQL Auth Proxy, please refer to the documentation. choose the right OS and follow the instructions.

Authenticaticating the Cloud SQL Auth proxy

The Cloud SQL Auth proxy provides several alternatives for authentication, depending on your environment. For this time, we will use credentials from gcloud CLI client.

Install 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

Start the Cloud SQL Auth proxy

There are 3 ways to start the Cloud SQL Auth Proxy there is TCP socket, Unix socket, and Docker. For this time we will use a TCP socket.

We can simply run:

./cloud-sql-proxy --address 127.0.0.1 --port 5432 [INSTANCE_CONNECTION_NAME]

That will make the Cloud SQL Auth proxy listen at 127.0.0.1:5432 for the local connection. or we can not define the — address and — port (this will use the localhost / 127.0.0.1 and the default port of the database)

Please make sure you are in the right working directory as the cloud SQL proxy file is located.

[INSTANCE_CONNECTION_NAME] Refer to Cloud SQL Connection Name. The connection name can be found in Cloud SQL instance overview on Google Cloud Console.

Make sure your Cloud SQL instance has a Public IP. We can also using private IP to connect using Cloud SQL Auth Proxy, but you must be in the same network as your Cloud SQL (we specify the Cloud SQL network only when we turn on Private IP on the instance). Because we are using our personal computers or local environment that is not in the same network as our Cloud SQL instance so we will use the Public IP.

For using Private IP, we just need to add --private-ip flag only.

When there are no show errors, that means we succeed to connect our Cloud SQL instance to our local environment. But where there go?

Connect with the database client

Next, we will try to connect to our Cloud SQL instance using a database client, for this time I will use postgresql-client (psql) because I using postgreSQL as a database engine in my Cloud SQL instance.

Make sure to open it in the other terminal because the step before we run the Cloud SQL Proxy, or we can run the Cloud SQL Proxy in the background process with add & at the end of the command.

./cloud-sql-proxy --address 127.0.0.1 --port 5432 [INSTANCE_CONNECTION_NAME] &

and we can now safely terminate with ctrl + c . But how we can end the process? we can run ps the command. and in there we can see the cloud-sql-proxy process is running, now copy the PID (process id) and run kill [PID] , change [PID] with your PID where the cloud-sql-proxy process running.

For Linux user we can simply run “sudo apt install postgresql-client”, otherwise you can use other database client to connect.

Now we can run:

psql --host 127.0.0.1 --port 5432 --user postgres

The host and port flag should be the same as we define when running the Cloud SQL Proxy, for the user flag use your database user, for this example I will use the default user of postgreSQL database.

And that is, now we can connect to Cloud SQL from our local environment using Cloud SQL Auth Proxy.

— Thanks, see you next time.

--

--

Edwin Tantawi

"If you have knowledge, let others light their candles at it" ~ Margaret Fuller