title: Cloud SQL instance connection working locally, but not on App Engine
body: <p>I am trying to host an API implemented as a Node.js app on Google Cloud App Engine. The database that the API utilizes is a PostgreSQL 9.6 database which is hosted with a Cloud SQL instance. The database is connected to the Node.js API through <code>Knex</code>. </p>
  
  <p>When hosted on App Engine, the API endpoints that do not require any contact with the database work fine. However, when the database needs to be contacted to complete the API call, the following error appears in the logs:</p>
  
  <blockquote>
    <p>Unhandled rejection TimeoutError: Knex: Timeout acquiring a
    connection. The pool is probably full. Are you missing a
    .transacting(trx) call?</p>
  </blockquote>
  
  <p>However, if I use <code>Knex</code> to connect to the Cloud SQL instance locally and address the API through localhost:3000, an API call requiring the database completes perfectly fine. This verifies that the PostgreSQL database instance on Cloud SQL is working fine and that something about hte Node.js app being hosted on App Engine is making a connection to the Cloud SQL instance fail.</p>
  
  <p>I connect to the database like so:</p>
  
  <pre><code>module.exports = require('knex')({
          client: 'pg',
          debug:  true,
          connection: {
            host : '35.194.32.254', 
            user : 'postgres',
            password : 'mypassword',
            database : 'mydatabase'
          },
        });
  </code></pre>
  
  <p>Again, local connections succeed, App Engine connections fail.</p>
  
  <p>My <code>app.yaml</code> file is as follows:</p>
  
  <pre><code>runtime: nodejs
  env: flex
  beta_settings:
    cloud_sql_instances: my-project-12345:us-central1:mydatabase
  </code></pre>
  
  <p>To ensure this is not a permission issue, I granted the App Engine service account <code>my-project-12345@appspot.gserviceaccount.com</code> the Cloud SQL Client permission. I've also verified that the Cloud SQL instance is deployed in the same region as the Node.js app on App Engine. </p>
  
  <p><strong>My question is this</strong>: Why can't Node.js on the App Engine connect to the Cloud SQL instance if it can connect to it locally?</p>