Migrate data from PostgreSQL

更新时间:
复制 MD 格式

You can use DTS, DataWorks, Flink CDC, and Catalog to migrate data from PostgreSQL sources, such as self-managed PostgreSQL, ApsaraDB RDS for PostgreSQL, and PolarDB for PostgreSQL, to ApsaraDB for SelectDB. You can select a suitable migration method based on the data volume and your business scenario.

Migration solutions

DTS, DataWorks, Flink CDC, and catalog can all be used to migrate data from PostgreSQL to SelectDB, but the supported data varies for each method. You can choose the appropriate migration method based on your business scenario.

Solution

Historical data migration

Incremental data synchronization

Schema migration

Database migration

Incremental DDL sync

Data verification

DTS

✔️

✔️

✔️

✔️

✔️

✔️

DataWorks

✔️

✔️

✔️

✔️

✔️

Flink CDC

✔️

✔️

✔️

✔️

✔️

catalog

✔️

Prerequisites

Procedure

Migrate with DTS

DTS supports migrating historical data and synchronizing incremental data from PostgreSQL to SelectDB. It also supports features such as schema migration, DDL synchronization, and data verification.

Migrate with DataWorks

Step 1: Add data sources

When you develop a data synchronization task, you need to create PostgreSQL and SelectDB data sources in DataWorks.

  1. Create a PostgreSQL data source.

  2. Create a SelectDB data source. For more information, see Data Source Management. Some configuration parameters for the SelectDB data source are as follows:

    Parameter

    Description

    Data source name

    The name of the data source.

    JDBC URL

    JDBC connection string jdbc:mysql://<ip>:<port>/<dbname>.

    Find the VPC Endpoint (or Public Endpoint) and MySQL Port on the Instance Details > Network Information page in the SelectDB console.

    Example: jdbc:mysql://selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:9030/test_db

    FE HTTP endpoint

    The HTTP access URL is <ip>:<port>.

    Find the VPC Endpoint (or Public Endpoint) and HTTP Port on the Instance Details > Network Information page in the SelectDB console.

    Example: selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:8080

    Username

    The username of the SelectDB instance.

    Password

    The password for the user of the SelectDB instance.

Step 2: Configure data synchronization

Configure a data synchronization task by using one of the following methods:

Migrate with Flink CDC

Flink uses Flink CDC to migrate data from PostgreSQL to SelectDB. Flink CDC supports both historical data migration and incremental data synchronization, and provides comprehensive capabilities such as schema and table migration and DDL synchronization.

Prepare the environment

Set up a Flink environment. This example uses a Flink 1.16 standalone environment.

  1. Download and extract the flink-1.16.3-bin-scala_2.12.tgz package. To find other available versions, see Apache Flink.

    wget https://archive.apache.org/dist/flink/flink-1.16.3/flink-1.16.3-bin-scala_2.12.tgz
    tar -zxvf flink-1.16.3-bin-scala_2.12.tgz
  2. Go to the FLINK_HOME/lib directory and download flink-sql-connector-postgres-cdc-2.4.2 and flink-doris-connector-1.16-1.5.2. The following sample code is for your reference.

    Note

    Database migration is supported in Flink 1.15 and later. For information about how to download the Flink Doris Connector for different Flink versions, see Flink Doris Connector.

    cd flink-1.16.3
    cd lib/
    wget https://repo.maven.apache.org/maven2/org/apache/flink/flink-sql-connector-postgres-cdc/2.4.2/flink-sql-connector-postgres-cdc-2.4.2.jar
    wget https://repo.maven.apache.org/maven2/org/apache/doris/flink-doris-connector-1.16/1.5.2/flink-doris-connector-1.16-1.5.2.jar
  3. Start a Flink standalone cluster. Example:

    bin/start-cluster.sh
  4. Create a SelectDB instance. For more information, see Create an instance.

  5. Connect to a SelectDB instance using the MySQL protocol. For more information, see Connect to an instance.

  6. Create a test database and a test table.

    1. Create a test database.

      CREATE DATABASE test_db;
    2. Create a test table.

      USE test_db;
      CREATE TABLE employees (
          emp_no       int NOT NULL,
          birth_date   date,
          first_name   varchar(20),
          last_name    varchar(20),
          gender       char(2),
          hire_date    date
      )
      UNIQUE KEY(`emp_no`)
      DISTRIBUTED BY HASH(`emp_no`) BUCKETS 1;

