I created Postgress + Spring app and It works perfectly locally (I had different docker-compose file fot it, but it works)
The target is: I want to deploy it to GCP.
I’m already running Postgress DB on GCP.
Done more or less with this tutorial
And tbh for me it looks good:
Now I want also put my api onto the GCP that will be using this DB
But firstly I need to test it locally.
I’m running my app with docker:
And when I try to run it connection fails:
What am I missing?
It’s my first time deploying DB + API onto GCP, so I’m kinda doing it blindly
So any reamrks will be apprecieated 😀
Sorry for so little information, but I honestly don’t know what more info I can give You :/
(I do not care wheter it is done perfectly or not)
2
Answers
To connect to a PostgreSQL instance on Cloud SQL I’d recommend that you read over the documentation because there are multiple ways of doing it.
Basically the connection requires:
In your particular case, my recommendation would be that you run locally a Cloud SQL Auth Proxy container along your application. It allows you to perform the connections by simply using the connection name and the credentials of a Service Account with client access (at least
roles/cloudsql.client
). Using this proxy also eliminates the need of manually whitelisting your source IP.It will also allow you to perform connections from both your local computer and whichever GCP service you choose rather similarly (depending on the service). As a best practice, the idea would be to connect to one host or another according to the environment variables read from your application.
I’d recommend that you go through this document to understand how the connection is configured with the Cloud SQL Auth Proxy, and this document to see how it’s done from Cloud Run (as an example).
You may try to check this link wherein in it uses JSON environment file.