This tutorial shows you how build a simple Hello World Go application with CockroachDB and the Go pgx driver.
Step 1. Start CockroachDB
Create a free cluster
- If you haven't already, sign up for a CockroachCloud account.
- Log in to your CockroachCloud account.
- On the Clusters page, click Create Cluster.
On the Create your cluster page, select CockroachCloud Free.
Note:This cluster will be free forever.
(Optional) Select a cloud provider (GCP or AWS) in the Additional configuration section.
(Optional) Select a region in the Additional configuration section. For optimal performance, select the cloud provider region closest to the region in which you are running your application.
Click Create your free cluster.
Your cluster will be created in approximately 20-30 seconds.
Set up your cluster connection
Navigate to the cluster's SQL Users page, and create a new user, with a new password.
Navigate to the Cluster Overview page, select Connect, and, under the Connection String tab, download the cluster certificate.
Take note of the connection string provided. You'll use it to connect to the database later in this tutorial.
- If you haven't already, download the CockroachDB binary.
Run the
cockroach start-single-node
command:$ 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
sql
connection string to connect to the cluster later in this tutorial.
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 2. Get the code
Clone the code's GitHub repo:
$ git clone https://github.com/cockroachlabs/hello-world-go-pgx/
The main.go
file contains all of the code for the sample Hello World app:
package main
import (
"bufio"
"context"
"log"
"os"
"github.com/jackc/pgx/v4"
)
func main() {
// Read in connection string
scanner := bufio.NewScanner(os.Stdin)
log.Println("Enter a connection string: ")
scanner.Scan()
connstring := scanner.Text()
// Attempt to connect
config, err := pgx.ParseConfig(os.ExpandEnv(connstring))
if err != nil {
log.Fatal("error configuring the database: ", err)
}
conn, err := pgx.ConnectConfig(context.Background(), config)
if err != nil {
log.Fatal("error connecting to the database: ", err)
}
defer conn.Close(context.Background())
log.Println("Hey! You successfully connected to your CockroachDB cluster.")
}
The main
method of this program does the following:
- Attempts to connect to a running cluster, given a connection string.
- Prints a message to the terminal about the connection status.
Step 3. Run the code
Initialize and run the app:
$ go mod init basic-sample && go mod tidy
$ go run main.go
The program will prompt you for a connection string to the database:
Enter a connection string:
Enter postgresql://root@localhost:26257?sslmode=disable
(the sql
connection URL provided in the cockroach
welcome text).
Enter the connection string provided in the Connection info window of the CockroachCloud Console.
You need to provide a SQL user password in order to securely connect to a CockroachCloud cluster. The connection string should have a placeholder for the password (<ENTER-PASSWORD>
).
After entering the connection string, the program will execute.
The output should look like this:
Hey! You successfully connected to your CockroachDB cluster.
See also
You might also be interested in the following pages: