Search code examples
google-cloud-platformstrapi

Connecting to Google Cloud PostgresSQL Server for local Strapi Application


I have a Google cloud Postgres server set up, and under the production environment I am able to connect to the server correctly. However, when I try to connect to the same cloud server in my local server, it doesn't seem to work. Here's the configuration for my database.js

module.exports = ({ env }) => {
  return {
    defaultConnection: "default",
    connections: {
      default: {
        connector: "bookshelf",
        settings: {
          client: "postgres",
          host: `/cloudsql/${env("INSTANCE_CONNECTION_NAME")}`,

          database: env("DATABASE_NAME"),
          username: env("DATABASE_USERNAME"),
          password: env("DATABASE_PASSWORD"),
        },
        options: {},
      },
    },
  };
};

I also have the app.yaml set up as normal. I have also created the .env file to store the relevant env information.

The error I am getting is

error Error: connect ENOENT /cloudsql/my-app-286221:us-central1:blm-resources/.s.PGSQL.5432

Does Strapi in local development support connection to cloud database? Or am I doing something wrong here.


Solution

  • This shouldn’t be a strapi issue. First you need to have an access from outside to google cloud postgres database. I’m not familiar with google cloud services, but from documentation there seem to be a couple of things to do to grant access to database.

    More info from documentation:
    https://cloud.google.com/sql/docs/postgres/connect-external-app#appaccessIP

    Basically you grant access for connection from outside and then you add that connection information to your strapi config file.

    I noticed your host: is not pointing to http:// or https:// but to some google server’s local address.