Connect to a ClickHouse® cluster
You can connect to the hosts of DoubleCloud Managed Service for ClickHouse® via IDEs, CLI tools, or sample connection scripts for different languages. You can also connect to your cluster directly in your browser using WebSQL.
You can only connect to your cluster hosts using an SSL connection.
Warning
By default, Managed ClickHouse® clusters on DoubleCloud allow connections only from IP addresses and ranges added to allowlists. Before you connect to your cluster, check the allowlist configuration:
-
WebSQL: Make sure access for DoubleCloud services is enabled.
-
Other application, CLI, IDE, or ClickHouse Client: Make sure that the IP address or CIDR is added to the allowlist.
Connect with WebSQL
WebSQL is a DoubleCloud service that allows you to connect to your Managed ClickHouse® clusters from your browser tab. It provides a full-fledged SQL editor that you can use to run SQL queries. Learn more about WebSQL
To connect to a Managed ClickHouse® cluster with WebSQL:
-
On the Clusters
-
Click WebSQL at the top right:
-
After you've connected to a cluster, select a database in the connection manager on the left:
-
Enter a query in the query editor and click Execute:
Connect with IDEs
We tested the connections in the following environments:
-
Windows 10 Enterprise 1909:
-
JetBrains DataGrip:
2021.2
-
DBeaver Community Edition:
21.2.0
-
DataGrip
To connect to a Managed ClickHouse® cluster with DataGrip, take the following steps:
-
Open File → New → Data Source → ClickHouse®.
-
In the General tab:
-
In Connection type, select URL only.
-
Install the driver.
-
In the Authentication drop-down menu, select User & Password.
-
Copy the User and Password from the Overview tab on your cluster information page.
-
Copy the JDBC connection string from the Overview tab on your cluster information page into the URL field.
-
-
In the SSH/SSL tab:
-
Check the Use SSL box.
-
In the Mode drop-down menu, select
Verify CA
.
-
-
Click Test connection. If the connection is successful, you will see a green icon.
-
Click OK.
DBeaver
To connect to a Managed ClickHouse® cluster with DBeaver, take the following steps:
-
Open Database → New Database Connection.
-
Select ClickHouse® from the list of database types.
-
Click Next.
-
On the Main tab:
-
Host - paste the host's domain name (FQDN) from the Hosts tab on your cluster information page.
-
Port -
8443
by default. -
Database/Schema - enter the name of the database to connect to.
-
-
Copy Username and Password for your cluster from the Overview tab on your cluster information page.
-
On the Driver Properties tab:
-
Find
ssl
on the list of properties and change its value totrue
. -
Specify the
socket_timeout
. The recommended value is300000
or higher. Enter a larger value if you are going to perform longer queries.
-
-
Click Test Connection.... If the connection is successful, you will see a window with the connection status, information on the DBMS, and the driver.
-
Click Finish.
Connect with CLI tools
We tested the connections in the following environments:
-
Ubuntu 20.04 LTS:
-
Bash:
5.0.17
- clickhouse-client (DEB):
18.16.1.
- clickhouse-client (RPM):
18.16.1.
- clickhouse-client (DEB):
-
Docker:
20.10.12
, build:e91ed57
-
-
Windows 10 Enterprise 1909:
-
PowerShell:
5.1.18362
-
curl.exe:
7.55.1 WinSSL
-
DoubleCloud Managed Service for ClickHouse® ports
You can connect to DoubleCloud Managed Service for ClickHouse® clusters via the following ports:
9440
- the Native interface port, use it to connect with the clickhouse-client8443
,443
- TLS ports to send requests to the HTTP interface.9363
- the metrics port to connect Prometheus or other third-party solutions.
All the above ports are SSL-encrypted.
Bash
-
Install and configure the software:
-
Connect to the ClickHouse® official DEB repository
sudo apt update && sudo apt install -y apt-transport-https ca-certificates dirmngr && \ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754 && \ echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \ /etc/apt/sources.list.d/clickhouse.list
-
Refresh the package list and install the clickhouse-client
sudo apt update && sudo apt install -y clickhouse-client
-
-
Connect to your ClickHouse® host. You can copy the value of the Native Interface field from the Overview tab on your cluster information page. It has the following structure:
clickhouse-client --host <FQDN of the host> \ --secure \ --user <Cluster user name> \ --password <Cluster user password> \ --port 9440
-
Install and configure the software:
-
Connect to a ClickHouse® official RPM repository
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://packages.clickhouse.com/rpm/clickhouse.repo
-
Install the clickhouse-client
sudo yum install -y clickhouse-client
Warning
If you run a RedHat 7-based Linux distribution, including Cent OS 7, Oracle Linux 7 and others, you need to download and install trusted certificates, and manually add the path to them in the clickhouse-client configuration file as follows:
-
Install the
root
certificate:curl https://letsencrypt.org/certs/isrg-root-x2-cross-signed.pem > \ /etc/pki/ca-trust/source/anchors/isrg-root-x2-cross-signed.pem
-
Install the
intermediate
certificate:curl https://letsencrypt.org/certs/lets-encrypt-r3-cross-signed.pem > \ /etc/pki/ca-trust/source/anchors/lets-encrypt-r3-cross-signed.pem
-
Update the list of trusted certificates:
sudo update-ca-trust
-
Locate your clickhouse-client configuration file (by default, you can find it at
/etc/clickhouse-client/config.xml
) and add the path to the certificates into the<openSSL>
section:<client> <!-- Used for connection to server's secure tcp port --> <loadDefaultCAFile>true</loadDefaultCAFile> <cacheSessions>true</cacheSessions> <disableProtocols>sslv2,sslv3</disableProtocols> <preferServerCiphers>true</preferServerCiphers> <caConfig>/etc/ssl/certs/ca-bundle.crt</caConfig> <!-- Use for self-signed: <verificationMode>none</verificationMode> --> <invalidCertificateHandler> <!-- Use for self-signed: <name>AcceptCertificateHandler</name> --> <name>RejectCertificateHandler</name> </invalidCertificateHandler> </client>
-
-
-
Connect to your ClickHouse® host. You can copy the value of the Native Interface field from the Overview tab on your cluster information page. It has the following structure:
clickhouse-client --host <FQDN of the host> \ --secure \ --user <Cluster user name> \ --password <Cluster user password> \ --port 9440
cURL
Type the following in your terminal:
curl -H "X-ClickHouse-User: <Cluster user name>" \
-H "X-ClickHouse-Key: <Cluster user password>" \
'https://<FQDN of any ClickHouse® host in your cluster>:8443/?database=<Database name>&query=SELECT%20version()'
You will see the current ClickHouse® version in the output if your query is successful.
Docker
-
(Optional) Start Docker
service docker start
-
Pull the clickhouse-client
docker pull clickhouse/clickhouse-client
-
In a container, run a command that contains a connection string. Use the value of the Native Interface field from the Overview tab on your cluster information page as part of the Docker connection string. The command has the following structure:
docker run --network host --rm -i -t clickhouse/clickhouse-client \ --host <FQDN of the host> \ --secure \ --user <Cluster user name> \ --password <Cluster user password> \ --port 9440
Powershell
Type the following in your Powershell:
curl.exe \
-H "X-ClickHouse-User: <Cluster user name>" \
-H "X-ClickHouse-Key: <Cluster user password>" \
'https://<FQDN of any ClickHouse® host on your cluster>:8443/?database=<Database name>query=SELECT+version()'
You will see the current ClickHouse® version in the output if your query is successful.
Sample connection scripts
We tested the connections in the following environments:
-
Ubuntu 20.04 LTS:
C++
-
Write the script to connect to your database:
main.cpp
#include <iostream> #include <clickhouse/client.h> int main() { clickhouse::Client client(clickhouse::ClientOptions() .SetHost("<FQDN of any ClickHouse® host on your cluster>") .SetPort(9440) .SetUser("<Cluster user name>") .SetPassword("<Cluster user password>") .SetSSLOptions(clickhouse::ClientOptions::SSLOptions()) ); client.Select("SELECT version();", [](const clickhouse::Block &block) { for (size_t i = 0; i < block.GetRowCount(); ++i) { std::cout << block[0]->As<clickhouse::ColumnString>()->At(i) << std::endl; } }); }
-
In the same folder, create a
CMakeLists.txt
file to compile your script:cmake_minimum_required(VERSION 3.16) project(junk) set(CMAKE_CXX_STANDARD 20) SET(WITH_OPENSSL ON) add_subdirectory(clickhouse-cpp) INCLUDE_DIRECTORIES (clickhouse-cpp) INCLUDE_DIRECTORIES (clickhouse-cpp/contrib) add_executable(junk main.cpp) target_link_libraries (junk pthread clickhouse-cpp-lib)
-
Build your C++ environment, then compile your script:
git clone https://github.com/ClickHouse/clickhouse-cpp \ mkdir build && cd biuld \ cmake ../ \ make
-
Connect to your cluster:
./junk
If your script works, you'll see the current ClickHouse® version in the output.
C#
-
Create a directory for your .NET project:
ClickHouseTest
. -
Go to the project directory, install the template and dependencies for your environment:
cd ClickHouseTest \ dotnet new console -o ClickHouseTest -f net6.0 \ dotnet add ClickHouseTest package ClickHouse.Ado --version 1.4.3
-
Write the script to connect to your database:
Program.cs
using ClickHouse.Ado; string cstr = "Encrypt=True;"+ "Host=<FQDN of any ClickHouse® host on your cluster>;"+ "Port=9440;"+ "Database=default;"+ "User=<Cluster user name>;"+ "Password=<Cluster user password>"; var settings = new ClickHouseConnectionSettings(cstr); var conn = new ClickHouseConnection(settings); conn.Open(); var cmd = conn.CreateCommand("SELECT version()"); var reader = cmd.ExecuteReader(); do { while (reader.Read()) { Console.Write(reader.GetValue(0)); Console.WriteLine(); } } while (reader.NextResult()); conn.Close();
-
Run your program:
dotnet run
If your script works, you'll see the current ClickHouse® version in the output.
Go
-
Install the software:
-
Install the Go language interpreter:
sudo apt update && sudo apt install -y golang-go
-
Install Git:
sudo apt update && sudo apt install -y git
-
Install
clickhouse-go
with the necessary packages:go get -v -u github.com/ClickHouse/clickhouse-go/v2 \ github.com/google/uuid \ github.com/pierrec/lz4/v4 \ github.com/ClickHouse/clickhouse-go/lib/proto/query.go:25:2
-
-
Write the script to connect to your database:
dc_connect.go
package main import ( "database/sql" "fmt" "log" _ "github.com/ClickHouse/clickhouse-go/v2" ) func main() { host := "<FQDN of any ClickHouse® host on your cluster>" user := "<Cluster user name>" password := "<Cluster user password>" dsn := fmt.Sprintf("clickhouse://%s:%s@%s:9440?username=%s&password=%s&secure=true", user, password, host) connect, err := sql.Open("clickhouse", dsn) if err != nil { log.Fatal(err) } if err := connect.Ping(); err != nil { panic(err) } rows, err := connect.Query("SELECT version()") if err != nil { panic(err) } defer rows.Close() for rows.Next() { var version string if err := rows.Scan(&version); err != nil { panic(err) } log.Printf("version: %s", version) } if err := rows.Err(); err != nil { panic(err) } }
-
Connect to your cluster:
go run dc_connect.go
If your query is successful, you'll see the current ClickHouse® version in the output.
Java
-
Install
maven
and its dependencies:sudo apt update && sudo apt install -y maven
-
Write the script to connect to your cluster:
dc-connect/src/main/java/dc_connect.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; class clickhouse_example { public static void main(String[] args) throws SQLException{ String url = "<JDBC connection string>"; try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select version()")) { while (rs.next()) { System.out.println(rs.getString(1)); } } } }
-
Add the following dependencies to your
pom.xml
configuration file:<dependencies> <dependency> <groupId>com.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.3.2-patch7</version> <classifier>http</classifier> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
-
Compile your code:
mvn compile && mvn exec:java -Dexec.mainClass="<class name>"
If your script works, you'll see the current ClickHouse® version in the output.
Node.js
-
Install the software:
sudo apt update && sudo apt install -y nodejs npm && \ npm install querystring
-
Write a script to connect to your database:
dc_connect.js
"use strict" const http = require('https'); const querystring = require('querystring'); const fs = require('fs'); const DB_HOST = "<FQDN of any ClickHouse® host on your cluster>"; const DB_NAME = "<Database name>"; const DB_USER = "<Cluster user name>"; const DB_KEY = "<Ckuster user password>"; const options = { 'method': 'GET', 'path': '/?' + querystring.stringify({ 'database': DB_NAME, 'query': 'SELECT version()', }), 'port': 8443, 'hostname': DB_HOST, 'headers': { 'X-ClickHouse-User': DB_USER, 'X-ClickHouse-Key': DB_KEY, }, }; const rs = http.request(options, (res) => { res.setEncoding('utf8'); res.on('data', (chunk) => { console.log(chunk); }); }); rs.end();
-
Connect to your cluster:
node dc_connect.js
You'll see the current ClickHouse® version in the output if your query is successful.
PHP
-
Install the software and dependencies:
sudo apt update && sudo apt install -y php
-
Write a script to connect to your database:
dc_connect.php
<?php $host = '<FQDN of any ClickHouse® host on your cluster>'; $db = '<Database name>'; $query = 'SELECT version()'; $auth = [ 'X-ClickHouse-User: <Cluster user name>', 'X-ClickHouse-Key: <Cluster user password>', ]; $ssl = [ 'verify_peer' => true, ]; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'protocol_version' => 1.1, 'header' => $auth ], 'ssl' => $ssl ]); $url = sprintf('https://%s:8443/?database=%s&query=%s', $host, $db, urlencode($query)); $rs = file_get_contents($url, false, $context); print_r($rs); ?>
-
Connect to your cluster:
php dc_connect.php
If your query is successful, you'll see the current ClickHouse® version in the output.
Python
-
Install the software and dependencies:
sudo apt update && sudo apt install -y python3 python3-pip && pip3 install requests
-
Write a script to connect to your database:
dc_connect.py
import requests url = 'https://{host}:8443/?database={db}&query={query}'.format( host='<FQDN of any ClickHouse® host on your cluster>', db='<Database name>', query='SELECT version()') auth = { 'X-ClickHouse-User': '<Cluster user name>', 'X-ClickHouse-Key': '<Cluster user password>', } rs = requests.get(url, headers=auth, verify=cacert) rs.raise_for_status() print(rs.text)
-
Connect to your cluster:
python3 dc_connect.py
If your query is successful, you'll see the current ClickHouse® version in the output.
Ruby
-
Install the software:
sudo apt update && sudo apt install -y ruby
-
Write a script to connect to your database:
dc_connect.rb
require "net/http" require "openssl" require "uri" DB_HOST = "<FQDN of any ClickHouse® host on your cluster>" DB_NAME = "<Database name>" DB_USER = "<Cluster user name>" DB_PASS = "<Cluster user password>" QUERYSTRING = { :database => DB_NAME, :query => "SELECT version()" } uri = URI("https://" + DB_HOST + "/") uri.port = 8443 uri.query = URI.encode_www_form(QUERYSTRING) req = Net::HTTP::Get.new(uri) req.add_field("X-ClickHouse-User", DB_USER) req.add_field("X-ClickHouse-Key", DB_PASS) conn = Net::HTTP.new(uri.host, uri.port) conn.use_ssl = true conn.verify_mode = OpenSSL::SSL::VERIFY_PEER rs = conn.request(req) puts rs.body
-
Connect to your cluster:
ruby dc_connect.rb
If your query is successful, you'll see the current ClickHouse® version in the output.
Select an available host automatically
If the current host is unavailable, you can use the following URL to connect to any available host on the cluster:
-
rw.<cluster_id>.at.double.cloud
to connect to one of the cluster's available hosts.<cluster_id>
is the CNAME -
<shard_name>.<cluster_id>.at.double.cloud
to connect to an Alive host in a specific shard.
You can find <cluster_id>
in the ID field in the Overview tab on your cluster information page
or copy the HTTPS connection string from the Hosts tab.
You can find <shard_name>
in the Hosts tab on your cluster information page.
If the host from the above address line becomes unavailable, you might encounter a short lag before the address leads to another available host.
Connect with public and private connection strings
When you connect to a cluster via a peering connection from VPC, you need to use a private address instead of the normally used public address.
To obtain a cluster's private connection string, go to the cluster overview page. Under Connection strings, switch to the Private tab:
You can also connect to a certain host on your cluster. The structures of a cluster and a host connection string differ as follows:
-
Public address:
rw.<cluster_id>.at.double.cloud # or <host_name>.<cluster_id>.at.double.cloud
-
Private address:
rw.<cluster_id>.private.at.double.cloud # or <host_name>.<cluster_id>.private.at.double.cloud