[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Apache Airflow™](../index.md) > [Step-by-step guides](index.md) > Connections > Managed Service for ClickHouse®: Connecting to a database

# Connecting to Yandex Managed Service for ClickHouse®

With a [directed acyclic graph (DAG)](../concepts/index.md#about-the-service), you can configure a connection to a database in a Yandex Managed Service for ClickHouse® cluster. Data for connecting to the DB is stored in Yandex Lockbox and automatically inserted into the graph.

## Getting started {#before-begin}

1. [Create a Managed Service for ClickHouse® cluster](../../managed-clickhouse/operations/cluster-create.md) with the following parameters:
   * **DB name**: `default-bd`
   * **Username**: `admin`
   * **Password**: `admin-password`

   {% note warning %}

   You cannot create a database named `default`.

   {% endnote %}

1. [Create](../../storage/operations/buckets/create.md) a Yandex Object Storage bucket to store the DAG file in.

1. [Configure the Managed Service for Apache Airflow™ cluster](cluster-update.md):

   1. Enable the **Use Lockbox Secret Backend** option allowing you to use secrets in [Yandex Lockbox](../../lockbox/concepts/index.md) to [store Apache Airflow™ configuration data, variables, and connection parameters](../concepts/impersonation.md#lockbox-integration).
   1. Under **Dependencies**, add the `airflow-clickhouse-plugin` pip package.
   1. Under **DAG file storage**, select the Object Storage bucket you created earlier. Your DAG file will be fetched from it.

1. Issue the `lockbox.payloadViewer` [role](../../lockbox/security/index.md#lockbox-payloadViewer) to your service account.

   {% note info %}

   There is no need to assign the `lockbox.payloadViewer` role for the whole folder. It is enough to [assign it for a specific Yandex Lockbox secret](../../lockbox/operations/secret-access.md) once you [create it](#create-lockbox-secret).

   {% endnote %}

## Create a Yandex Lockbox secret {#create-lockbox-secret}

For the Apache Airflow™ cluster to work correctly, your Yandex Lockbox secret's name must have this format: `airflow/<artifact_type>/<artifact_ID>`, where:
   * `<artifact_type>`: Decides what data will be stored in the secret. The allowed values are:
     * `connections`: Connections.
     * `variables`: Variables.
     * `config`: Configuration data.
   * `<artifact_ID>`: ID to use to access the artifact in Apache Airflow™.

[Create a Yandex Lockbox](../../lockbox/operations/secret-create.md) secret with the following parameters:
   * **Name**: `airflow/connections/ch`.
   * **Secret type**: `Custom`.
   * **Key**: `conn`.
   * **Value**: Select **Text** and specify the following contents:

      ```json
      {
        "conn_type": "clickhouse",
        "host": "<ClickHouse®_cluster_host_FQDN>",
        "port": 9440,
        "schema": "default-bd",
        "login": "admin",
        "password": "admin-password",
        "extra": {
            "secure": "True"
        }
      }
      ```

For more information on how to learn the FQDN of a ClickHouse® cluster host, see [FQDNs of ClickHouse® hosts](../../managed-clickhouse/operations/connect/fqdn.md).

The secret will store the data to connect to the database in the Managed Service for ClickHouse® cluster.

## Prepare the DAG file and run the graph {#dag}

1. Create a local file named `clickhouse.py` and paste the following script to it:

   ```python
   from airflow.decorators import dag, task
   from airflow_clickhouse_plugin.hooks.clickhouse import ClickHouseHook


   @dag(schedule=None)
   def clickhouse():
       @task
       def query_clickhouse():
           ch_hook = ClickHouseHook(clickhouse_conn_id="ch")
           result = ch_hook.execute('select 1;')
           print(f'query result: {result}')

       query_clickhouse()


   clickhouse()
   
   ```

1. Upload the `clickhouse.py` DAG file to the bucket you created earlier.
1. [Open the Apache Airflow™ web interface](af-interfaces.md#web-gui).
1. Make sure there is a new graph named `clickhouse` in the **DAGs** section.

   It may take a few minutes to load a DAG file from the bucket.

1. To run the graph, click ![image](../../_assets/managed-airflow/trigger-dag.png =18x) in the line with its name.

## Check the result {#check-result}

To check the result in the Apache Airflow™ web interface:

{% list tabs group=instructions %}
   
- Apache Airflow™ version below 3.0 {#version-2}

  1. In the **DAGs** section, open the `clickhouse` graph.
  1. Go to the **Graph** section.
  1. Select **query_clickhouse**.
  1. Go to **Logs**.
  1. Make sure the logs contain the `query result: [(1,)]` line. This means the query was successful.

- Apache Airflow™ version 3.0 or higher {#version-3}

  1. In the **DAGs** section, click the `clickhouse` graph.
  1. Go to **Tasks**.
  1. Select **query_clickhouse**.
  1. Go to **Tasks Instances**.
  1. Select the task instance.
  1. The **Logs** section will open.
  1. Make sure the logs contain the `query result: [(1,)]` line. This means the query was successful.

{% endlist %}

## Troubleshooting {#troubleshooting}

* [How do I fix the `SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED]` error?](../qa/index.md#airflow-clickhouse-ssl)
* [How do I fix the `No module named 'airflow_clickhouse_plugin'` error?](../qa/index.md#airflow-clickhouse-plugin)