Oracle_fdw

本文以例子的形式介绍 Kingbase (Postgresql)数据库如何通过 oracle_fdw 扩展访问Oracle数据库。以下例子在PG12.3 与 Kingbase V8R6进行过实际验证。

一、准备数据

1、Oracle端

IP:192.168.237.42, SID:SOGG ,数据库用户:user01 / user01
SQL> create table t1(id integer,name char(9));

2、Kingbase端

IP:192.168.237.43
create role user_ora with login password 'user_ora';
create schema user_ora authorization user_ora;

二、安装配置

1、安装fdw

解压安装包,将相关的文件拷贝到对应的 share , lib 目录。注意:要确认安装包是否包含 libclntsh.so 文件,如果没有,需要从oracle 安装路径拷贝。

2、创建扩展

create extension oracle_fdw;
create server to_ora42 foreign data wrapper oracle_fdw options(dbserver '//192.168.237.42:1521/SOGG');
grant usage on foreign server to_ora42 to user_ora;
create user mapping for user_ora server to_ora42 options(user 'user01',password 'user01');

server与mapping 创建后,在pg_foreign_server 和pg_user_mappings 会有相应条目

test=# select * from pg_foreign_server where srvname='to_ora42';
  oid  | srvname  | srvowner | srvfdw | srvtype | srvversion | srvacl |              srvoptions               
-------+----------+----------+--------+---------+------------+--------+---------------------------------------
 16552 | to_ora42 |       10 |  16549 |         |            |        | {dbserver=//192.168.237.42:1521/SOGG}

test=# select * from pg_user_mappings where srvname='to_ora42';
 umid  | srvid | srvname  | umuser | usename  |           umoptions           
-------+-------+----------+--------+----------+-------------------------------
 16553 | 16552 | to_ora42 |  16550 | user_ora | {user=user01,password=user01}
(1 row)

3、外部表

create foreign table ft_t1(id integer,name char(9)) server to_ora42 options(schema 'USER01', table 'T1');

注意:这里的  'USER01' 和 'T1' 都是大写的,与Oracle 数据字典的信息大小写一致。否则在访问时会有如下问题:

test=> create foreign table ft_t1(id integer,name char(9)) server to_ora42 options(schema 'USER01', table 't1');
CREATE FOREIGN TABLE
test=> select * from ft_t1;                                                                                     
ERROR:  Oracle table "USER01"."t1" for foreign table "ft_t1" does not exist or does not allow read access
DETAIL:  ORA-00942: table or view does not exist
HINT:  Oracle table names are case sensitive (normally all uppercase).

三、可以手动创建wrapper

create extension 后,pg_foreign_data_wrapper 就有 oracle_fdw , fdwoptions 为空。在 fdwoptions 可以指定些选项,比如字符集。

create foreign data wrapper oracle_fdw_1 handler oracle_fdw_handler validator oracle_fdw_validator options (nls_lang 'AMERICAN_AMERICA.ZHS16GBK');

 

上一篇:MySQL外键字段为什么必须加索引


下一篇:Mysql完整性约束详解(字段唯一,非空,主键primary key,外键foreign key,自增长auto_increment)