Search code examples
node.jspostgresqlgoogle-app-enginegoogle-cloud-sql

How to allow Google App Engine access Google Cloud PostgreSQL


I've found problem my node.js in Google App Engine, as it cannot connect to Google Cloud PostgreSQL:

Unhandled rejection SequelizeBaseError: connect ETIMEDOUT ***.***.***.***:5432
at (/app/node_modules/sequelize/lib/dialects/postgres/connection-manager.js:111)
at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:176)
at emitOne (events.js:96)
at Connection.emit (events.js:188)
at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:59)
at emitOne (events.js:96)
at Socket.emit (events.js:188)
at emitErrorNT (net.js:1281)
at _combinedTickCallback (internal/process/next_tick.js:74)
at process._tickCallback (next_tick.js:98)

I'm not sure how to add an IP address in the following (maybe I missed something to configure inside Google Cloud SQL).

enter image description here


Solution

  • There is some really nice documentation on how to connect App Engine applications to Cloud SQL instances. There you can find detailed steps on how to let your applications access the Cloud SQL instances, for each of the supported programming and SQL languages, both in local development and production environments.

    Assuming your connection issues are found when deploying your application to production (i.e. App Engine environment), here you have a summary of the main steps you will have to perform:

    1. Get the full name of you Cloud SQL instance with the command gcloud sql instances describe [INSTANCE_NAME]. It should be of the type <PROJECT_ID>:<REGION>:<INSTANCE_NAME>.
    2. Grant access to App Engine using the service account associated with your App Engine application. It should have the Cloud SQL Client or Cloud SQL Editor roles.
    3. Define the environment variables and beta settings for your SQL instance in the app.yaml file.

    (Add this code, edited to fit your parameters, to the app.yaml file):

    env_variables:
      SQL_USER: <YOUR_CLOUD_SQL_USER>
      SQL_PASSWORD: <YOUR_CLOUD_SQL_PASSWORD>
      SQL_DATABASE: <YOUR_CLOUD_SQL_DATABASE>
      INSTANCE_CONNECTION_NAME: <INSTANCE_CONNECTION_NAME>   # With format as <PROJECT_ID>:<REGION>:<INSTANCE_NAME>
    
    beta_settings:
      cloud_sql_instances: <INSTANCE_CONNECTION_NAME>   # With same format
    
    1. Add a Node.js PostreSQL client library to your application using the package.json file. The documentation suggests using Knex.js and pg, but presumably you will be able use the one of your choice.

    (Add this code to the package.json file):

    "dependencies": {
      "async": "2.5.0",
      "express": "4.15.4",
      "knex": "0.13.0",
      "pg": "7.3.0",
      "prompt": "1.0.0"
    },
    

    Once you have set the proper configuration for your application, you can have a look at this sample Node.js code. In order to find any missing item in your main script.

    If you deploy your application now, it should work as a charm.

    P.S.: Bear in mind that, as App Engine and Cloud SQL are two products of the Google Cloud Platform family, there is no need to authorize external networks in your Cloud SQL instance, so you can forget about that part too.