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

# Loading Greenplum® data to a PostgreSQL data mart

You can migrate a database from Greenplum® to the PostgreSQL cluster using Yandex Data Transfer.

To transfer a database from Greenplum® to PostgreSQL:

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)).
* Managed Service for PostgreSQL cluster: use of computing resources allocated to hosts, storage and backup size (see [Yandex Managed Service for PostgreSQL pricing](../../managed-postgresql/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}

    {% note info %}
    
    Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.
    
    {% endnote %}

    1. [Create a Greenplum® source cluster](../../managed-greenplum/operations/cluster-create.md#create-cluster) in any suitable configuration with the `gp-user` admin username and public hosts.

    1. [Create a Yandex Managed Service for PostgreSQL target cluster](../../managed-postgresql/operations/cluster-create.md#create-cluster) in any suitable configuration with publicly accessible hosts. When creating a cluster, specify:

        * **Username**: `pg-user`.
        * **DB name**: `db1`.

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

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


- 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-postgresql.tf](https://github.com/yandex-cloud-examples/yc-data-transfer-from-greenplum-to-postgresql/blob/main/greenplum-postgresql.tf) configuration file to the same working directory.

        This file describes:

        * [Networks](../../vpc/concepts/network.md#network) and [subnets](../../vpc/concepts/network.md#subnet) that will host your clusters.
        * [Security groups](../../vpc/concepts/security-groups.md) to connect to clusters.
        * Greenplum® source cluster in Yandex MPP Analytics for PostgreSQL.
        * Managed Service for PostgreSQL target cluster.
        * Target endpoint.
        * Transfer.

    1. In the `greenplum-postgresql.tf` file, specify the admin user passwords and Greenplum® and PostgreSQL versions.
    1. Run the `terraform init` command in the directory with the configuration file. This command initializes the provider specified in the configuration files and enables you to use its resources and data sources.
    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 the transfer {#prepare-transfer}

1. [Create a `Greenplum®`-type source endpoint](../operations/endpoint/source/greenplum.md) and configure it using the following settings:

    * **Connection type**: `Cluster`.
    * **Cluster**: `<Greenplum®_source_cluster_name>` from the drop-down list
    * **Database**: `postgres`.
    * **User**: `gp-user`.
    * **Password**: `<user_password>`.
    * **Service object schema**: `public`.

1. Create a target endpoint and a transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a `PostgreSQL` target endpoint](../operations/endpoint/target/postgresql.md) with these cluster connection settings:

            * **Installation type**: `Yandex Managed Service for PostgreSQL cluster`
            * **Managed Service for PostgreSQL cluster**: `<PostgreSQL_target_cluster_name>` from the drop-down list.
            * **Database**: `db1`.
            * **User**: `pg-user`.
            * **Password**: `<user_password>`.

        1. [Create a transfer](../operations/transfer.md#create) of the **_Snapshot_**-type that will use the endpoints you created.

            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 **Snapshot** field under **Transfer parameters**, select **Regular** and specify the copy interval. The transfer will automatically activate after the specified interval.

            {% note warning %}

            Before setting up regular copying, verify that the [target endpoint](../operations/endpoint/target/postgresql.md#additional-settings) is configured with a `DROP` or `TRUNCATE` cleanup policy to prevent data duplication.

            {% endnote %}

    - Terraform {#tf}

        1. In the `greenplum-postgresql.tf` file, specify these variables:

            * `gp_source_endpoint_id`: Source endpoint ID.
            * `transfer_enabled`: Set to `1` to create the transfer.

        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.

    {% endlist %}

## 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 check that the data was transferred correctly, connect to the Managed Service for PostgreSQL target cluster and make sure that 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.public.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. In the [target endpoint parameters](../operations/endpoint/target/postgresql.md#additional-settings), select either `DROP` or `TRUNCATE` as cleanup policy.
1. [Connect to the Greenplum® cluster](../../managed-greenplum/operations/connect/index.md).
1. In the `x_tab` table, delete the row with the `41` ID and update the one with the `42` ID:

    ```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. Check the changes in the `x_tab` table of the PostgreSQL target:

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

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

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

To reduce the consumption of resources, delete those you do not need:

1. Make sure the transfer status is **Completed**, upon which you can [delete](../operations/transfer.md#delete) the transfer.
1. [Delete both the source and target endpoints](../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 PostgreSQL cluster](../../managed-postgresql/operations/cluster-delete.md).
        1. [Delete the Yandex MPP Analytics for PostgreSQL cluster](../../managed-greenplum/operations/cluster-delete.md).

    - 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 %}