Skip to main content

Connect to the Aiven for ClickHouse® service with Go

To connect to your Aiven for 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.17 or later

Install the ClickHouse Go module

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

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

If the version of Go is lower than 1.18.4 (visible via go version), install an older version of clickhouse-go. For this purpose, use command go get github.com/ClickHouse/clickhouse-go/v2@v2.2.

Connect with the native protocol

Identify connection information

To run the code for connecting to your service, first identify values of the following variables:

VariableDescription
HostHost for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native
PortPort for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native
DatabaseDatabase Name in your the ClickHouse service available in the Aiven console: Service Overview > Connection information > ClickHouse native
UsernameUser for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native
PasswordPassword for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse native

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

The HTTPS connection is supported for the database/SQL API only. By default, connections are established over the native protocol. The HTTPS connection needs to be enabled either by modifying the DSN to include the HTTPS protocol or by specifying the protocol in the connection options.

Identify connection information

To run the code for connecting to your service, first identify values of the following variables:

VariableDescription
Host

Host for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

HttpPort

Port for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

DatabaseDatabase Name in your the ClickHouse service available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
UsernameUser for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC
PasswordPassword for the ClickHouse connection available in the Aiven console: Service Overview > Connection information > ClickHouse HTTPS & JDBC

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.