This tutorial shows you how build a simple Go application with CockroachDB and the Go pq driver.
Step 1. Start CockroachDB
Create a free cluster
- If you haven't already, sign up for a CockroachDB Cloud account.
- Log in to your CockroachDB Cloud account.
- On the Clusters page, click Create Cluster.
On the Create your cluster page, select Serverless.
Unless you change your monthly budget, this cluster will be free forever.
Click Create cluster.
Your cluster will be created in a few seconds and the Create SQL user dialog will display.
Create a SQL user
The Create SQL user dialog allows you to create a new SQL user and password.
- Enter a username in the SQL user field or use the one provided by default.
- Click Generate & save password.
- Copy the generated password and save it in a secure location.
Get the connection string
The Connect to cluster dialog shows information about how to connect to your cluster.
- Select General connection string from the Select option dropdown.
Open the General connection string section, then copy the connection string provided and save it in a secure location.
The sample application used in this tutorial uses system CA certificates for server certificate verification, so you can skip the Download CA Cert instructions.Note:
The connection string is pre-populated with your username, password, cluster name, and other details. Your password, in particular, will be provided only once. Save it in a secure place (Cockroach Labs recommends a password manager) to connect to your cluster in the future. If you forget your password, you can reset it by going to the SQL Users page.
- If you haven't already, download the CockroachDB binary.
$ cockroach start-single-node --advertise-addr 'localhost' --insecure
This starts an insecure, single-node cluster.
Take note of the following connection information in the SQL shell welcome text:
CockroachDB node starting at 2021-08-30 17:25:30.06524 +0000 UTC (took 4.3s) build: CCL v21.1.6 @ 2021/07/20 15:33:43 (go1.15.11) webui: http://localhost:8080 sql: postgresql://root@localhost:26257?sslmode=disable
You'll use the
sqlconnection string to connect to the cluster later in this tutorial.
--insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.
Step 2. Get the code
Clone the code's GitHub repo:
$ git clone https://github.com/cockroachlabs/hello-world-go-pq
Step 3. Run the code
You can now run the code sample (
main.go) provided in this tutorial to do the following:
- Create a table.
- Insert some rows into the table you created.
- Read values from the table.
Execute a batch of statements as an atomic transaction.
Note that CockroachDB may require the client to retry a transaction in the case of read/write contention. The CockroachDB Go client includes a generic retry function (
ExecuteTx()) that runs inside a transaction and retries it as needed. The code sample shows how you can use this function to wrap SQL statements.
Initialize the module:
$ cd hello-world-go-pq
$ go mod init basic-sample && go mod tidy
Run the code:
$ go run main.go
The output should be:
Balances: 1 1000 2 250 Success Balances: 1 900 2 350
Read more about using the Go pq driver.
You might also be interested in the following pages:
- Client Connection Parameters
- Connection Pooling
- Data Replication
- Fault Tolerance & Recovery
- Replication & Rebalancing
- Cross-Cloud Migration
- Automated Operations