[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for ClickHouse®](../index.md) > [Step-by-step guides](index.md) > Databases > Managing databases

# Managing databases in Managed Service for ClickHouse®

Managed Service for ClickHouse® provides two methods for managing cluster databases:

* Using the native Yandex Cloud interfaces, such as the CLI, API, and management console. Select this method to create and delete cluster databases using the Yandex Managed Service for ClickHouse® features.
* Using SQL queries against the cluster. Select this method to use your own solution to create and manage databases, or if you need MySQL® database support in Managed Service for ClickHouse®.

## Database management via SQL {#sql-database-management}

To enable this management method, select **User management via SQL** and **Managing databases via SQL** when [creating](cluster-create.md) or [reconfiguring](update.md#SQL-management) your cluster.

In a cluster with enabled database management via SQL:

* You cannot manage databases and [users](cluster-users.md#sql-user-management) via the native Yandex Cloud interfaces, such as the CLI, API, and management console.
* You cannot enable user or database management via the native Yandex Cloud interfaces.
* The existing users, user settings, and databases created with the native Yandex Cloud interfaces will remain unchanged.
* Use the `admin` account for management. You set the `admin` password when selecting the **User management via SQL** and **Managing databases via SQL** options.

## Getting a list of cluster databases {#list-db}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
  1. Navigate to **Managed Service for&nbsp;ClickHouse**.
  1. Click the name of your cluster and select the **Databases** tab.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To get a list of cluster databases, run this command:

  ```bash
  yc managed-clickhouse database list \
     --cluster-name=<cluster_name>
  ```

  You can get the cluster name with the [list of clusters in the folder](cluster-list.md#list-clusters).

- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Call the [Database.List](../api-ref/Database/list.md) method, e.g., via the following [cURL](https://curl.se/) request:

      ```bash
      curl \
        --request GET \
        --header "Authorization: Bearer $IAM_TOKEN" \
        --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/databases'
      ```

      You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Database/list.md#yandex.cloud.mdb.clickhouse.v1.ListDatabasesResponse) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.List](../api-ref/grpc/Database/list.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

      ```bash
      grpcurl \
        -format json \
        -import-path ~/cloudapi/ \
        -import-path ~/cloudapi/third_party/googleapis/ \
        -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/database_service.proto \
        -rpc-header "Authorization: Bearer $IAM_TOKEN" \
        -d '{
              "cluster_id": "<cluster_ID>"
            }' \
        mdb.api.cloud.yandex.net:443 \
        yandex.cloud.mdb.clickhouse.v1.DatabaseService.List
      ```

      You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. View the [server response](../api-ref/grpc/Database/list.md#yandex.cloud.mdb.clickhouse.v1.ListDatabasesResponse) to make sure your request was successful.

- SQL {#sql}

  1. [Connect](connect/clients.md) to your cluster as [`admin`](#sql-database-management).
  1. Get a list of databases:

      ```sql
      SHOW DATABASES;
      ```

{% endlist %}

## Creating a database {#add-db}

{% note info %}

You can create a maximum of 1000 databases in each cluster.

{% endnote %}

For more about limits, see [Quotas and limits](../concepts/limits.md).

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
  1. Navigate to **Managed Service for&nbsp;ClickHouse**.
  1. Click the cluster name.
  1. Select the **Databases** tab.
  1. Click **Create database**.
  1. Enter a name for the database.

      {% note info %}
      
      The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
      
      {% endnote %}

  1. Select the database engine: 
      
      * `Atomic`, the default option, supports the non-blocking `DROP TABLE` and `RENAME TABLE` operations and atomic `EXCHANGE TABLES` operations.
      * `Replicated` supports table metadata replication across all database replicas. The set of tables and their schemas will be the same for all replicas.

        It is only available in [replicated](../concepts/replication.md) clusters.

      You can only set the engine when creating a database and cannot change it later.

  1. Click **Create**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  Run the `database create` command, providing the new database name:

  ```bash
  yc managed-clickhouse database create <DB_name> \
     --cluster-name=<cluster_name> \
     --engine=<DB_engine>
  ```

  Where:
  * `--cluster-name`: Database cluster name.
  * `--engine`: Database engine. This is an optional setting. The possible values are:
    * `database-engine-atomic` (default): `Atomic` engine; supports non-blocking `DROP TABLE` and `RENAME TABLE` queries, and atomic `EXCHANGE TABLES` queries.
    * `database-engine-replicated`: `Replicated` engine; supports table metadata replication across all database replicas. The set of tables and their schemas will be the same for all replicas.

      It is only available in [replicated](../concepts/replication.md) clusters.
                          
    You can only set the engine when creating a database and cannot change it later.

  {% note info %}
  
  The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
  
  {% endnote %}

  You can get the cluster name from the [list of clusters in your folder](cluster-list.md#list-clusters).

  Managed Service for ClickHouse® will start creating the database.


- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create such a file, see the [Creating a cluster](cluster-create.md) section.

    1. Add the `yandex_mdb_clickhouse_database` resource:

        ```hcl
        resource "yandex_mdb_clickhouse_database" "<DB_name>" {
          cluster_id = "<cluster_ID>"
          name       = "<DB_name>"
          engine     = "<DB_engine>"
        }
        ```

        Where:
        * `cluster_id`: Database cluster ID.
        * `name`: Database name.
        * `engine`: Database engine. This is an optional setting. The possible values are:
          * `atomic` (default): `Atomic` engine; supports non-blocking `DROP TABLE` and `RENAME TABLE` queries, and atomic `EXCHANGE TABLES` queries.
          * `replicated`: `Replicated` engine; supports table metadata replication across all database replicas. The set of tables and their schemas will be the same for all replicas.

            It is only available in [replicated](../concepts/replication.md) clusters.
                          
          You can only set the engine when creating a database and cannot change it later.

        {% note info %}
        
        The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
        
        {% endnote %}

        When creating both a cluster and a database with Terraform at the same time, specify a name for the new cluster rather than cluster ID in the `yandex_mdb_clickhouse_database` resource:

        ```hcl

        resource "yandex_mdb_clickhouse_cluster_v2" "<cluster_name>" {
          name = "<cluster_name>"
          ...
        }

        resource "yandex_mdb_clickhouse_database" "<DB_name>" {
          cluster_id = yandex_mdb_clickhouse_cluster_v2.<cluster_name>.id
          name       = "<DB_name>"
        }
        ```

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    For more information, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_database.md).


- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Call the [Database.Create](../api-ref/Database/create.md) method, e.g., via the following [cURL](https://curl.se/) request:

      ```bash
      curl \
          --request POST \
          --header "Authorization: Bearer $IAM_TOKEN" \
          --header "Content-Type: application/json" \
          --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/databases' \
          --data '{
                    "databaseSpec": {
                      "name": "<DB_name>",
                      "engine": "<database_engine>"
                    }
                  }'
      ```

      Where: 

      * `databaseSpec.name`: Database name.

        {% note info %}
        
        The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
        
        {% endnote %}
        
      * `databaseSpec.engine`: Database engine. The possible values are:
        
        * `DATABASE_ENGINE_ATOMIC` (default): `Atomic` engine; supports non-blocking `DROP TABLE` and `RENAME TABLE` queries, and atomic `EXCHANGE TABLES` queries.
        * `DATABASE_ENGINE_REPLICATED`: `Replicated` engine; supports table metadata replication across all database replicas. The set of tables and their schemas will be the same for all replicas.
                            
          It is only available in [replicated](../concepts/replication.md) clusters.
        
        * `DATABASE_ENGINE_UNSPECIFIED`: This value will set the default engine, i.e., `DATABASE_ENGINE_ATOMIC`.
                            
        You can only set the engine when creating a database and cannot change it later.

      You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. Check the [server response](../api-ref/Database/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Create](../api-ref/grpc/Database/create.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

      ```bash
      grpcurl \
          -format json \
          -import-path ~/cloudapi/ \
          -import-path ~/cloudapi/third_party/googleapis/ \
          -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/database_service.proto \
          -rpc-header "Authorization: Bearer $IAM_TOKEN" \
          -d '{
                "cluster_id": "<cluster_ID>",
                "database_spec": {
                  "name": "<DB_name>",
                  "engine": "<database_engine>"
                }
              }' \
          mdb.api.cloud.yandex.net:443 \
          yandex.cloud.mdb.clickhouse.v1.DatabaseService.Create
      ```

      Where: 

      * `database_spec.name`: Database name.

        {% note info %}
        
        The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
        
        {% endnote %}
        
      * `database_spec.engine`: Database engine. The possible values are:

        * `DATABASE_ENGINE_ATOMIC` (default): `Atomic` engine; supports non-blocking `DROP TABLE` and `RENAME TABLE` queries, and atomic `EXCHANGE TABLES` queries.
        * `DATABASE_ENGINE_REPLICATED`: `Replicated` engine; supports table metadata replication across all database replicas. The set of tables and their schemas will be the same for all replicas.
                            
          It is only available in [replicated](../concepts/replication.md) clusters.
        
        * `DATABASE_ENGINE_UNSPECIFIED`: This value will set the default engine, i.e., `DATABASE_ENGINE_ATOMIC`.
                            
        You can only set the engine when creating a database and cannot change it later.

      You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters).

  1. View the [server response](../api-ref/grpc/Database/create.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- SQL {#sql}

  1. [Connect](connect/clients.md) to your cluster as [`admin`](#sql-database-management).
  1. Create a database:

      ```sql
      CREATE DATABASE <DB_name>;
      ```

      {% note info %}
      
      The database name may contain Latin letters, numbers, and underscores. It may be up to 63 characters long. You cannot create a database named `default`.
      
      {% endnote %}

  To learn more about creating databases, see [this ClickHouse® guide](https://clickhouse.com/docs/enen/sql-reference/statements/create/database).

{% endlist %}

## Deleting a database {#remove-db}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the cluster.
  1. Navigate to **Managed Service for&nbsp;ClickHouse**.
  1. Click the name of your cluster and select the **Databases** tab.
  1. Click ![image](../../_assets/console-icons/ellipsis.svg) in the relevant database row and select **Delete**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  To delete a database, run this command:

  ```bash
  yc managed-clickhouse database delete <DB_name> \
     --cluster-name=<cluster_name>
  ```

  You can get the cluster name with the [list of clusters in the folder](cluster-list.md#list-clusters).


- Terraform {#tf}

    1. Open the current Terraform configuration file with the infrastructure plan.

        For more on how to create such a file, see the [Creating a cluster](cluster-create.md) section.

    1. Remove the `yandex_mdb_clickhouse_database` resource with the name of the database you want to delete.

    1. Make sure the settings are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

    1. Confirm resource changes.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

    For more information, see [this Terraform provider guide](../../terraform/resources/mdb_clickhouse_database.md).


- REST API {#api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Call the [Database.Delete](../api-ref/Database/delete.md) method, e.g., via the following [cURL](https://curl.se/) request:

      ```bash
      curl \
          --request DELETE \
          --header "Authorization: Bearer $IAM_TOKEN" \
          --url 'https://mdb.api.cloud.yandex.net/managed-clickhouse/v1/clusters/<cluster_ID>/databases/<DB_name>'
      ```

      You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name from the [list of databases in your cluster](#list-db).

  1. Check the [server response](../api-ref/Database/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

  1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

      ```bash
      export IAM_TOKEN="<IAM_token>"
      ```

  1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
     
     ```bash
     cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
     ```
     
     Below, we assume that the repository contents reside in the `~/cloudapi/` directory.
  1. Call the [DatabaseService.Delete](../api-ref/grpc/Database/delete.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

      ```bash
      grpcurl \
          -format json \
          -import-path ~/cloudapi/ \
          -import-path ~/cloudapi/third_party/googleapis/ \
          -proto ~/cloudapi/yandex/cloud/mdb/clickhouse/v1/database_service.proto \
          -rpc-header "Authorization: Bearer $IAM_TOKEN" \
          -d '{
                "cluster_id": "<cluster_ID>",
                "database_name": "<DB_name>"
              }' \
          mdb.api.cloud.yandex.net:443 \
          yandex.cloud.mdb.clickhouse.v1.DatabaseService.Delete
      ```

      You can get the cluster ID from the [list of clusters in your folder](cluster-list.md#list-clusters), and the database name, from the [list of databases in your cluster](#list-db).

  1. View the [server response](../api-ref/grpc/Database/delete.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- SQL {#sql}

  1. [Connect](connect/clients.md) to your cluster as [`admin`](#sql-database-management).
  1. Delete the database:

      ```sql
      DROP DATABASE <DB_name>;
      ```

  Learn more about deleting objects in [this ClickHouse® guide](https://clickhouse.com/docs/enen/sql-reference/statements/drop).

{% endlist %}

{% note warning %}

If you plan to create a new database with the same name, wait for the deletion to complete. Otherwise, the system will restore the original database. You can check the operation status in the [list of cluster operations](cluster-list.md#list-operations).

{% endnote %}

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