Submit a Flink CDC job

Use the following syntax to submit the Flink CDC job:

<FLINK_HOME>/bin/flink run \
    -Dexecution.checkpointing.interval=10s \
    -Dparallelism.default=1 \
    -c org.apache.doris.flink.tools.cdc.CdcTools \
    lib/flink-doris-connector-1.16-1.5.2.jar \
    postgres-sync-database \
    --database db1\
    --postgres-conf hostname=127.0.0.1 \
    --postgres-conf port=5432 \
    --postgres-conf username=postgres \
    --postgres-conf password="123456" \
    --postgres-conf database-name=postgres \
    --postgres-conf schema-name=public \
    --postgres-conf slot.name=test \
    --postgres-conf decoding.plugin.name=pgoutput \
    --including-tables "tbl1|test.*" \
    --sink-conf fenodes=selectdb-cn-****.selectdbfe.rds.aliyuncs.com:8080 \
    --sink-conf username=admin \
    --sink-conf password=****

Parameters

Parameter

Required

Description

execution.checkpointing.interval

Yes

The Flink checkpoint interval. This affects the data synchronization frequency. A value of 10s is recommended.

parallelism.default

No

The parallelism of the Flink job. You can increase the parallelism to improve data synchronization speed.

database

Yes

The database name for synchronization to SelectDB.

including-tables

No

The PostgreSQL tables to synchronize.

You can use a pipe (|) to separate multiple table names. Regular expressions are supported.

For example, --including-tables table1|tbl.* synchronizes table1 and all tables that start with tbl.

excluding-tables

No

The tables to exclude. This parameter is configured in the same way as including-tables.

postgres-conf

Yes

Postgres CDC Source configuration. For configuration details, see Postgres CDC Connector, where hostname, username, password, database-name, table-name, and schema-name are required.

sink-conf

Yes

Configurations for the Doris Sink. For more information, see Import data by using Flink.

table-conf

No

Configurations for the SelectDB table. These are the properties specified when you create the SelectDB table.

Migrate with a catalog

The Catalog feature in SelectDB allows you to use federated queries to access PostgreSQL, enabling you to easily and quickly migrate historical data from PostgreSQL to SelectDB.

  1. Connect to the SelectDB instance. For more information, see Connect to an instance.

    Note

    When you log in by using DMS, the SWITCH command fails. We recommend that you connect by using a MySQL client.

  2. Create a PostgreSQL JDBC catalog.

    CREATE CATALOG jdbc_postgresql PROPERTIES (
        "type"="jdbc",
        "user"="root",
        "password"="123456",
        "jdbc_url" = "jdbc:postgresql://127.0.0.1:5432/demo",
        "driver_url" = "postgresql-42.5.1.jar",
        "driver_class" = "org.postgresql.Driver"
    );

    Parameters

    Parameter

    Required

    Description

    user

    Yes

    The username for the database account.

    password

    Yes

    The password for the database account.

    jdbc_url

    Yes

    The JDBC connection string.

    driver_url

    Yes

    The JAR file name of the JDBC driver.

    driver_class

    Yes

    The class name of the JDBC driver.

    lower_case_table_names

    No

    Specifies whether to synchronize the database and table names from the external JDBC data source in lowercase.

    Default value: "false"

    only_specified_database

    No

    Specifies whether to synchronize only the specified databases.

    Default value: "false"

    include_database_list

    No

    When only_specified_database=true, specify the multiple databases to synchronize, separated by commas (,). Database names are case-sensitive.

    Default value: ""

    exclude_database_list

    No

    When only_specified_database=true, specify the databases to synchronize. Use commas (,) to separate multiple database names. The database names are case-sensitive.

    Default value: ""

    For more information, see JDBC data sources.

  3. After you create a table in SelectDB, you can use the in-database ETL syntax insert into select to synchronize data. For more information about insert into, see Insert Into.

    # Create a table.
    CREATE TABLE selectdb_table ...
    
    # Migrate data.
    INSERT INTO selectdb_table SELECT * FROM jdbc_postgresql.pg_database.pg_table;