title: How to connect to Cloud SQL from Cloud Build to run knex database migration?
body: <p>I investigated <a href="https://stackoverflow.com/questions/52352103/run-node-js-database-migrations-on-google-cloud-sql-during-google-cloud-build">this</a> question on StackOverflow but unfortunately to me nothing helps.</p>
  <p>I have <code>cloudbuild.yaml</code> file</p>
  <pre><code>steps:
    - name: 'node:14.16.0'
      entrypoint: 'yarn'
      id: yarn-install
      args: ['install']
      waitFor: [&quot;-&quot;]
  
    - name: gcr.io/cloud-builders/yarn
      id: proxy-install
      entrypoint: sh
      args:
        - &quot;-c&quot;
        - &quot;wget https://storage.googleapis.com/cloudsql-proxy/v1.23.0/cloud_sql_proxy.linux.amd64 -O /workspace/cloud_sql_proxy &amp;&amp; chmod +x /workspace/cloud_sql_proxy&quot;
      waitFor: [&quot;-&quot;]
  
    - id: migrate
      name: gcr.io/cloud-builders/yarn
      env:
        - NODE_ENV=$_NODE_ENV
        - DB_NAME=$_DB_NAME
        - DB_USER=$_DB_USER
        - DB_PASSWORD=MY_FAKE_PASSWORD
        - CLOUD_SQL_CONNECTION_NAME=$_CLOUD_SQL_CONNECTION_NAME
      entrypoint: sh
      args:
        - &quot;-c&quot;
        - &quot;(./workspace/cloud_sql_proxy -dir=/workspace -instances=$_CLOUD_SQL_CONNECTION_NAME &amp; sleep 2) &amp;&amp; yarn run knex migrate:latest&quot;
      timeout: &quot;1200s&quot;
      waitFor: [&quot;yarn-install&quot;, &quot;proxy-install&quot;]
  </code></pre>
  <p>I want to connect to my Cloud SQL database to apply schema migration by using <code>yarn run knex migrate:latest</code>.</p>
  <p>But it fails on the <em>migrate</em> step</p>
  <p>Logs from Cloud Build</p>
  <pre><code>info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
  error Command failed with exit code 1.
      at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1145:16)
  Error: connect ENOENT /cloudsql/project:us-east1:project-posgresql1/.s.PGSQL.5432
  Using environment: production
  Working directory changed to /workspace/src/infrastructure/knex
  Requiring external module ts-node/register
  $ ./node_modules/knex/bin/cli.js --knexfile=./src/infrastructure/knex/knex.config.ts migrate:latest --env production migrate:latest
  yarn run v1.22.5
  sh: 1: ./workspace/cloud_sql_proxy: not found
  Already have image (with digest): gcr.io/cloud-builders/yarn
  </code></pre>
  <p>I don't know how to debug it correctly... Could you help me to find the root cause of the problem?</p>
  <p>P.S.</p>
  <p>...@cloudbuild.gserviceaccount.com has the following roles</p>
  <ul>
  <li>Cloud Build Service Account</li>
  <li>Cloud SQL Client</li>
  <li>Service Account User</li>
  <li>Cloud Run Admin</li>
  <li>Secret Manager Secret Accessor</li>
  </ul>
  <p>P.S.S <a href="http://knexjs.org/" rel="nofollow noreferrer">knex</a> is a JavaScript query builder for SQL-like databases</p>