oracle_fdw的安装和使用

1.下载instant oracle client

下载网址:https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

下载如下两个压缩包:

instantclient-basic-linux.x64-11.2.0.4.0.zip

instantclient-sdk-linux.x64-11.2.0.4.0.zip

oracle_fdw依赖上述两个包,包里面的内容可以在oracle下载页查看,如下

Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications

Instant Client Package - SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client

oracle_fdw官方(https://github.com/laurenz/oracle_fdw#5-installation-requirements)介绍里面有以下对依赖包的描述:

You need to install Oracle's C header files as well (SDK package for Instant Client). If you use the Instant Client ZIP files provided by Oracle, you will have to create a symbolic link from libclntsh.so to the actual shared library file yourself.

2.安装instant oracle client

# mkdir /usr/local/oracle
# unzip -d /usr/local/oracle instantclient-basic-linux.x64-11.2.0.4.0.zip
# unzip -d /usr/local/oracle instantclient-sdk-linux.x64-11.2.0.4.0.zip

3.下载并上传oracle_fdw

下载网址:https://github.com/laurenz/oracle_fdw/releases

4.配置环境变量(主要是动态链接库)

# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/pgsql/lib:/usr/local/oracle/instantclient_11_2
# export PATH=$PATH:/usr/local/pgsql/bin
# export ORACLE_HOME=/usr/local/oracle/instantclient_11_2
# cd /usr/local/oracle/instantclient_11_2
# oracle_fdw找的是libclntsh.so,因此要加一个软连接去掉版本号
# ln -s libclntsh.so.11.1 libclntsh.so
# unzip oracle_fdw-ORACLE_FDW_2_1_0.zip
# cd oracle_fdw-ORACLE_FDW_2_1_0
# make
# make install

注意:这些环境变量是临时的,安装oracle_fdw使用

          在后面pg中创建插件的时候,还是会报找不到动态连接库(虽然我配置了postgres用户的LD_LIBRARY_PATH环境变量),后面通过修改ld.conf解决(详见第五步),因此这里的LD_LIBRARY_PATH环境变量配置可以直接使用后面的ld.conf解决,但为显示整个过程,这里暂时使用临时变量

5.进入数据库安装插件

# su - postgres
# psql
postgres=#  create extension oracle_fdw;

执行会报错:ERROR:  could not load library "/usr/local/pgsql-10.6/lib/oracle_fdw.so": libclntsh.so.11.1: cannot open shared object file: No such file or directory

通过以下步骤解决:

# cd /etc/ld.so.conf.d/
# vi oracle-x86_64.conf
-------------------------------------
/usr/local/oracle/instantclient_11_2
-------------------------------------
# ldconfig

将oracle的库加入动态链接库,再次添加擴展即可

6.創建tnsnames.ora

# cd /usr/local/oracle/instantclient_11_2/
# mkdir -p network/admin/

创建一个或者复制一个tnsnames.ora过来,配置TNS

7.外部表使用

创建外部服务器

create server oracle_test foreign data wrapper oracle_fdw options(dbserver 'db');

oracle_test为外部服务器的名称,db为上一步tnsnames.ora中配置的tns

授权

grant usage on foreign server oracle_test to postgres;

本步不是必须的,如果要给非外部服务器的创建者使用,可以赋权

创建mapping

create user mapping for postgres server oracle_test options (user 'oracle', password 'oracle');

进入oracle数据库的凭证

创建外部表

create foreign table
    test_fdw( id int,
    name varchar(10) ) server oracle_test options (schema 'USER01',table 'TEST01');

schema和table必须为大写,否则无法使用

查看外部服务的信息

select oracle_diag();
select * from test_fdw;
原文地址:https://www.cnblogs.com/monkey6/p/11118377.html