How to connect to a Google Cloud PostgreSQL 15.2 SQL instance remotely from Windows

How to connect to a Google Cloud PostgreSQL 15.2 SQL instance remotely from Windows

I had previously .installed PostgreSQL 15.2 on my Windows 11 machine

Stop a running instance of PostgreSQL on locahost:5432

net stop postgresql-x64-15

Create a new SQL instance at https://console.cloud.google.com/sql/

I choose PostgreSQL, it's similar to MySQL.

Create a database and a user for that instance and note down the passwords.

Note down the connection name which would be something like this : somename-name-987654:asia-south1:instanceName

It'll be in the Overview tab on the left-hand menu.

Authenticate and acquire credentials for the API - gcloud auth application-default login

Download Google Cloud SQL proxy from https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/v2.7.0/cloud-sql-proxy.x64.exe and rename it to cloud-sql-proxy.exe in C:\ (in C: drive just for simplicity)

Run it like this : PS C:> .\cloud-sql-proxy.exe somename-name-987654:asia-south1:instanceName

This will run the proxy at 127.0.0.1 on port 5432 which is the same host and port for a local PostgreSQL running on your Windows, hence I had to stop the existing PostgreSQL running on my machine. Instead, run your local Windows-based PostgreSQL instance on a different port if you want local and Google's local proxy running at the same time.

More info at: https://cloud.google.com/sql/docs/postgres/connect-instance-auth-proxy

Enter the details in Table Plus - replace myUser & myDatabase with your username you created in Google Cloud Console.

Test and it should connect. It's super fast on my laptop so as good as localhost.

This is super useful if you want to just have the database hosted on a separate location whereas the application can be elsewhere (cloud or otherwise).