oracle_fdw官方安裝包下載、安裝指導、使用說明、常見問題地址:http://pgxn.org/dist/oracle_fdw/ .在全部操做開始以前,請首先閱讀該文檔。html
要求PostgreSQL是 9.1 以及更高版本。從 9.2開始支持ANALYZE;從9.3開始支持INSERT、UODATE 和 DELETE。mysql
要求Oracle客戶端是10.1以及更高版本。基於Oracle Instant Client ,或者 帶有Universal Installer的Oracle Client和Server的安裝,Oracle_fdw能夠進行建立和使用。經過Oracle Client 10 編譯的二進制文件,能夠被更高版本的客戶端使用,而不須要recompilation 或者 relink。linux
環境:sql
一個windows xp(32bit)虛擬機,裝有oracle11g。本文假設該oracle服務器安裝完畢,可遠程訪問,ip爲192.168.100.234。數據庫
一個centos(32bit)linux虛擬機,用來安裝postgresql,oracle客戶端,oracle_fdw。(本文全部操做在此進行)windows
下載PostgreSQL源碼安裝包,本文使用的:centos
postgresql-9.4.4.tar.gz服務器
編譯安裝:oracle
注意:編譯時使用--without-ldapapp
/configure --prefix=/opt/pgsql --with-pgport=5432--with-segsize=8 --with-wal-segsize=64 --with-wal-blocksize=64 --with-perl --without-openssl --without-pam --without-ldap --enable-thread-safety gmake world gmake install -world cd /opt/pgsql mkdir data useradd -m postgres passwd postgres chown postgres data su - postgres cd .. cd bin ./initdb -D ../data --locale=C -U postgres ./pg_ctl start -D ../data ./psql
添加環境變量:
vi /etc/profile
在文件末尾添加
export PG_HOME=/opt/pgsql Esc :w :q
source /etc/profile
官方介紹和安裝指導:http://www.oracle.com/technetwork/database/features/instant-client/index-100365.html
官方下載地址:http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html
安裝過程:
1. 下載:basic/sdk/sqlplus三個安裝包, 必定要與操做系統和位數(32bit或64bit)符合,本文使用的是:
instantclient-basic-linux-12.1.0.2.0 .zip
instantclient-sdk-linux-12.1.0.2.0.zip
instantclient-sqlplus-linux-12.1.0.2.0.zip
2. 建立一個oracle客戶端的目錄/opt/oracle,
3.將三個壓縮包解壓後,將全部文件直接拷貝到/opt/oracle/下面.
4.在/opt/oracle/下面建立配置文件tnsname.ora
cd /opt/oracle vi tnsname.ora
添加內容:
MYDB = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.234)(PORT=1521)) ) (CONNECT_DATA=(SID=orcl)(SERVER = DEDICATED)) )
5.配置關於oracle客戶端的環境變量
vi /etc/profile
添加如下內容
export ORACLE_HOME=/opt/oracle; export SQLPATH=/opt/oracle; export TNS_ADMIN=/opt/oracle; export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH; export PATH=$PATH:$ORACLE_HOME;
source /opt/profile
6. 建立oracle用戶,並對客戶端目錄受權
groupadd oinstall useradd - g oinstall oracle passwd oracle
chown -R oracle:oinstall /opt/oracle chmod -R 775 /usr/oracle
7. 使用客戶端進行鏈接測試
su - oracle cd /opt/oracle [oracle@localhost oracle]$ sqlplus lyy/lyy@//192.168.100.234:1521/orcl SQL*Plus: Release 12.1.0.2.0 Production on Sun Sep 6 19:32:12 2015 Copyright (c) 1982, 2014, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL>
此時oracle客戶端配置完畢並鏈接成功。
常見問題:參考 http://my.oschina.net/liuyuanyuangogo/blog/502090 來解決。
官方源碼地址:http://pgfoundry.org/frs/?group_id=1000600
官方安裝包下載、安裝指導、使用說明、常見問題地址:http://pgxn.org/dist/oracle_fdw/
1.下載源碼並閱讀安裝說明.
官方下載和使用說明地址:http://pgxn.org/dist/oracle_fdw/
本文下載的是:oracle_fdw-1.2.0.zip
要確保與postgresql安裝的操做系統及其位數(32bit或64bit)匹配。
2. 解壓壓縮包並配置Makefile
查找pg_config位置
[root@oracle_fdw-1.2.0]# find / -name pg_config/opt/PostgresPlus/9.3AS/bin/pg_config
更改oracle_fdw-0.9.9文件夾裏的Makefile文件,指定pg_config位置
[root@oracle_fdw-1.2.0]# cat Makefile | grep PG_CONFIG
改成PG_CONFIG=$PG_HOME/bin/pg_config(更改文件的路徑)
3.編譯並安裝(必須是root用戶下)
直接在oracle_fdw-1.2.0目錄下執行:
[root@localhost oracle_fdw-1.2.0]# make
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -O2 -fpic -shared -o oracle_fdw.so oracle_fdw.o oracle_utils.o oracle_gis.o -L/opt/pgsql/lib -Wl,-rpath,'/opt/pgsql/lib',--enable-new-dtags -L/opt/oracle -L/opt/oracle/bin -L/opt/oracle/lib -lclntsh -L/usr/lib/oracle/12.1/client/lib -L/usr/lib/oracle/12.1/client64/lib -L/usr/lib/oracle/11.2/client/lib -L/usr/lib/oracle/11.2/client64/lib -L/usr/lib/oracle/11.1/client/lib -L/usr/lib/oracle/11.1/client64/lib -L/usr/lib/oracle/10.2.0.5/client/lib -L/usr/lib/oracle/10.2.0.5/client64/lib -L/usr/lib/oracle/10.2.0.4/client/lib -L/usr/lib/oracle/10.2.0.4/client64/lib -L/usr/lib/oracle/10.2.0.3/client/lib -L/usr/lib/oracle/10.2.0.3/client64/lib
[root@localhost oracle_fdw-1.2.0]# ldd oracle_fdw.so
linux-gate.so.1 => (0x007e6000) libclntsh.so.12.1 => /opt/oracle/libclntsh.so.12.1 (0x007e7000) libc.so.6 => /lib/libc.so.6 (0x00248000) libnnz12.so => /opt/oracle/libnnz12.so (0x04873000) libons.so => /opt/oracle/libons.so (0x00110000) libdl.so.2 => /lib/libdl.so.2 (0x00144000) libm.so.6 => /lib/libm.so.6 (0x005e4000) libpthread.so.0 => /lib/libpthread.so.0 (0x00671000) libnsl.so.1 => /lib/libnsl.so.1 (0x00149000) librt.so.1 => /lib/librt.so.1 (0x00162000) /lib/ld-linux.so.2 (0x0022b000) libaio.so.1 => /usr/lib/libaio.so.1 (0x0016b000) libclntshcore.so.12.1 => /opt/oracle/libclntshcore.so.12.1 (0x02b7c000)
[root@localhost oracle_fdw-1.2.0]# make install
/bin/mkdir -p '/opt/pgsql/lib' /bin/mkdir -p '/opt/pgsql/share/extension' /bin/mkdir -p '/opt/pgsql/share/extension' /bin/mkdir -p '/opt/pgsql/share/doc/extension' /usr/bin/install -c -m 755 oracle_fdw.so '/opt/pgsql/lib/oracle_fdw.so' /usr/bin/install -c -m 644 oracle_fdw.control '/opt/pgsql/share/extension/' /usr/bin/install -c -m 644 oracle_fdw--1.1.sql oracle_fdw--1.0--1.1.sql '/opt/pgsql/share/extension/' /usr/bin/install -c -m 644 README.oracle_fdw '/opt/pgsql/share/doc/extension/'
出現以上信息說明orale_fdw編譯安裝完成。
常見錯誤:
在make時遇到錯誤cannot find -lclntsh:
/usr/bin/ld: cannot find -lclntsh collect2: ld returned 1 exit status make: *** [oracle_fdw.so] Error 1
解決辦法:到oracle客戶端的目錄下,爲libclntsh.so建立指向libclntsh.so.12.1的軟連接。
[postgres@localhost bin]$ su - root Password: [root@localhost ~]# cd /opt/oracle/ --建立軟鏈接 [root@localhost oracle]# ln -s libclntsh.so.12.1 libclntsh.so --查看指向狀況 [root@localhost oracle]# ll total 175420 -rwxrwxr-x 1 oracle oinstall 24706 Jul 8 2014 adrci -rwxrwxr-x 1 oracle oinstall 438 Jul 8 2014 BASIC_README -rwxrwxr-x 1 oracle oinstall 33309 Jul 8 2014 genezi -rwxrwxr-x 1 oracle oinstall 342 Jul 8 2014 glogin.sql -rwxrwxr-x 1 oracle oinstall 5520733 Jul 8 2014 libclntshcore.so.12.1 lrwxrwxrwx 1 root root 17 Sep 6 23:02 libclntsh.so -> libclntsh.so.12.1 -rwxrwxr-x 1 oracle oinstall 45817130 Jul 8 2014 libclntsh.so.12.1 -rwxrwxr-x 1 oracle oinstall 5323903 Jul 8 2014 libnnz12.so -rwxrwxr-x 1 oracle oinstall 1958194 Jul 8 2014 libocci.so.12.1 -rwxrwxr-x 1 oracle oinstall 109543276 Jul 8 2014 libociei.so -rwxrwxr-x 1 oracle oinstall 183705 Jul 8 2014 libocijdbc12.so -rwxrwxr-x 1 oracle oinstall 268133 Jul 8 2014 libons.so -rwxrwxr-x 1 oracle oinstall 81153 Jul 8 2014 liboramysql12.so -rwxrwxr-x 1 oracle oinstall 1561437 Jul 8 2014 libsqlplusic.so -rwxrwxr-x 1 oracle oinstall 1299573 Jul 8 2014 libsqlplus.so drwxrwxr-x 3 oracle oinstall 4096 Sep 6 20:16 oci -rwxrwxr-x 1 oracle oinstall 3692096 Jul 8 2014 ojdbc6.jar -rwxrwxr-x 1 oracle oinstall 3698857 Jul 8 2014 ojdbc7.jar drwxrwxr-x 5 oracle oinstall 4096 Jul 8 2014 sdk -rwxrwxr-x 1 oracle oinstall 7353 Jul 8 2014 sqlplus -rwxrwxr-x 1 oracle oinstall 442 Jul 8 2014 SQLPLUS_README -rwxrwxr-x 1 oracle oinstall 172720 Jul 8 2014 uidrvci -rwxrwxr-x 1 oracle oinstall 71202 Jul 8 2014 xstreams.jar --查看libclntsh.so.12.1所調用的庫 [root@localhost oracle_fdw-1.2.0]# ldd /opt/oracle/libclntsh.so.12.1 linux-gate.so.1 => (0x00a30000) libnnz12.so => /opt/oracle/libnnz12.so (0x00248000) libons.so => /opt/oracle/libons.so (0x00110000) libdl.so.2 => /lib/libdl.so.2 (0x00144000) libm.so.6 => /lib/libm.so.6 (0x00149000) libpthread.so.0 => /lib/libpthread.so.0 (0x00172000) libnsl.so.1 => /lib/libnsl.so.1 (0x0018c000) librt.so.1 => /lib/librt.so.1 (0x00937000) libc.so.6 => /lib/libc.so.6 (0x0069f000) /lib/ld-linux.so.2 (0x0022b000) libaio.so.1 => /usr/lib/libaio.so.1 (0x001a5000) libclntshcore.so.12.1 => /opt/oracle/libclntshcore.so.12.1 (0x083b1000) --操做完畢便可從新執行make和make install
[root@localhost oracle_fdw-1.2.0]# cd /opt/oracle/ [root@localhost oracle]# cp libclntsh.so.12.1 /opt/pgsql/lib/ [root@localhost oracle]# cp libnnz12.so /opt/pgsql/lib/ [root@localhost oracle]# chown .daemon /opt/pgsql/lib/libclntsh.so.12.1 [root@localhost oracle]# chown .daemon /opt/pgsql/lib/libnnz12.so
[postgres@localhost bin]$ ./psql psql (9.4.4) Type "help" for help. postgres=# create extension oracle_fdw; CREATE EXTENSION postgres=# create server oracledb foreign data wrapper oracle_fdw options(dbserver '//192.168.100.234:1521/orcl'); CREATE SERVER postgres=# create user oracle_fdw superuser password 'oracle'; CREATE ROLE postgres=# create user mapping for oracle_fdw server oracledb options (user 'lyy',password 'lyy'); CREATE USER MAPPING postgres=# create foreign table oracle_lyy(id int, name varchar) server oracledb options(schema 'lyy', table 'LYY');--注意oracle中表名通常爲大寫 CREATE FOREIGN TABLE postgres=# \q [postgres@localhost bin]$ ./psql -U oracle_fdw -d postgres psql (9.4.4) Type "help" for help. postgres=# select * from oracle_lyy; id | name ----+---------------------- 1 | ddd (1 row)
此後,在oracle中對lyy表的操做提交後,PostgreSQL中oracle_lyy也會更新。在PostgreSQL中對oracle_lyy表的操做提交後,Oracle中lyy也會更新。
詳細請參閱官方問題列表: problem http://pgxn.org/dist/oracle_fdw/
錯誤舉例:
Encoding -------- Characters stored in an Oracle database that cannot be converted to the PostgreSQL database encoding will silently be replaced by "replacement characters", typically a normal or inverted question mark, by Oracle.You will get no warning or error messages. If you use a PostgreSQL database encoding that Oracle does not know (currently, these are EUC_CN, EUC_KR, LATIN10, MULE_INTERNAL, WIN874 and SQL_ASCII), non-ASCII characters cannot be translated correctly. You will get a warning in this case, and the characters will be replaced by replacement characters as described above. You can set the "nls_lang" option of the foreign data wrapper to force a certain Oracle encoding, but the resulting characters will most likely be incorrect and lead to PostgreSQL error messages. This is probably only useful for SQL_ASCII encoding if you know what you are doing. See "Options" above.
PotgreSQL初始化時指定--locale-C(該編碼oracle不識別),以後使用oracle_fdw會出現,以下錯誤:
postgres=# select * from oracle_lyy; WARNING: no Oracle character set for database encoding "SQL_ASCII" DETAIL: All but ASCII characters will be lost. HINT: You can set the option "nls_lang" on the foreign data wrapper to force an Oracle character set. WARNING: no Oracle character set for database encoding "SQL_ASCII"
只能從新初始化一個能夠支持的編碼的庫,好比--locale=en_US.utf8.
參考文章:
http://francs3.blog.163.com/blog/static/4057672720122354546425/
官方文檔:http://www.postgresql.org/docs/9.4/static/postgres-fdw.html
postgres_fdw是PostgreSQL 9.3新增的功能模塊。
postgres_fdw用來訪問外部的PostgreSQL服務器。
舉例列示步驟:
經過配置pg_hba.conf和postgreql.conf兩個配置文件,配置完畢後需重啓服務。
postgres=# create extension postgres_fdw; CREATE EXTENSION postgres=# create server foreign_server foreign data wrapper postgres_fdw options (host '192.168.100.232',port '5432', dbname 'postgres'); CREATE SERVER postgres=# create user postgres_fdw superuser password 'postgres'; CREATE ROLE postgres=# create user mapping for postgres_fdw server foreign_server options (user 'postgres', password 'postgres'); CREATE USER MAPPING postgres=# create foreign table lyy_fdw(id int, name varchar)server foreign_server options (schema_name 'public',table_name 'lyy');--確保首先在oracle中建立表LYY。 CREATE FOREIGN TABLE --此時外部數據表建立完畢,進行查詢(已可得到外部服務器中的public.lyy表的數據): postgres=# select * from postgres.lyy_fdw; id | name ----+------ 1 | lily 2 | lucy 11 | hhhh (3 rows)
此後每當外部數據庫服務器中相應的表數據變化,本地服務器中的相應外部數據表數據也變化。
官方文檔:http://www.postgresql.org/docs/9.4/static/file-fdw.html
file_fdw是PostgreSQL 9.1 新增的功能模塊。
file_fdw能夠用來訪問PostgreSQL服務器系統中的數據文件。數據文件的格式必須是COPY FROM能夠讀取的;請參考COPY 。對這些數據文件的訪問,目前僅是隻讀的。
舉例列示步驟:
轉至《經過file_fdw讀取PostgreSQL日誌文件》:http://my.oschina.net/liuyuanyuangogo/blog/370692
轉至《file_fdw建立外部表及其與普通表的結合》:http://my.oschina.net/liuyuanyuangogo/blog/369717