[Yandex Cloud documentation](../../index.md) > [Yandex MPP Analytics for PostgreSQL](../index.md) > [Tutorials](index.md) > Loading data from Object Storage to Yandex MPP Analytics for PostgreSQL using Data Transfer

# Loading data from Yandex Object Storage to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

# Loading data from Yandex Object Storage to Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer

You can migrate data from Yandex Object Storage to the Greenplum® table in Yandex MPP Analytics for PostgreSQL using Yandex Data Transfer. Follow these steps:

1. [Prepare your test data](#prepare-data).
1. [Create a database in the target cluster](#prepare-data).
1. [Prepare and activate your transfer](#prepare-transfer).
1. [Test the transfer](#verify-transfer).

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


## Required paid resources {#paid-resources}

* Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* Yandex MPP Analytics for PostgreSQL cluster, which includes the use of computing resources allocated to hosts, storage and backup size (see [Yandex MPP Analytics for PostgreSQL pricing](../pricing/index.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}


1. Set up your infrastructure:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Create a target Greenplum® cluster](../operations/cluster-create.md) in any suitable configuration with publicly available hosts and the following settings:

            * **Username**: `user1`.
            * **Password**: `<user_password>`.

            {% 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. If you are using security groups in a cluster, make sure they are [configured correctly](../operations/connect/index.md#configuring-security-groups) and allow connections to it.


        1. [Create an Object Storage bucket](../../storage/operations/buckets/create.md).

        
        1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `storage-viewer` with the `storage.viewer` role. The transfer will use it to access the bucket.

        1. [Create a static access key](../../iam/operations/authentication/manage-access-keys.md#create-access-key) for the `storage-viewer` service account.


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

            This file describes:

            * [Network](../../vpc/concepts/network.md#network).
            * [Subnet](../../vpc/concepts/network.md#subnet).
            * Cluster access [security group](../../vpc/concepts/security-groups.md).
            * Service account for bucket operations, e.g., creation and access.
            * Yandex Lockbox secret for the service account static key required to configure the source endpoint.
            * Object Storage source bucket.
            * Greenplum® target cluster in Yandex MPP Analytics for PostgreSQL.
            * Transfer.

        1. In the `object-storage-to-greenplum.tf` file, specify the values of the following variables:

            * `folder_id`: Cloud folder ID, same as in the provider settings.
            * `bucket_name`: Bucket name consistent with the [naming conventions](../../storage/concepts/bucket.md#naming).
            * `gp_version`: Greenplum® version.
            * `gp_password`: Greenplum® 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 %}

1. [Enable](../operations/update.md#change-additional-settings) the **Access from Data Transfer** option in the target cluster.

## Prepare your test data {#prepare-data}

1. Create two CSV files with test data:

    * `demo_data1.csv`:

        ```csv
        1,Anna
        2,Robert
        3,Umar
        4,Algul
        5,Viktor
        ```

    * `demo_data2.csv`:

        ```csv
        6,Maria
        7,Alex
        ```

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data1.csv` file to the Object Storage bucket.

## Create a database in the target cluster {#prepare-data}

1. [Connect](../operations/connect/index.md) to the auxiliary `postgres` database in the Greenplum® target cluster as `user1`.

1. Create a database named `db1`:

    ```sql
    CREATE DATABASE db1;
    ```

## Prepare and activate your transfer {#prepare-transfer}

1. [Create a source endpoint](../../data-transfer/operations/endpoint/source/object-storage.md#objstorage-name) of the `Object Storage` type with the following settings:

    * **Database type**: `Object Storage`.
    * **Bucket**: Object Storage bucket name.

    
    * **Access Key ID**: Service account’s static access key ID. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).
    * **Secret Access Key**: Service account’s secret access key. If you created your infrastructure using Terraform, [copy the key’s value from the Yandex Lockbox secret](../../lockbox/operations/secret-get-info.md#secret-contents).


    * **Endpoint**: `https://storage.yandexcloud.net`.
    * **Region**: `ru-central1`.
    * **Data format**: `CSV`.
    * **Delimiter**: Comma (`,`).
    * **Table**: `table1`.
    * **Result table schema**: Select `Manual` and specify the following field names and data types:

        * `Id`: `Int64`
        * `Name`: `UTF8`

    Leave the other settings at their defaults.

1. [Create a `Greenplum®` target endpoint](../../data-transfer/operations/endpoint/target/greenplum.md#gp) with these cluster connection settings:

    * **Connection type**: `Cluster`
    * **Cluster**: `<target_Greenplum®_cluster_name>` from the drop-down list.
    * **Database**: `db1`.
    * **User**: `user1`.
    * **Password**: `<user_password>`.

1. Create and activate your transfer:

    {% list tabs group=instructions %}

    - Manually {#manual}

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

        1. [Activate the transfer](../../data-transfer/operations/transfer.md#activate) and wait for its status to change to **Replicating**.

    - Using Terraform {#tf}

        1. In the `object-storage-to-greenplum.tf` file, specify these variables:

            * `source_endpoint_id`: Source endpoint ID.
            * `target_endpoint_id`: Target endpoint ID.
            * `transfer_enabled`: Set to `1` to create a 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.

        1. The transfer will be activated automatically. Wait for its status to change to **Replicating**.

    {% endlist %}

## Test the transfer {#verify-transfer}

Make sure the transfer works correctly by testing copying and replication.

### Test copying {#verify-copy}

1. [Connect](../operations/connect/index.md) to the `db1` database in the Greenplum® target cluster.

1. Run this request:

    ```sql
    SELECT * FROM public.table1;
    ```

    {% cut "Response example" %}

    ```sql
      __file_name  | __row_index | Id |  Name
    ---------------+-------------+----+--------
    demo_data1.csv |           1 |  1 | Anna
    demo_data1.csv |           2 |  2 | Robert
    demo_data1.csv |           3 |  3 | Umar
    demo_data1.csv |           4 |  4 | Algul
    demo_data1.csv |           5 |  5 | Viktor
    ```

    {% endcut %}

### Test replication {#verify-replication}

1. [Upload](../../storage/operations/objects/upload.md#simple) the `demo_data2.csv` file to the Object Storage bucket.

1. Make sure the data from `demo_data2.csv` has been added to the target database:

    1. [Connect](../operations/connect/index.md) to the `db1` database in the Greenplum® target cluster.

    1. Run this request:

        ```sql
        SELECT * FROM public.table1;
        ```

        {% cut "Response example" %}

        ```sql
          __file_name  | __row_index | Id |  Name
        ---------------+-------------+----+--------
        demo_data1.csv |           1 |  1 | Anna
        demo_data1.csv |           2 |  2 | Robert
        demo_data1.csv |           3 |  3 | Umar
        demo_data1.csv |           4 |  4 | Algul
        demo_data1.csv |           5 |  5 | Viktor
        demo_data2.csv |           1 |  6 | Maria
        demo_data2.csv |           2 |  7 | Alex
        ```

        {% endcut %}

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

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

1. [Delete the transfer](../../data-transfer/operations/transfer.md#delete).
1. [Delete the source endpoint](../../data-transfer/operations/endpoint/index.md#delete).
1. [Delete the target endpoint](../../data-transfer/operations/endpoint/index.md#delete).
1. [Delete the objects](../../storage/operations/objects/delete.md) from the bucket.
1. Delete the rest of the resources depending on how you created them:

    {% list tabs group=instructions %}

    - Manually {#manual}

        1. [Delete the Greenplum® cluster](../operations/cluster-delete.md).
        1. [Delete the Object Storage bucket](../../storage/operations/buckets/delete.md).

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