Error while connecting to Cloud SQL PostgreSQL instance on GCE VM with Knex.js

Viewed 409

I'm using Knex.js to connect to a Cloud SQL PostgreSQL instance, and I have double checked all my information a million times at this point with just a complete lack of understanding about why this error is happening. Here is my code:

const connectWithUnixSockets = (config) => {
    const dbSocketPath = "/cloudsql"
  
    // Establish a connection to the database
    return Knex({
      client: 'pg',
      connection: {
        user: 'postgres', // default user provided by google
        password: Config.DBPass, // password for the account
        database: 'ceclub', // database name
        host: `${dbSocketPath}/Connection-name`,//I have checked this part a thousand times, it's correct.
      },
      // ... Specify additional properties here.
      ...config
    });
  }
let config = {
    pool: {
        max:5,
        min:5,
        acquireTimeoutMillis:60000,
    },
    createTimeoutMillis:30000,
    idleTimeoutMillis:600000,
    createRetryIntervalMillis:200
}
 let knex = connectWithUnixSockets(config);

Before it gets asked I did just copy this from the gcloud functions documentation. I've made sure to give myself admin permissions and the service account for my VM admin permissions for Cloud SQL, and I have authorized the IP of my VM. However, everytime I run this program I end up with the following issue:

Error: connect ENOENT /cloudsql/connection-name/.s.PGSQL.5432
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'connect',
  address: '/cloudsql/connection-name/.s.PGSQL.5432'
}

I have double checked the documentation a bunch and I am just at a loss for what I might have messed up. Does anyone have any ideas?

1 Answers

You mention both Cloud Functions but also a VM, so it's not clear to me which you are using to try to connect.

In Cloud Functions, the environment provides a /cloudsql directory for you that connects to your instance.

If you are running locally (or in a different enviroment such as a GCE instance), you can use the Cloud SQL proxy to create a similar directory. Check out the source code for the sample you referenced (with instructions for running with the proxy) in this GitHub repo.

Additionally, you can check out the "Authorization with the Cloud SQL Proxy" page for detailed instructions on how to use the proxy.

Related