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 |
✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | |
✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | |
✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ❌ | |
✔️ | ❌ | ❌ | ❌ | ❌ | ❌ |
Prerequisites
Ensure network connectivity between the PostgreSQL instance and the SelectDB instance.
The PostgreSQL instance and the SelectDB instance must be in the same VPC. If they are not, you must first resolve the network connectivity issue. For more information, see How to resolve network connectivity issues between a SelectDB instance and a data source?
The IP address of the PostgreSQL instance has been added to the IP address whitelist of SelectDB. For more information, see Set an IP address whitelist.
If the PostgreSQL instance has an IP address whitelist, the IP address range of the network segment for the SelectDB instance has been added to the IP address whitelist of the PostgreSQL instance.
To get the IP range of the VPC where your SelectDB instance resides, see How do I find the IP CIDR block of the VPC where my ApsaraDB for SelectDB instance is located?
To get the public IP address of your SelectDB instance, use the
pingcommand on its public endpointSelectDB.
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.
To synchronize data, see the following topics:
To migrate data, see the following topics:
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.
Create a PostgreSQL data source.
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_dbFE 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:8080Username
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.
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.tgzGo 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.
NoteDatabase 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.jarStart a Flink standalone cluster. Example:
bin/start-cluster.shCreate a SelectDB instance. For more information, see Create an instance.
Connect to a SelectDB instance using the MySQL protocol. For more information, see Connect to an instance.
Create a test database and a test table.
Create a test database.
CREATE DATABASE test_db;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, |
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 |
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.
Connect to the SelectDB instance. For more information, see Connect to an instance.
NoteWhen you log in by using DMS, the
SWITCHcommand fails. We recommend that you connect by using a MySQL client.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.
After you create a table in SelectDB, you can use the in-database ETL syntax
insert into selectto synchronize data. For more information aboutinsert 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;