Error connecting to Google Cloud SQL instance using Sequelize

68 views Asked by At

I'm encountering an issue while trying to connect to a Google Cloud SQL instance using Sequelize in a Node.js application. The connection was working fine previously, but now I'm getting an error message. Here's the function I'm using to establish the connection:

async function loadSequelize() {
  //This connection is for Google Cloud ONLY (IF You want to connect locally please change the connection config).
  const sequelize = new Sequelize(process.env.DEFAULT_DATABASE_NAME, process.env.DATABASE_USER, process.env.DATABASE_PASSWORD, {
    dialect: 'postgres',
    logging: false,
    // e.g. host: '/cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance'
    host: process.env.INSTANCE_UNIX_SOCKET,
    pool: {
        max: 2,
        min: 0,
        acquire: 10000,
        idle: 0
    },
    dialectOptions: {
        // e.g. socketPath: '/cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance'
        // same as host string above
        socketPath: process.env.INSTANCE_UNIX_SOCKET
    },
    logging: false,
    operatorsAliases: false
  });
  await sequelize.authenticate();
  return sequelize;
}

However, upon running the application, I'm encountering the following error:

ConnectionError [SequelizeConnectionError]: connect ENOENT /cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance.s.PGSQL.5432

I haven't made any changes to the connection configuration, and it was working fine yesterday. I also attempted to run the application locally, but the issue persists. Any insights into what might be causing this issue or how to troubleshoot it would be greatly appreciated. Thank you!

1

There are 1 answers

0
enocom On

For whatever reason it looks like your Unix socket path is wrong.

It should be:

/cloudsql/my-awesome-project:us-central1:my-cloud-sql-instance/.s.PGSQL.5432

The .s.PGSQL.5432 socket lives in a sub-directory named after your instance connection name. I don't know if sequalize expects you to add that full path or not, though.