VK Cloud logo

Connecting to a service instance

Cloud ClickHouse accepts connections over two protocols: TCP and HTTP. The connection to the service instance is protected with a self-signed TLS certificate.

The instructions provide examples of connecting using clickhouse-client (for connecting from a terminal) and drivers for Python, Java, C++, and Go (for connecting from application code).

Preparing to connect

  1. Go to the VK Cloud management console.

  2. Go to Data PlatformService instances. Make sure that the required Cloud ClickHouse instance has the Active status.

  3. On the General information tab of the service instance, copy the host and port from the required connection string, depending on the protocol (TCP or HTTP) and whether TLS encryption is enabled.

  4. Prepare the database name, login, and password for the Cloud ClickHouse user account. This can be:

The following steps depend on the connection method:

The Cloud ClickHouse version is displayed after each connection example is executed.

clickhouse-client

clickhouse-client allows you to connect to a Cloud ClickHouse instance over TCP. We recommend using a client of the same version as the instance. The minimum requirement is that the first two version numbers match: for an instance of version 24.3.18.7, use client version 24.3.0.0 or higher; for version 25.3.14.14, use client version 25.3.0.0 or higher.

  1. Download the package of the required version:

  2. Install the client by following the instructions in the official ClickHouse documentation.

  3. Open a terminal and connect with TLS:

    $ clickhouse-client \   --host=<HOST> \   --port=<PORT> \   --user='<LOGIN>' \   --password='<PASSWORD>' \   --secure \   --accept-invalid-certificate

    To connect without TLS, remove the --secure and --accept-invalid-certificate flags.

  4. Check the connection by running a test query:

    SELECT version();

Python

To connect using Python, use the driver:

  • clickhouse-connect — HTTP connection. Recommended version — 1.6.0. Minimum — 1.0.0.
  • clickhouse-driver — TCP connection. Recommended version — 0.2.11. Minimum — 0.2.0.
  1. Open a terminal and install the driver:

    $ pip install clickhouse-connect==1.6.0
  2. To connect to Cloud ClickHouse with TLS, run the command:

    import clickhouse_connectclient = clickhouse_connect.get_client(    host='<HOST>',    port='<PORT>',    username='<LOGIN>',    password='<PASSWORD>',    database='<DB>',    secure=True,    verify=False)print(client.command('SELECT version()'))

    To connect without TLS, remove the secure=True and verify=False parameters.

Java

To connect using Java over the HTTP protocol, use the clickhouse-jdbc JDBC driver.

Recommended driver version — 0.9.8. Minimum — 0.7.0.

  1. Add the dependency to your project:

    <dependency>    <groupId>com.clickhouse</groupId>    <artifactId>clickhouse-jdbc</artifactId>    <version>0.9.8</version></dependency>
  2. To connect to Cloud ClickHouse with TLS, run the command:

    import com.clickhouse.jdbc.ClickHouseDataSource;String url = "jdbc:clickhouse://<HOST>:<PORT>/<DB>?ssl=true&sslmode=NONE";Properties props = new Properties();props.setProperty("user", "<LOGIN>");props.setProperty("password", "<PASSWORD>");ClickHouseDataSource ds = new ClickHouseDataSource(url, props);try (Connection conn = ds.getConnection();     Statement stmt = conn.createStatement();     ResultSet rs = stmt.executeQuery("SELECT version()")) {    if (rs.next()) {        System.out.println(rs.getString(1));    }}

    To connect without TLS, remove the ?ssl=true&sslmode=NONE parameters from the URL.

C++

To connect using C++ over TCP, use the clickhouse-cpp client library.

Recommended library version — 2.6.0.

  1. Open a terminal and install the clickhouse-cpp library, for example from source:

    $ git clone --branch v2.6.0 https://github.com/ClickHouse/clickhouse-cpp.git$ mkdir clickhouse-cpp/build && cd clickhouse-cpp/build$ cmake ..$ make$ sudo make install
  2. To connect to Cloud ClickHouse with TLS, run the command:

    #include <clickhouse/client.h>clickhouse::Client client(    clickhouse::ClientOptions()        .SetHost("<HOST>")        .SetPort("<PORT>")        .SetUser("<LOGIN>")        .SetPassword("<PASSWORD>")        .SetDefaultDatabase("<DB>")        .SetSSLOptions(clickhouse::ClientOptions::SSLOptions()            .SetSkipVerification(true)));clickhouse::Block block;client.Select("SELECT version()", &block);if (block.GetRowCount() > 0) {    std::cout << block[0]->As<clickhouse::ColumnString>()->At(0) << std::endl;}

    To connect without TLS, remove the .SetSSLOptions(...) method from the ClientOptions() call chain.

Go

To connect using Go over TCP or HTTP, use the clickhouse-go driver.

Recommended driver version — 2.48.0. Minimum — 2.0.0.

  1. Open a terminal and install the driver:

    $ go get github.com/ClickHouse/clickhouse-go/v2@v2.48.0
  2. To connect to Cloud ClickHouse with TLS, run the command:

    package mainimport (    "database/sql"    "fmt"    "log"    _ "github.com/ClickHouse/clickhouse-go/v2")func main() {    conn, err := sql.Open("clickhouse", "clickhouse://<LOGIN>:<PASSWORD>@<HOST>:<PORT>/<DB>?secure=true&skip_verify=true")    if err != nil {        log.Fatal(err)    }    defer conn.Close()    var version string    if err := conn.QueryRow("SELECT version()").Scan(&version); err != nil {        log.Fatal(err)    }    fmt.Println(version)}

    To connect without TLS, remove the secure=true&skip_verify=true parameters from the connection string.

Was this article helpful?