Skip to content
Connect to Managed ClickHouse® with Go

Connect to Managed ClickHouse® with Go

To connect to your Managed ClickHouse® service with Go, you can use the native protocol or the HTTPS protocol in specific cases. This article provides you with instructions for both scenarios.

Prerequisites

Go 1.21 or later, with toolchain auto-switching enabled (the current clickhouse-go release requires a recent Go toolchain)

Install the ClickHouse Go module

To install the ClickHouse Go module, run the following command:

go get github.com/ClickHouse/clickhouse-go/v2

Note

Check your toolchain with go version before installing. The current clickhouse-go release requires the Go version listed in the prerequisites above, and go get fails on an older toolchain. Upgrade Go rather than pinning an old driver release: the 2022-era v2.2 tag no longer receives fixes and does not track the protocol changes in ClickHouse 25.x.

Connect with the native protocol

Connect to the service

Replace the placeholders in the code with meaningful information on your service connection and run the code.

package main
import "fmt"
import "log"
import "crypto/tls"
import "github.com/ClickHouse/clickhouse-go/v2"
func main() {
    host := "HOST"
    native_port := NATIVE_PORT
    database := "DATABASE_NAME"
    username := "USERNAME"
    password := "PASSWORD"
    tls_config := &tls.Config{}
    conn, err := clickhouse.Open(&clickhouse.Options{
        Addr: []string{fmt.Sprintf("%s:%d", host, native_port)},
        Auth: clickhouse.Auth{
            Database: database,
            Username: username,
            Password: password,
        },
        TLS: tls_config,
    })
    if err != nil {
        log.Fatal(err)
    }
    v, err := conn.ServerVersion()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(v)
}

Connect with HTTPS

Important

By default, connections are established over the native protocol. Current versions of clickhouse-go support HTTPS in both the native API and the database/SQL API: enable it either by modifying the DSN to include the HTTPS protocol or by specifying the protocol in the connection options.

Connect to the service

Replace the placeholders in the code with meaningful information on your service connection and run the code.

package main
import "database/sql"
import "fmt"
import "log"
import _ "github.com/ClickHouse/clickhouse-go/v2"
func main() {
        host := "HOST"
        https_port := HTTPS_PORT
        username := "USERNAME"
        password := "PASSWORD"
        conn, err := sql.Open(
                "clickhouse",
                fmt.Sprintf(
                        "https://%s:%d?username=%s&password=%s&secure", host, https_port, username, password))
        if err != nil {
                log.Fatal(err)
        }
        rows, err := conn.Query("SELECT version()")
        if err != nil {
                log.Fatal(err)
        }
        defer rows.Close()
        for rows.Next() {
                var version string
                if err := rows.Scan(&version); err != nil {
                        log.Fatal(err)
                }
                fmt.Println(version)
        }
}

You have your service connection established and configured. You can proceed to uploading data into your database.

Last updated on