Create a socket connection when using Node.js

Opens a Unix socket connection to Cloud SQL for PostgreSQL by using the Node.js knex module.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample

Node.js

const createUnixSocketPool = async config => {
  const dbSocketPath = process.env.DB_SOCKET_PATH || '/cloudsql';

  // Establish a connection to the database
  return Knex({
    client: 'pg',
    connection: {
      user: process.env.DB_USER, // e.g. 'my-user'
      password: process.env.DB_PASS, // e.g. 'my-user-password'
      database: process.env.DB_NAME, // e.g. 'my-database'
      host: `${dbSocketPath}/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
    },
    // ... Specify additional properties here.
    ...config,
  });
};

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.