Skip to main content
This tutorial shows you how build a simple Go application with CockroachDB and the upper/db data access layer.

Before you begin

  1. .
  2. Start up a or local cluster.
  3. Choose the instructions that correspond to whether your cluster is secure or insecure:
The --insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.

Step 1. Create the maxroach user and bank database

Start the :
In the SQL shell, issue the following statements to create the maxroach user and bank database:
Give the maxroach user the necessary permissions:
Exit the SQL shell:

Step 2. Generate a certificate for the maxroach user

Create a certificate and key for the maxroach user by running the following command:
The code samples will run with maxroach as the user.

Step 3. Run the Go code

The sample code shown below uses upper/db to map Go-specific objects to SQL operations. Specifically, the code:
  • Creates the accounts table, if it does not already exist.
  • Deletes any existing rows in the accounts table.
  • Inserts two rows into the accounts table.
  • Prints the rows in the accounts table to the terminal.
  • Deletes the first row in the accounts table.
  • Updates the rows in the accounts table within an explicit .
  • Prints the rows in the accounts table to the terminal once more.
Note that the sample code also includes a function that simulates a transaction error (crdbForceRetry()). Upper/db’s CockroachDB adapter when transaction errors are thrown. As a result, this function forces a transaction retry. To run the code, copy the sample above, or download it directly.
To clone a version of the code below that connects to insecure clusters, run the following command:git clone https://github.com/cockroachlabs/hello-world-go-upperdb/Note that you will need to edit the connection string to use the certificates that you generated when you set up your secure cluster.

Step 1. Create the maxroach user and bank database

Start the :
In the SQL shell, issue the following statements to create the maxroach user and bank database:
Give the maxroach user the necessary permissions:
Exit the SQL shell:

Step 2. Run the Go code

The sample code shown below uses upper/db to map Go-specific objects to SQL operations. Specifically, the code:
  • Creates the accounts table, if it does not already exist.
  • Deletes any existing rows in the accounts table.
  • Inserts two rows into the accounts table.
  • Prints the rows in the accounts table to the terminal.
  • Deletes the first row in the accounts table.
  • Updates the rows in the accounts table within an explicit .
  • Prints the rows in the accounts table to the terminal once more.
Note that the sample code also includes a function that simulates a transaction error (crdbForceRetry()). Upper/db’s CockroachDB adapter when transaction errors are thrown. As a result, this function forces a transaction retry. Copy the code or download it directly. To clone a version of the code below that connects to insecure clusters, run the following command: git clone https://github.com/cockroachlabs/hello-world-go-upperdb/ Change to the directory where you cloned the repo and get the dependencies with go mod init:
Then run the code:
The output should look similar to the following:
Note that the forced transaction errors result in errors printed to the terminal, but the transactions are retried until they succeed.

What’s next?

Read more about upper/db: You might also be interested in the following pages: