3

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?

3
  • Does this answer your question? Prisma DATABASE_URL error (Cloud Run + Cloud SQL) Commented Nov 14, 2022 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? Commented Nov 18, 2022 at 3: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 Commented Aug 17, 2023 at 16:50

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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
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 cloud.google.com/sql/docs/mysql/connect-build where you can enable the Cloud SQL Proxy as part of your migrate step.
This is a very useful comment. Thanks for sharing.
Got it to work! You are amazing! cloud.google.com/sql/docs/mysql/connect-build is exactly what I needed!
@MaxYuan can you share your solution?
- id: 'migrate-socket-and-migrate-db' name: '${_IMAGE_NAME}-proxy' dir: './server' entrypoint: '/bin/bash' args: - '-c' - | set -a source .env set +a /cloudsql/cloud_sql_proxy -instances=$$SQL_INSTANCE_CONNECTION_NAME -dir=/cloudsql & sleep 2; npx prisma migrate deploy
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.