Build a Node.js App with CockroachDB and the Node.js pg Driver

This tutorial shows you how build a simple Node.js application with CockroachDB and the Node.js pg driver.

We have tested the Node.js pg driver enough to claim beta-level support. If you encounter problems, please open an issue with details to help us make progress toward full support.

Step 1. Start CockroachDB

  1. If you haven't already, download the CockroachDB binary.
  2. Run the cockroach demo command:

    $ cockroach demo \

    This starts a temporary, in-memory cluster and opens an interactive SQL shell to the cluster.

  3. Take note of the (sql/tcp) connection string in the SQL shell welcome text:

    # Connection parameters:
    #   (console)
    #   (sql)     postgres://root:admin@?host=%2Fvar%2Ffolders%2Fk1%2Fr048yqpd7_9337rgxm9vb_gw0000gn%2FT%2Fdemo255013852&port=26257
    #   (sql/tcp) postgres://root:admin@    

    You will use it in your application code later.

Step 2. Create a database

  1. In the SQL shell, create the bank database that your application will use:

  2. Create a SQL user for your app:

    > CREATE USER <username> WITH PASSWORD <password>;

    Take note of the username and password. You will use it in your application code later.

  3. Give the user the necessary permissions:

    > GRANT ALL ON DATABASE bank TO <username>;

Step 3. Install client driver

To let your application communicate with CockroachDB, install the Node.js pg driver:

$ npm install pg

Step 4. Get the code

Download the sample code directly, or clone the code's GitHub repository.

Step 5. Update the connection parameters

Open the app.js file, and edit the connection configuration parameters:

  • Replace the value for user with the user you created earlier.
  • Replace the value for password with the password you created for your user.
  • Replace the value for port with the port to your cluster.
  • At the top of the file, uncomment the const fs = require('fs'); line.

    This line imports the fs Node module, which enables you to read in the CA cert that you downloaded from the CockroachCloud Console.

  • Replace the value for user with the user you created earlier.

  • Replace the value for password with the password you created for your user.

  • Replace the value for host with the name of the CockroachCloud Free host (e.g., host: '').

  • Replace the value for port with the port to your cluster.

  • Replace the value for database with the database that you created earlier, suffixed with the name of the cluster (e.g., database: '{cluster_name}.bank').

  • Remove the existing ssl object and its contents.

  • Uncomment the ssl object with the ca key-value pair, and edit the fs.readFileSync('/certs/ca.crt').toString() call to use the path to the cc-ca.crt file that you downloaded from the CockroachCloud Console.

Step 6. Run the code

The sample code creates a table, inserts some rows, and then reads and updates values as an atomic transaction.

Here are the contents of app.js:

//For secure connection:
// const fs = require('fs');
const { Pool } = require("pg");

// Configure the database connection.

const config = {
  user: "max",
  password: "roach",
  host: "localhost",
  database: "bank",
  port: 26257,
  ssl: {
    rejectUnauthorized: false,
  //For secure connection:
  /*ssl: {
        ca: fs.readFileSync('/certs/ca.crt')

// Create a connection pool

const pool = new Pool(config);

// Wrapper for a transaction.  This automatically re-calls the operation with
// the client as an argument as long as the database server asks for
// the transaction to be retried.

async function retryTxn(n, max, client, operation, callback) {
  await client.query("BEGIN;");
  while (true) {
    if (n === max) {
      throw new Error("Max retry count reached.");
    try {
      await operation(client, callback);
      await client.query("COMMIT;");
    } catch (err) {
      if (err.code !== "40001") {
        return callback(err);
      } else {
        console.log("Transaction failed. Retrying transaction.");
        await client.query("ROLLBACK;", () => {
          console.log("Rolling back transaction.");
        await new Promise((r) => setTimeout(r, 2 ** n * 1000));

// This function is called within the first transaction. It creates a table and inserts some initial values.

async function initTable(client, callback) {
  await client.query(
    "CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT);",
  await client.query(
    "INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250);",
  await client.query("SELECT id, balance FROM accounts;", callback);

async function transferFunds(client, callback) {
  const from = 1;
  const to = 2;
  const amount = 100;
  const selectFromBalanceStatement = "SELECT balance FROM accounts WHERE id = $1 ;";
  const selectFromValues = [from];
  await client.query(selectFromBalanceStatement, selectFromValues, (err, res) => {
    if (err) {
      return callback(err);
    } else if (res.rows.length === 0) {
      console.log("account not found in table");
      return callback(err);
    var acctBal = res.rows[0].balance;
    if (acctBal < amount) {
      return callback(new Error("insufficient funds"));

  const updateFromBalanceStatement = "UPDATE accounts SET balance = balance - $1 WHERE id = $2 ;";
  const updateFromValues = [amount, from];
  await client.query(updateFromBalanceStatement, updateFromValues, callback);

  const updateToBalanceStatement = "UPDATE accounts SET balance = balance + $1 WHERE id = $2 ;";
  const updateToValues = [amount, to];
  await client.query(updateToBalanceStatement, updateToValues, callback);

  const selectBalanceStatement = "SELECT id, balance FROM accounts;";
  await client.query(selectBalanceStatement, callback);

// Run the transactions in the connection pool

(async () => {
  // Connect to database
  const client = await pool.connect();

  // Callback
  function cb(err, res) {
    if (err) throw err;

    if (res.rows.length > 0) {
      console.log("New account balances:");
      res.rows.forEach((row) => {

  // Initialize table in transaction retry wrapper
  console.log("Initializing table...");
  await retryTxn(0, 15, client, initTable, cb);

  // Transfer funds in transaction retry wrapper
  console.log("Transferring funds...");
  await retryTxn(0, 15, client, transferFunds, cb);

  // Exit program
})().catch((err) => console.log(err.stack));

Note that all of the database operations are wrapped in the retryTxn function. This function attempts to commit statements in the context of an explicit transaction. If a retry error is thrown, the wrapper will retry committing the transaction, with exponential backoff, until the maximum number of retries is reached (by default, 15).

To run the code:

$ node app.js

The output should be:

Initializing table...
New account balances:
{ id: '1', balance: '1000' }
{ id: '2', balance: '250' }
Transferring funds...
New account balances:
{ id: '1', balance: '900' }
{ id: '2', balance: '350' }

What's next?

Read more about using the Node.js pg driver.

You might also be interested in the following pages:

YesYes NoNo