在PostgreSQL数据库中,oracle_fdw是PostgreSQL数据库支持的外部扩展。通过使用oracle_fdw扩展可以读取到Oracle数据库中的数据。它是一种非常方便且常见的PostgreSQL与Oracle的同步数据的方法。使用oracle_fdw扩展需要依赖Oracle的Instance Client环境。视频讲解如下:
https://www.bilibili.com/video/BV1QV1LYEE24/?aid=113384821365...
下面通过具体的步骤来演示如何使用oracle_fdw扩展。
(1)从Oracle官方网站下载以下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)设置Oracle环境变量。
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)设置pg_config的环境变量,并编译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数据库服务器,并登录PostgreSQL数据库实例创建oracle_fdw扩展。
postgres=# create extension oracle_fdw;
(9)查看当前PostgreSQL数据库中已安装的扩展。
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)创建基于oracle_fdw的外部数据库服务。
postgres=# create server oracle_fdw foreign data wrapper
oracle_fdw options(dbserver '//192.168.79.173:1521/orcl');
# 这里创建的外部数据库服务名称叫oracle_fdw,
# 并通过参数dbserver指定了外部Oracle数据库的地址信息。
(11)查看当前数据库中移创建的外部服务。
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)创建PostgreSQL和Oracle之间的用户映射。
postgres=# create user mapping for postgres server oracle_fdw
options (user 'c##scott', password 'tiger');
# 该语句为本地postgres用户创建了一个访问
# 远程服务器oracle_fdw时的用户映射,
# 也就是使用用户名c##scott和密码 tiger连接远程服务器。
(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数据库中创建外部表访问Oracle中的数据。
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)现在可以在本地数据库中通过外部表访问Oracle数据库中对应的远程表。
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)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。