1

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

  • Can you verify the things mentioned in [this thread](https://www.googlecloudcommunity.com/gc/Databases/Ent-package-auto-migration-error-quot-dial-tcp-127-0-0-1-5432/m-p/505912)? – Roopa M May 18 '23 at 07:17
  • What's you Cloud Run configuration? WHat's your Cloud SQL configuration (public/private IPs)? – guillaume blaquiere May 18 '23 at 09:09
  • @guillaumeblaquiere the Cloud SQL config is using public IP, and I already Add the SQL connection to Cloud Run manually – Fidriyanto Rizkillah May 19 '23 at 05:14

1 Answers1

1

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.

enocom
  • 1,496
  • 1
  • 15
  • 22
  • I think the connection is working just fine, the error occurs when I try to perform the migration. I added the screenshot of the snippet, I'm fairly new to both golang and GCP, so sorry if there's a misunderstanding from my side – Fidriyanto Rizkillah May 19 '23 at 05:27
  • i also can connect to the cloud sql via tableplus or dbeaver – Fidriyanto Rizkillah May 19 '23 at 18:18
  • Take a look at https://cloud.google.com/sql/docs/postgres/connect-run#public-ip-default_1 which has some more details. In short, in Cloud Run, you'll have to connect over a Unix socket, or use the Go Connector I linked to. – enocom May 19 '23 at 21:24