First step is to list the available db's to figure out which one to describe
and then copy the connectionName
for.
gcloud sql instances list
Assuming the db you want is called foo
you would then copy the connectionName as follows.
gcloud sql instances describe foo | grep connectionName | awk '{print $2}' | xclip -selection clipboard
Download cloud-sql-proxy here.
Optionally move cloud-sql-proxy
to your bin such that it's path accessible with the following command.
sudo mv cloud-sql-proxy /usr/local/bin
Setup application default credentials using this gcloud guide linked here.
Finally use the cloud-sql-proxy to forward a local port such that the db can be accessed locally.
cloud-sql-proxy --port 6432 {replace with the copied connectionName}
For something a bit more automatic. This uses
fzf
to select the instance name. Useful if you're a maniac and have multiple SQL instances.