oracle迁移PostgreSQL(使用 ora_migrator + oracle_fdw )

说明:
ora_migrator是一个插件,提供了一系列内置函数,使用这些函数接口以及oracle_fdw插件(PostgreSQL访问oracle的fdw接口),可以将Oracle的schema(包括table,view,sequence,function,procedure, trigger,等)连同数据,迁移到Oracle(过程和程序包需要手动迁移)。

安装:
oracle_fdw的安装见:oracle同步数据到PostgreSQL——oracle_fdw https://shimo.im/docs/JtyqRkcTcrPRpqDH
ora_migrator安装:
下载地址:https://github.com/cybertec-postgresql/ora_migrator
ora_migrator安装方式也比较简单,将安装包解压之后安装即可,然后去数据库中创建插件。

bill=# create extension ora_migrator ;
CREATE EXTENSION

使用举例:
安装完oracle_fdw和ora_migrator插件后,就可以开始迁移了。

1、构建oracle环境
这里创建三张表用来模拟有索引、有外键约束的情况。因为之前有使用过ora2pg工具迁移,但是在存在外键的情况下会导致数据导致出差。这里验证下ora_migrator能够解决这个问题。

SQL> create table bill.test1(id int,info varchar2(20));

Table created.

SQL> insert into bill.test1 values(1,'a');

1 row created.
SQL> create index idx_t1 on test1(id);

Index created.
SQL> create table tb_supplier
  2  (
  supplier_id number not null,
  supplier_name varchar2(50) not null,
  3    4    5    contact_name varchar2(50),
  6    CONSTRAINT pk_supplier PRIMARY KEY (supplier_id)
  7  );

Table created.

SQL> create table tb_products
(
  product_id number not null,
  2    3    4    product_name varchar2(100),
  supplier_id number not null,
  5    6    constraint fk_products_supplier foreign key (supplier_id) references tb_supplier(supplier_id)
);  7  

SQL>  insert into tb_supplier values(1,'a','a');

1 row created.
SQL> insert into tb_products values(1,'aa',1);

1 row created.

2、创建连接ORACLE的server,用户映射

CREATE SERVER oradb_bill FOREIGN DATA WRAPPER oracle_fdw OPTIONS (dbserver 'orasid');

create user mapping for bill server oradb_bill options(user 'orauser',password 'orapwd');

3、迁移
以上oracle foreign server访问ORACLE,将bill这个用户下的对象进行迁移。

bill=# SELECT oracle_migrate(server => 'oradb_bill', only_schemas => '{BILL}');  
NOTICE:  Creating staging schemas "ora_stage" and "pgsql_stage" ...
NOTICE:  Creating Oracle metadata views in schema "ora_stage" ...
NOTICE:  Copying definitions to PostgreSQL staging schema "pgsql_stage" ...
NOTICE:  Creating schemas ...
WARNING:  Error creating schema "bill"
DETAIL:  schema "bill" already exists: 
NOTICE:  Creating sequences ...
NOTICE:  Creating foreign tables ...
NOTICE:  Migrating table bill.tb_products ...
NOTICE:  Migrating table bill.tb_supplier ...
NOTICE:  Migrating table bill.test1 ...
NOTICE:  Creating UNIQUE and PRIMARY KEY constraints ...
NOTICE:  Creating FOREIGN KEY constraints ...
NOTICE:  Creating CHECK constraints ...
NOTICE:  Creating indexes ...
NOTICE:  Setting column default values ...
NOTICE:  Dropping staging schemas ...
NOTICE:  Migration completed with 1 errors.
 oracle_migrate 
----------------
              1
(1 row)

NOTICE中记录了迁移过程中的日志,这里出现一个schema已存在的错误。

4、验证
表全部迁移过来了。

bill=# \d
              List of relations
 Schema |        Name        | Type  | Owner 
--------+--------------------+-------+-------
 bill   | tb_products        | table | bill
 bill   | tb_supplier        | table | bill
 bill   | test1              | table | bill
 public | pg_stat_statements | view  | bill
(4 rows)

含有外键约束的表数据是否迁移成功了呢?可以发现没有数据,而且迁移的日志中也没有记录这个错误!

bill=# select * from tb_supplier;
 supplier_id | supplier_name | contact_name 
-------------+---------------+--------------
(0 rows)

bill=# select * from tb_products;
 product_id | product_name | supplier_id 
------------+--------------+-------------
(0 rows)

另一张表的数据成功迁移了:

bill=# select * from test1;
 id | info 
----+------
  1 | a
(1 row)

总结:
这种使用 ora_migrator + oracle_fdw的方法来迁移数据是利用pg外部接口来实现的,相比较与ora2pg插件使用起来会比较简单,但是感觉ora2pg工具的功能还是更加强大。两者似乎对于外键约束的情况均无法直接解决,均存在不足之处。

发布了70 篇原创文章 · 获赞 5 · 访问量 3143

猜你喜欢

转载自blog.csdn.net/weixin_39540651/article/details/103289680