[Yandex Cloud documentation](../../../index.md) > [Yandex DataSphere](../../index.md) > [Step-by-step guides](../index.md) > Connecting to data sources > Connecting to a ClickHouse® database

# Connecting to a ClickHouse® database

Managed Service for ClickHouse® enables deploying and maintaining [ClickHouse®](https://clickhouse.com/) server clusters in the Yandex Cloud infrastructure.

## Getting started {#before-begin}

1. [Create](../../../managed-clickhouse/operations/cluster-create.md) a new Managed Service for ClickHouse® cluster with public host access. You can also use an existing cluster with publicly accessible hosts.
1. [Configure](../../../managed-clickhouse/operations/connect/index.md#configuring-security-groups) cluster security groups.
1. Open the DataSphere project:
   
   1. Select the project in your community or on the DataSphere [home page](https://datasphere.yandex.cloud) in the **Recent projects** tab.
   1. Click **Open project in JupyterLab** and wait for the loading to complete.
   1. Open the notebook tab.

## Connecting to a host {#connect-to-host}

To connect to Managed Service for ClickHouse® cluster hosts:

1. Get an SSL certificate. To do this, enter this command in a notebook cell:

    ```bash
    #!:bash
    mkdir ~/.clickhouse-client
    wget "https://storage.yandexcloud.net/cloud-certs/CA.pem" -O ~/.clickhouse-client/root.crt && \
    chmod 0600 ~/.clickhouse-client/root.crt
    ```

1. Establish a database connection. To do this, enter this command in a notebook cell:

    {% list tabs %}

    - Using the `requests` library:

      ```python
      import requests
      url = 'https://{host}:8443/?database={db}&query={query}'.format(
              host='<ClickHouse®_host_FQDN>',
              db='<DB_name>',
              query='SELECT version()')
      auth = {
              'X-ClickHouse-User': '<DB_username>',
              'X-ClickHouse-Key': '<DB_user_password>',
          }
      cacert = '/home/jupyter/.clickhouse-client/root.crt'
      rs = requests.get(url, headers=auth, verify=cacert)
      rs.raise_for_status()
      print(rs.text)
      ```

      If you succeed connecting to the cluster, you will get the ClickHouse® version in response to your test request:

      ```text
      22.3.17.13
      ```

    - Using the `clickhouse-driver` library:

      ```python
      %pip install clickhouse-driver
      from clickhouse_driver import Client
      client = Client(host='<ClickHouse®_host_FQDN>',
                      user='<DB_username>',
                      password='<DB_user_password>',
                      database='<DB_name>',
                      secure=True)
      client.execute('SELECT version()')
      ```

      If you succeed connecting to the cluster, you will get the ClickHouse® version in response to your test request:

      ```text
      [('22.3.17.13',)]
      ```

    {% endlist %}

_ClickHouse® is a registered trademark of [ClickHouse, Inc](https://clickhouse.com)._