[Yandex Cloud documentation](../../index.md) > [Yandex Data Transfer](../index.md) > [Tutorials](index.md) > Uploading data to data marts > Greenplum® to ClickHouse®

# Loading data from Greenplum® to a ClickHouse® data mart

You can migrate a database from Greenplum® to ClickHouse® using Yandex Data Transfer.

To transfer a database from Greenplum® to ClickHouse®:

1. [Set up your transfer](#prepare-transfer).
1. [Activate the transfer](#activate-transfer).
1. [Test copying after reactivation](#example-check-copy).

If you no longer need the resources you created, [delete them](#clear-out).


## Required paid resources {#paid-resources}

* Yandex MPP Analytics for PostgreSQL cluster: use of computing resources allocated to hosts, storage and backup size (see [Yandex MPP Analytics for PostgreSQL pricing](../../managed-greenplum/pricing/index.md)).
* Yandex Managed Service for ClickHouse® cluster: use of computing resources allocated to hosts, storage and backup size (see [Managed Service for ClickHouse® pricing](../../managed-clickhouse/pricing.md)).
* Each transfer: use of computing resources and the number of transferred data rows (see [Data Transfer pricing](../pricing.md)).
* Public IP addresses if public access is enabled for cluster hosts (see [Yandex Virtual Private Cloud pricing](../../vpc/pricing.md)).


## Getting started {#before-you-begin}

In our example, we will create all required resources in Yandex Cloud. Set up your infrastructure:

{% list tabs group=instructions %}

- Manually {#manual}

    1. [Create a Greenplum® source cluster in Yandex MPP Analytics for PostgreSQL](../../managed-greenplum/operations/cluster-create.md#create-cluster) with any suitable configuration.

    1. [Create a Yandex Managed Service for ClickHouse® target cluster](../../managed-clickhouse/operations/cluster-create.md#create-cluster) with a database named `db1` using any suitable configuration.

    
    1. If using security groups, make sure they are configured correctly and allow inbound connections to the clusters.

        * [Managed Service for ClickHouse®](../../managed-clickhouse/operations/connect/index.md#configuring-security-groups).
        * [Yandex MPP Analytics for PostgreSQL](../../managed-greenplum/operations/connect/index.md#configuring-security-groups).


    1. Create a `ClickHouse®` [target endpoint](../operations/endpoint/target/clickhouse.md). In the endpoint [settings](../operations/endpoint/target/clickhouse.md#additional-settings), specify `Drop` or `Truncate` as the cleanup policy to prevent duplication of copied data.


- Terraform {#tf}

    1. If you do not have Terraform yet, [install it](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
    1. [Get the authentication credentials](../../tutorials/infrastructure-management/terraform-quickstart.md#get-credentials). You can add them to environment variables or specify them later in the provider configuration file.
    1. [Configure and initialize a provider](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). There is no need to create a provider configuration file manually, you can [download it](https://github.com/yandex-cloud-examples/yc-terraform-provider-settings/blob/main/provider.tf).
    1. Place the configuration file in a separate working directory and [specify the parameter values](../../tutorials/infrastructure-management/terraform-quickstart.md#configure-provider). If you did not add the authentication credentials to environment variables, specify them in the configuration file.

    1. Download the [greenplum-clickhouse.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-greenplum-to-clickhouse/blob/main/greenplum-clickhouse.tf) configuration file to your current working directory.

        This file describes:

        * [Networks](../../vpc/concepts/network.md#network) and [subnets](../../vpc/concepts/network.md#subnet) where your clusters will be hosted.

        
        * [Security groups](../../vpc/concepts/security-groups.md) for cluster access.


        * Greenplum® source cluster in Yandex MPP Analytics for PostgreSQL.
        * Managed Service for ClickHouse® target cluster.
        * Target endpoint.

    1. In `greenplum-clickhouse.tf`, specify the following:

        * `mgp_password`: Greenplum® admin password.
        * `mch_db`: ClickHouse® database name.
        * `mch_user`: ClickHouse® database user name.
        * `mch_password`: ClickHouse® database user password.

    1. Validate your Terraform configuration files using this command:

        ```bash
        terraform validate
        ```

        Terraform will display any configuration errors detected in your files.

    1. Create the required infrastructure:

        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.

        All the required resources will be created in the specified folder. You can check resource availability and their settings in the [management console](https://console.yandex.cloud).

{% endlist %}

## Set up your transfer {#prepare-transfer}

1. Create a `Greenplum®` [source endpoint](../operations/endpoint/source/greenplum.md) and specify its cluster connection settings.

1. [Create](../operations/transfer.md#create) a [Snapshot](../concepts/index.md#transfer-type) transfer that uses endpoints for the source and target.

    While real-time replication is not supported for this endpoint pair, you can configure regular copying while creating the transfer. To do this, in the **Transfer parameters** field under **Snapshot**, select **Regular** and specify the copy interval. The transfer will automatically activate after the specified interval.

## Activate the transfer {#activate-transfer}

1. [Connect to the Greenplum® cluster](../../managed-greenplum/operations/connect/index.md), create a table named `x_tab`, and populate it with data:

    ```sql
    CREATE TABLE x_tab
    (
        id NUMERIC,
        name CHARACTER(5)
    );
    CREATE INDEX ON x_tab (id);
    INSERT INTO x_tab (id, name) VALUES
    (40, 'User1'),
    (41, 'User2'),
    (42, 'User3'),
    (43, 'User4'),
    (44, 'User5');
    ```

1. [Activate the transfer](../operations/transfer.md#activate) and wait for its status to change to **Completed**.
1. To verify the data transfer, connect to the Managed Service for ClickHouse® target cluster and check whether the columns of the `x_tab` table in the `db1` database match those of the `x_tab` table in the source database:

   ```sql
   SELECT id, name FROM db1.x_tab;
   ```

   ```text
   ┌─id─┬─name──┐
   │ 40 │ User1 │
   │ 41 │ User2 │
   │ 42 │ User3 │
   │ 43 │ User4 │
   │ 44 │ User5 │
   └────┴───────┘
   ```

## Verify that the copy operation works upon reactivation {#example-check-copy}

1. [Connect to the Greenplum® cluster](../../managed-greenplum/operations/connect/index.md). Then, in the `x_tab` table, delete one row and update another:

    ```sql
    DELETE FROM x_tab WHERE id = 41;
    UPDATE x_tab SET name = 'Key3' WHERE id = 42;
    ```

1. [Reactivate the transfer](../operations/transfer.md#activate) and wait for its status to change to **Completed**.
1. Make sure the changes have been applied to the `x_tab` table on the ClickHouse® target:

    ```sql
    SELECT id, name FROM db1.x_tab;
    ```

    ```text
    ┌─id─┬─name──┐
    │ 42 │ Key3  │
    │ 40 │ User1 │
    │ 43 │ User4 │
    │ 44 │ User5 │
    └────┴───────┘
    ```

## Delete the resources you created {#clear-out}

To minimize resource consumption, delete the resources you no longer need:

1. Make sure the transfer status is **Completed**, upon which you can [delete the transfer](../operations/transfer.md#delete).
1. [Delete the source endpoint](../operations/endpoint/index.md#delete).
1. Delete the rest of the resources depending on how you created them:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the Managed Service for ClickHouse®](../../managed-clickhouse/operations/cluster-delete.md) cluster.
        1. [Delete the Greenplum®](../../managed-greenplum/operations/cluster-delete.md) cluster.
        1. [Delete the target endpoint](../operations/endpoint/index.md#delete).

    - Terraform {#tf}

        1. In the terminal window, go to the directory containing the infrastructure plan.
        
            {% note warning %}
        
            Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
        
            {% endnote %}
        
        1. Delete resources:
        
            1. Run this command:
        
                ```bash
                terraform destroy
                ```
        
            1. Confirm deleting the resources and wait for the operation to complete.
        
            All the resources described in the Terraform manifests will be deleted.

    {% endlist %}

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