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;