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).
-
Go to the VK Cloud management console.
-
Go to Data Platform → Service instances. Make sure that the required Cloud ClickHouse instance has the
Activestatus. -
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.
-
Prepare the database name, login, and password for the Cloud ClickHouse user account. This can be:
- a user specified when creating the instance;
- an additional user added to the instance manually.
The following steps depend on the connection method:
The Cloud ClickHouse version is displayed after each connection example is executed.
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.
-
Download the package of the required version:
-
Install the client by following the instructions in the official ClickHouse documentation.
-
Open a terminal and connect with TLS:
$ clickhouse-client \--host=<HOST> \--port=<PORT> \--user='<LOGIN>' \--password='<PASSWORD>' \--secure \--accept-invalid-certificateTo connect without TLS, remove the
--secureand--accept-invalid-certificateflags. -
Check the connection by running a test query:
SELECT version();
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.
-
Open a terminal and install the driver:
$ pip install clickhouse-connect==1.6.0 -
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=Trueandverify=Falseparameters.
To connect using Java over the HTTP protocol, use the clickhouse-jdbc JDBC driver.
Recommended driver version — 0.9.8. Minimum — 0.7.0.
-
Add the dependency to your project:
MavenGradle<dependency><groupId>com.clickhouse</groupId><artifactId>clickhouse-jdbc</artifactId><version>0.9.8</version></dependency> -
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=NONEparameters from the URL.
To connect using C++ over TCP, use the clickhouse-cpp client library.
Recommended library version — 2.6.0.
-
Open a terminal and install the
clickhouse-cpplibrary, 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 -
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 theClientOptions()call chain.
To connect using Go over TCP or HTTP, use the clickhouse-go driver.
Recommended driver version — 2.48.0. Minimum — 2.0.0.
-
Open a terminal and install the driver:
$ go get github.com/ClickHouse/clickhouse-go/v2@v2.48.0 -
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 stringif 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=trueparameters from the connection string.