2

I am trying to connect my cloud run app to cloud sql, here is my cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/npm'
  env:
    - DATABASE_URL=$_DATABASE_URL
  entrypoint: npx
  dir: './server'
  args:
    - 'prisma'
    - 'migrate'
    - 'deploy'

However, I keep on getting the error Please make sure your database server is running at '/cloudsql/learninfra001:us-central1:learninfra001-postgres':'5432'. Here is the _DATABASE_URL I use for substitution variable postgresql://postgres:password@localhost/db?schema=public&host=/cloudsql/learninfra001:us-central1:learninfra001-postgres I have made sure the following:

  • The default cloud run service account has Cloud SQL Client role
  • The database db is created
  • Within the cloudrun service, under connections, the Cloud SQL connections is pointing to the correct instance (learninfra001:us-central1:learninfra001-postgres)

Using white-listed public IP, I am able to connect to the DB. However, I just can't seem to get cloud run to work. Is there anything else I could check? Or is there a way to get more logging to see why it is not connecting?

Max Yuan
  • 121
  • 1
  • 1
  • 6
  • Does this answer your question? [Prisma DATABASE\_URL error (Cloud Run + Cloud SQL)](https://stackoverflow.com/questions/67976986/prisma-database-url-error-cloud-run-cloud-sql) – Robert G Nov 14 '22 at 21:25
  • Hey @RobertG I updated my question. Unfortunately that didn't work for me. Do you have any other ideas I could try? – Max Yuan Nov 18 '22 at 03:11
  • for mysql for my database_url secret/env variable - I had to add the query string ?socket=/cloudsql/{connectionname} connection name being the instance name you can copy from the console in cloud sql – James Daly Aug 17 '23 at 16:50

1 Answers1

2

In short, you'll need to enable a Cloud SQL connection to your Cloud SQL instance from Cloud Build.

See https://cloud.google.com/sql/docs/mysql/connect-build for details.

enocom
  • 1,496
  • 1
  • 15
  • 22
  • I tried to use this but it still unable to connect. This is the link that I tried to use. `postgresql://postgres:password@localhost/db?schema=public&host=/cloudsql/learninfra001:us-central1:learninfra001-postgres` I have made sure service account has cloud sql client role and that cloud run cloudsql connections is pointing to the right cloud sql instance. During cloud build, I am still getting `Please make sure your database server is running at '/cloudsql/learninfra001:us-central1:learninfra001-postgres':'5432'.` What else should I look into? @enocom – Max Yuan Nov 18 '22 at 02:31
  • Looks like you're trying to connect to Cloud SQL from Cloud Build (not Cloud Run). Your URL will work for Cloud Run. For Cloud Build see https://cloud.google.com/sql/docs/mysql/connect-build where you can enable the Cloud SQL Proxy as part of your migrate step. – enocom Nov 18 '22 at 16:30
  • 2
    Got it to work! You are amazing! cloud.google.com/sql/docs/mysql/connect-build is exactly what I needed! – Max Yuan Nov 19 '22 at 08:16