PostgreSQL knex connection

Insert data by using the Node.js knex module to manage opening and closing a connection to Cloud SQL for PostgreSQL.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Node.js

To authenticate to Cloud SQL for PostgreSQL, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/**
 * Insert a vote record into the database.
 *
 * @param {object} pool The Knex connection object.
 * @param {object} vote The vote record to insert.
 * @returns {Promise}
 */
const insertVote = async (pool, vote) => {
  try {
    return await pool('votes').insert(vote);
  } catch (err) {
    throw Error(err);
  }
};

What's next

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