在PostgreSQL中訪問Oracle的具體步驟
在PostgreSQL數(shù)據(jù)庫中,oracle_fdw是PostgreSQL數(shù)據(jù)庫支持的外部擴展。通過使用oracle_fdw擴展可以讀取到Oracle數(shù)據(jù)庫中的數(shù)據(jù)。它是一種非常方便且常見的PostgreSQL與Oracle的同步數(shù)據(jù)的方法。使用oracle_fdw擴展需要依賴Oracle的Instance Client環(huán)境。
下面通過具體的步驟來演示如何使用oracle_fdw擴展。
(1)從Oracle官方網(wǎng)站下載以下3個Oracle Instance Client安裝包,如下圖所示。
instantclient-basic-linuxx64.zip instantclient-sdk-linuxx64.zip instantclient-sqlplus-linuxx64.zip

(2)解壓三個文件包。
unzip instantclient-basic-linuxx64.zip unzip instantclient-sdk-linuxx64.zip unzip instantclient-sqlplus-linuxx64.zip
(3)解壓后會生成instantclient_21_10目錄,將其更名為instantclient
mv instantclient_21_10 instantclient
(4)設(shè)置Oracle環(huán)境變量。
export ORACLE_HOME=/home/postgres/tools/instantclient export OCI_LIB_DIR=$ORACLE_HOME export OCI_INC_DIR=$ORACLE_HOME/sdk/include export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
(5)從GitHub上下載oracle_fwd擴展,并解壓安裝包,如下圖所示。

