Connection refused when running go migrate in cloud run

236 Views Asked by At

I'm trying to run db migration with golang migrate for Cloud SQL, on cloud run and it shows this log

cannot create migration: dial tcp 127.0.0.1:5432: connect: connection refused

The URI format I'm using is postgres://{user}:{password}@cloudsql/{host}:{port}/{db_name} The host is from the connection name from Cloud SQL project-name:region-name:instance-name

Is there anything wrong with my setup, or am I missing something on the cloud run setup? Or there is better way to do the db migration from inside the go project? Btw, i'm deploying the project to cloud run via github action to artifact registry

Edit: Code snippet of the go and db connection

1

There are 1 best solutions below

3
enocom On

You'll need to either use the Go Connector or use Cloud Run's built-in Cloud SQL integration (which runs the Cloud SQL Auth Proxy under the hood). By default, Cloud SQL IP addresses are firewalled off from the internet, which is why your current approach isn't working.