Connecting via Golang works with any golang PostgreSQL library; in this example we use pgx.

go get

go build

Grab your API key:

  • Log into and select the database you would like to query
  • Click the green "Connect" button
  • Click on your API key (the "Password" field in the connect menu) to copy it

BITDOTIO_APIKEY=<your api key> ./bitdotio-golang-example to save the key to your environment.

To run the program: go run filename.go

package main

import (


func main() {
    apiKey, ok := os.LookupEnv("BITDOTIO_APIKEY") // "Password" from connect menu
    if !ok {
        panic("BITDOTIO_APIKEY not set in environment")
    userName := "< username>"
    dbName := "dliden/2020_Census_Reapportionment"
    connectUrl := fmt.Sprintf("postgres://%s:%[email protected]:5432/%s", userName, apiKey, dbName)
    conn, err := pgx.Connect(context.Background(), connectUrl)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
    defer conn.Close(context.Background())

    // Let's see how the census population of Nevada has changed
    sqlQuery := fmt.Sprintf(`SELECT "Year", "Resident Population" FROM "dliden/2020_Census_Reapportionment"."Historical Apportionment" WHERE "Name" = 'Nevada';`)
    rows, err := conn.Query(context.Background(), sqlQuery)

    if err != nil {
        fmt.Println("query error")

    defer rows.Close()

    for rows.Next() {
        var Year int
        var Population int

        err = rows.Scan(&Year, &Population)

        if err != nil {
        fmt.Println(Year, Population)

    err = rows.Err()
    if err != nil {

You can always see the latest version of this code at

Did this page help you?