(6)設(shè)置pg_config的環(huán)境變量,并編譯oracle_fdw擴展。
export PATH=/home/postgres/training/pgsql/bin:$PATH cd oracle_fdw-ORACLE_FDW_2_5_0/ make make install
(7)使用root用戶添加Oracle依賴的庫信息,添加完成后切換回postgres用戶。
su - echo "/home/postgres/tools/instantclient/" >> /etc/ld.so.conf ldconfig su - postgres
(8)啟動PostgreSQL數(shù)據(jù)庫服務(wù)器,并登錄PostgreSQL數(shù)據(jù)庫實例創(chuàng)建oracle_fdw擴展。
postgres=# create extension oracle_fdw;
(9)查看當前PostgreSQL數(shù)據(jù)庫中已安裝的擴展。
postgres=# \dx # 輸出的信息如下: List of installed extensions -[ RECORD 1 ]--------------------------------------------------- Name | file_fdw Version | 1.0 Schema | public Description | foreign-data wrapper for flat file access -[ RECORD 2 ]--------------------------------------------------- Name | oracle_fdw Version | 1.2 Schema | public Description | foreign data wrapper for Oracle access -[ RECORD 3 ]--------------------------------------------------- Name | plpgsql Version | 1.0 Schema | pg_catalog Description | PL/pgSQL procedural language -[ RECORD 4 ]--------------------------------------------------- Name | postgres_fdw Version | 1.0 Schema | public Description | foreign-data wrapper for remote PostgreSQL servers
(10)創(chuàng)建基于oracle_fdw的外部數(shù)據(jù)庫服務(wù)。
postgres=# create server oracle_fdw foreign data wrapper
oracle_fdw options(dbserver '//192.168.79.173:1521/orcl');
# 這里創(chuàng)建的外部數(shù)據(jù)庫服務(wù)名稱叫oracle_fdw,
# 并通過參數(shù)dbserver指定了外部Oracle數(shù)據(jù)庫的地址信息。
(11)查看當前數(shù)據(jù)庫中移創(chuàng)建的外部服務(wù)。
postgres=# \des+ # 輸出的信息如下: List of foreign servers -[ RECORD 1 ]--------+---------------------------------------- Name | foreign_server Owner | postgres Foreign-data wrapper | postgres_fdw Access privileges | Type | Version | FDW options | (host '192.168.79.178', port '5432', dbname 'scott') Description | -[ RECORD 2 ]--------+---------------------------------------- Name | oracle_fdw Owner | postgres Foreign-data wrapper | oracle_fdw Access privileges | Type | Version | FDW options | (dbserver '//192.168.79.173:1521/orcl') Description | -[ RECORD 3 ]--------+----------------------------------------------- Name | service_file Owner | postgres Foreign-data wrapper | file_fdw Access privileges | Type | Version | FDW options | Description |
(12)創(chuàng)建PostgreSQL和Oracle之間的用戶映射。
postgres=# create user mapping for postgres server oracle_fdw
options (user 'c##scott', password 'tiger');
# 該語句為本地postgres用戶創(chuàng)建了一個訪問
# 遠程服務(wù)器oracle_fdw時的用戶映射,
# 也就是使用用戶名c##scott和密碼 tiger連接遠程服務(wù)器。
(13)查看用戶映射信息。
postgres=# \deu+
# 輸出的信息如下:
List of user mappings
-[ RECORD 1 ]------------------------------------------
Server | foreign_server
User name | postgres
FDW options | ("user" 'postgres', password 'Welcome_1')
-[ RECORD 2 ]------------------------------------------
Server | oracle_fdw
User name | postgres
FDW options | ("user" 'c##scott', password 'tiger')
(14)在PostgreSQL數(shù)據(jù)庫中創(chuàng)建外部表訪問Oracle中的數(shù)據(jù)。
postgres=# create foreign table oracle_emp( empno numeric(4,0) options (key 'true') not null, ename varchar(10), job varchar(9) , mgr numeric(4,0), hiredate timestamp, sal numeric(7,2) , comm numeric(7,2), deptno numeric(2,0) )server oracle_fdw options (schema 'C##SCOTT', table 'EMP'); # 注意,這里的'C##SCOTT'和'EMP'需要大寫。
(15)現(xiàn)在可以在本地數(shù)據(jù)庫中通過外部表訪問Oracle數(shù)據(jù)庫中對應(yīng)的遠程表。
postgres=# select * from oracle_emp; # 輸出的信息如下: empno | ename |...| sal | comm | deptno -------+--------+---+---------+---------+-------- 7369 | SMITH |...| 800.00 | | 20 7499 | ALLEN |...| 1600.00 | 300.00 | 30 7521 | WARD |...| 1250.00 | 500.00 | 30 7566 | JONES |...| 2975.00 | | 20 7654 | MARTIN |...| 1250.00 | 1400.00 | 30 7698 | BLAKE |...| 2850.00 | | 30 7782 | CLARK |...| 2450.00 | | 10 7788 | SCOTT |...| 3000.00 | | 20 7839 | KING |...| 5000.00 | | 10 7844 | TURNER |...| 1500.00 | 0.00 | 30 7876 | ADAMS |...| 1100.00 | | 20 7900 | JAMES |...| 950.00 | | 30 7902 | FORD |...| 3000.00 | | 20 7934 | MILLER |...| 1300.00 | | 10 (14 rows)
到此這篇關(guān)于在PostgreSQL中訪問Oracle的具體步驟的文章就介紹到這了,更多相關(guān)PostgreSQL訪問Oracle內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作
這篇文章主要介紹了Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
PGSQL實現(xiàn)判斷一個空值字段,并將NULL值修改為其它值
這篇文章主要介紹了PGSQL實現(xiàn)判斷一個空值字段,并將NULL值修改為其它值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL如何查詢表結(jié)構(gòu)和索引信息
文章介紹了在PostgreSQL中查詢表結(jié)構(gòu)和索引信息的幾種方法,包括使用`\d`元命令、系統(tǒng)數(shù)據(jù)字典查詢以及使用可視化工具DBeaver2024-12-12
解決sqoop從postgresql拉數(shù)據(jù),報錯TCP/IP連接的問題
這篇文章主要介紹了解決sqoop從postgresql拉數(shù)據(jù),報錯TCP/IP連接的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
利用OGG實現(xiàn)PostgreSQL實時同步的過程詳解
本文詳細闡述了利用OGG實現(xiàn)PostgreSQL實時同步的全過程,文章通過代碼示例和圖文結(jié)合講解的非常詳細,對大家的學(xué)習或工作有一定的參考價值,需要的朋友可以參考下2023-11-11
PostgreSQL 實現(xiàn)定時job執(zhí)行(pgAgent)
這篇文章主要介紹了PostgreSQL 實現(xiàn)定時job執(zhí)行(pgAgent),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
PostgreSQL因大量并發(fā)插入導(dǎo)致的主鍵沖突的解決方案
在數(shù)據(jù)庫操作中,并發(fā)插入是一個常見的場景,然而,當大量并發(fā)插入操作同時進行時,可能會遇到主鍵沖突的問題,本文將深入探討 PostgreSQL 中解決因大量并發(fā)插入導(dǎo)致的主鍵沖突的方法,并通過具體的示例進行詳細說明,需要的朋友可以參考下2024-07-07

