firebasegoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-sqlknex.js

Cloud SQL with Firebase Functions V2


I'm receiving connect ENOENT /cloudsql/<proj>:us-west1:<instance>/.s.PGSQL.5432 When connecting to a Cloud SQL instance of PostgreSQL 15 from a Firebase V2 Callable function. I've looked everywhere and even reluctantly used ChatGPT but I cannot seem to fix the issue.

  1. I have checked my connection config (with the values substituted out)
export const createUnixSocketPool = (config: DatabaseConfig) => {
  return knex({
    client: "pg",
    connection: {
      user: config.user,
      password: config.password,
      database: "default",
      host: "/cloudsql/<proj-id>:us-west1:<instance-name>",
    },
    pool: { min: 0, max: 1 },
  });
};
  1. project@appspot.gserviceaccount.com has the appropriate Client role for Cloud SQL.
  2. Cloud SQL Admin API is enabled.

Solution

  • Your code configuration looks good, but the service configuration is not. You must tell to the service to open a connection with your Cloud SQL database, and then to open a unix socket for your code

    I didn't find anywhere how to set up a Cloud SQL connection for Firebase Functions V2. The most appropriate documentation page is on Cloud Functions.