All Products
Search
Document Center

ApsaraDB for SelectDB:Migrate data from a PostgreSQL data source

Last Updated:Mar 28, 2026

ApsaraDB for SelectDB supports four approaches for loading data from a PostgreSQL source: Data Transmission Service (DTS), DataWorks, Flink change data capture (CDC), and catalog. The PostgreSQL source can be a self-managed PostgreSQL database, an ApsaraDB RDS for PostgreSQL instance, or a PolarDB for PostgreSQL cluster.

Compare migration solutions

SolutionHistorical data migrationIncremental data synchronizationSchema migrationDatabase migrationIncremental synchronization of DDL operationsData verification
DTSYesYesYesYesYesYes
DataWorksYesYesYesYesYesNo
Flink CDCYesYesYesYesYesNo
CatalogYesNoNoNoNoNo

Choose a migration solution

  • DTS: Best for production workloads that require real-time synchronization, DDL propagation, and end-to-end data verification. DTS handles the full migration lifecycle with minimal manual setup.

  • DataWorks: Suitable when your team already uses DataWorks for data integration workflows and needs batch synchronization with scheduling support.

  • Flink CDC: Suitable for teams with existing Flink infrastructure who need fine-grained control over the synchronization pipeline, including customizing parallelism and table filtering.

  • Catalog: A lightweight option for one-time historical data loads. Use a JDBC catalog to query PostgreSQL data through federated queries, then copy it into SelectDB with INSERT INTO SELECT.

Prerequisites

Before you begin, make sure that:

Use DTS to migrate data

DTS supports historical data migration, incremental data synchronization, schema migration, DDL operation synchronization, and data verification.

Refer to the following DTS guides based on your PostgreSQL source type:

Data synchronization:

Data migration:

Use DataWorks to migrate data

Step 1: Add data sources

Add a PostgreSQL data source and a SelectDB data source to DataWorks.

  1. Add a PostgreSQL data source. For details, see PostgreSQL data source.

  2. Add a SelectDB data source. For details, see Add and manage data sources. Use the following parameters when adding the SelectDB data source:

    ParameterDescriptionExample
    Data Source NameA name for this data source connection.selectdb_prod
    Host Address/IP AddressThe JDBC URL in jdbc:mysql://<ip>:<port>/<dbname> format. Get the VPC endpoint or public endpoint and the MySQL port from the Network Information section of the Instance Details page in the SelectDB console.jdbc:mysql://selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:9030/test_db
    HTTP Connection AddressThe HTTP endpoint in <ip>:<port> format. Get the VPC endpoint or public endpoint and the HTTP port from the Network Information section of the Instance Details page in the SelectDB console.selectdb-cn-4xl3jv1****.selectdbfe.rds.aliyuncs.com:8080
    UsernameThe username of the owner account of the SelectDB instance.admin
    PasswordThe password of the owner account of the SelectDB instance.

Step 2: Configure a data synchronization task

Create a batch synchronization task using either the visual editor or the code editor:

Use Flink CDC to migrate data

Flink CDC supports historical data migration, incremental data synchronization, schema migration, and DDL operation synchronization.

Set up the environment

The following steps deploy a Flink standalone cluster of version 1.16.

  1. Download and extract the Flink package.

    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

    If version 1.16.3 is no longer available, download a later version from Apache Flink downloads.

  2. Download the CDC connector and the SelectDB connector into the lib/ directory.

    Full data synchronization requires Flink 1.15 or later. For other connector versions, see org/apache/doris on Maven.
    cd flink-1.16.3/lib/
    wget https://repo1.maven.org/maven2/com/ververica/flink-sql-connector-mysql-cdc/2.4.2/flink-sql-connector-mysql-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 the Flink standalone cluster.

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

  5. Connect to the SelectDB instance over the MySQL protocol. For details, see Connect to an instance.

  6. Create a test database and table in SelectDB.

    CREATE DATABASE test_db;
    
    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

The following command submits a Flink CDC job that synchronizes all tables matching the filter from a PostgreSQL source to the SelectDB database db1.

<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=****

Parameter reference:

ParameterRequiredDescriptionExample
execution.checkpointing.intervalYesCheckpoint interval, which controls synchronization frequency.10s
parallelism.defaultNoParallelism of the Flink job. Increase this value to accelerate synchronization.1
databaseYesThe target database name in the SelectDB instance.db1
including-tablesNoPostgreSQL tables to synchronize. Separate multiple table names with |. Regular expressions are supported.table1|tbl.*
excluding-tablesNoTables to exclude. Uses the same syntax as including-tables.tmp.*
postgres-confYesPostgres CDC source configuration. The hostname, username, password, table-name, schema-name, and database-name parameters are required. For the full parameter list, see Postgres CDC Connector.
sink-confYesDoris sink configuration. For details, see Use Flink to import data.
table-confNoSelectDB table properties applied when the target table is created.

Use a catalog to migrate data

The catalog approach uses a JDBC catalog to query PostgreSQL data through federated queries, then copies it into SelectDB with INSERT INTO SELECT. This approach is suited for one-time historical data loads.

Do not use Data Management (DMS) to connect to the SelectDB instance for this procedure — the SWITCH statement becomes invalid in DMS sessions. Use a MySQL client instead.
  1. Connect to the SelectDB instance. For details, see Connect to an instance.

  2. Create a JDBC catalog for the PostgreSQL source.

    Parameters:

    ParameterRequiredDescriptionExample
    userYesThe account used to connect to the PostgreSQL source.root
    passwordYesThe password for the account.123456
    jdbc_urlYesThe JDBC URL for the PostgreSQL source.jdbc:postgresql://127.0.0.1:5432/demo
    driver_urlYesThe filename of the JDBC driver JAR.postgresql-42.5.1.jar
    driver_classYesThe JDBC driver class name.org.postgresql.Driver
    lower_case_table_namesNoWhether to sync database and table names in lowercase. Default: false.false
    only_specified_databaseNoWhether to migrate only specific databases. Default: false.false
    include_database_listNoComma-separated list of databases to migrate. Takes effect only when only_specified_database=true. Database names are case-sensitive. Default: "".db1,db2
    exclude_database_listNoComma-separated list of databases to exclude. Takes effect only when only_specified_database=true. Database names are case-sensitive. Default: "".tmp_db
    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",
        "checksum" = "20c8228267b6c9ce620fddb39467d3eb"
    );

    For the complete catalog configuration reference, see JDBC data source.

  3. Create a table in SelectDB and copy data from the PostgreSQL source using INSERT INTO SELECT.

    -- Create the target table
    CREATE TABLE selectdb_table ...
    
    -- Migrate data from the PostgreSQL source
    INSERT INTO selectdb_table SELECT * FROM jdbc_postgresql.pg_database.pg_table;

    For details on the INSERT INTO syntax, see Use INSERT INTO to import data.