Search code examples
postgresqlgoogle-app-enginestrapi

Cannot connect to Postgres Database from Strapi on Google App Engine


Need help connecting to my Google SQL Postgres Database from Google App Engine. I've followed https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/google-app-engine.html but keep running into the following error:

2022-09-27 16:11:32 default[20220928t020816]  [2022-09-27 16:11:32.307] debug: ⛔️ Server wasn't able to start properly.
2022-09-27 16:11:32 default[20220928t020816]  [2022-09-27 16:11:32.309] error: connect ECONNREFUSED /cloudsql/project-id:us-central1:database-id/.s.PGSQL.5432

database.ts

export default ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: env('DATABASE_HOST', '127.0.0.1'),
      port: env.int('DATABASE_PORT', 5432),
      database: env('DATABASE_NAME', 'database-id'),
      user: env('DATABASE_USERNAME', 'strapi'),
      password: env('DATABASE_PASSWORD', 'strapi'),
      ssl: env.bool('DATABASE_SSL', false),
    },
  },
});

app.yaml

runtime: nodejs16

instance_class: F1

env_variables:
  ADMIN_JWT_SECRET: 'XXXXXXXXXXXXX'
  API_TOKEN_SALT: 'XXXXXXXXXXXXX'
  APP_KEYS: 'XXXXXXXXXXXXX'
  DATABASE_HOST: '/cloudsql/project-id:us-central1:database-id'
  DATABASE_PORT: '5432'
  DATABASE_NAME: 'database-id'
  DATABASE_USERNAME: 'postgres'
  DATABASE_PASSWORD: 'XXXXXXXXXXXXX'
  DATABASE_SSL: 'false'
  HOST: '0.0.0.0'
  JWT_SECRET: 'XXXXXXXXXXXXX'
  NODE_ENV: 'production'
  PORT: '1337'

beta_settings:
  cloud_sql_instances: 'project-id:us-central1:database-id'

Solution

  • One of the causes of error: connect ECONNREFUSED and the most common one when connecting to Cloud SQL using Unix Sockets (as is the case here) is that your GCP project does not have the Cloud SQL Admin API enabled.

    Enable the Cloud SQL Admin API and that should help resolve the connectivity issue.