[Oracle - PostgreSQL to Oracle DBLink] use. FDW

Oracle FDW란.

Postgresql 에서 Oracle에 있는 데이터를 가져오기 위해서는 FDW를 이용할 수 있다. FDW는 Foreign Data Wrapper 의 약자로 이기종 데이터베이스의 외래테이블 간에 연결하여 사용할 수 있는 솔루션이다.

 

 

테스트 환경 
postgresql : 13.7
Oracle : 11.2.0.4
 

 

※ oracle 부분 확인 필요 사항
오라클 클라이언트 다운로드가 필요(zip 파일) - 오라클 계정 필요
오라클 서버와 버전이 맞는 버전을 사용하는 것이 좋음(이슈 관련)
32비트 오라클 서버와 64비트 PostgreSQL 서버는 동작 불가.(동일한 bit의 프로그램 필요)
#SELECT * FROM v$version; 로 oracle 버전 정보 확인
 
 
 
----------Oracle client version 11.2 or better is required.---------- oracle_fdw 2.3.0버전 기준
그 외 호환 버전 정보는 Matrix in support document 207303.1 문서 참조
호환 Matrix에서는 최신 버전이 아닌 oracle 버전의 호환성에 대해 확인해주지 않는다는 점은 감안하여야 한다.
oracle_fdw 2.2.0 버전 기준 oracle client 10.2버전은 지원하지 않는다.
 
 
 
--SID값으로 Service_name값 확인하기, 확인한 Service_name값으로 foreign server 설정값에 넣어주면 된다.
select value from v$parameter where name='<sid값>';
-------------------------
ORCLCDB.localdomain

 

 

■ oracle fdw 파일 설치 (pg) 
가장 최신 버전인 2.5로 설치 진행. oracle client와 호환 버전 체킹 필요

 

 

■ oracle basic, sdk 설치 (pg)

oracle client 버전 및 필요 패키지

[root@jh-post ~]# ls -lrt
-rw-r--r-- 1 root root   129108 Mar 20 09:50 oracle_fdw-ORACLE_FDW_2_5_0.tar.gz
-rw-r--r-- 1 root root   643089 Mar 20 09:50 instantclient-sdk-linux.x64-11.2.0.4.0.zip
-rw-r--r-- 1 root root 60704657 Mar 20 09:50 instantclient-basic-linux.x64-11.2.0.4.0.zip

 

 

 

■ 클라이언트 및 sdk 설치

[root@jh-post ~]# unzip instantclient-basic-linux.x64-11.2.0.4.0.zip

[root@jh-post ~]# unzip instantclient-sdk-linux.x64-11.2.0.4.0.zip



## 이후 fdw설치시 에러 발생 방지를 위해 심볼릭링크 생성
[root@jh-post ~]# cd instantclient_11_2/

[root@jh-post instantclient_11_2]# ln -s libclntsh.so.11.1 libclntsh.so



## 환경설정 추가
[root@jh-post ~]# su - postgres

[postgres@jh-post ~]$ vi ~/.bash_profile
POSTGRES_HOME=/mnt/pgsql/13
LD_LIBRARY_PATH=/mnt/pgsql/13/lib:/home/postgres/instantclient_11_2
#LD_LIBRARY_PATH=/home/postgres/instantclient_11_2
export ORACLE_HOME=/home/postgres/instantclient_11_2
PGDATA=$POSTGRES_HOME/data
MANPATH=$MANPATH:$POSTGRES_HOME/man

PATH=$POSTGRES_HOME/bin:$PATH:$ORACLE_HOME

export PATH
export POSTGRES_HOME
export PGDATA
export MANPATH
export LD_LIBRARY_PATH

 

 

■ Oracle fdw 설치

[root@jh-post ~]# tar -zxvf ORACLE_FDW_2_5_0.tar.gz


[root@jh-post ~]# cd oracle_fdw-ORACLE_FDW_2_5_0

[postgres@jh-post oracle_fdw-ORACLE_FDW_2_5_0]$ make
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I"/home/postgres/instantclient_11_2/sdk/include" -I"/home/postgres/instantclient_11_2/oci/include" -I"/home/postgres/instantclient_11_2/rdbms/public" -I"/home/postgres/instantclient_11_2/"  -I. -I./ -I/mnt/pgsql/13/include/server -I/mnt/pgsql/13/include/internal  -D_GNU_SOURCE   -c -o oracle_fdw.o oracle_fdw.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I"/home/postgres/instantclient_11_2/sdk/include" -I"/home/postgres/instantclient_11_2/oci/include" -I"/home/postgres/instantclient_11_2/rdbms/public" -I"/home/postgres/instantclient_11_2/"  -I. -I./ -I/mnt/pgsql/13/include/server -I/mnt/pgsql/13/include/internal  -D_GNU_SOURCE   -c -o oracle_utils.o oracle_utils.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -I"/home/postgres/instantclient_11_2/sdk/include" -I"/home/postgres/instantclient_11_2/oci/include" -I"/home/postgres/instantclient_11_2/rdbms/public" -I"/home/postgres/instantclient_11_2/"  -I. -I./ -I/mnt/pgsql/13/include/server -I/mnt/pgsql/13/include/internal  -D_GNU_SOURCE   -c -o oracle_gis.o oracle_gis.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC -shared -o oracle_fdw.so oracle_fdw.o oracle_utils.o oracle_gis.o -L/mnt/pgsql/13/lib    -Wl,--as-needed -Wl,-rpath,'/mnt/pgsql/13/lib',--enable-new-dtags  -L"/home/postgres/instantclient_11_2/" -L"/home/postgres/instantclient_11_2/bin" -L"/home/postgres/instantclient_11_2/lib" -L"/home/postgres/instantclient_11_2/lib/amd64"  -lclntsh




[postgres@jh-post oracle_fdw-ORACLE_FDW_2_5_0]$ make install
/usr/bin/mkdir -p '/mnt/pgsql/13/lib'
/usr/bin/mkdir -p '/mnt/pgsql/13/share/extension'
/usr/bin/mkdir -p '/mnt/pgsql/13/share/extension'
/usr/bin/mkdir -p '/mnt/pgsql/13/share/doc/extension'
/usr/bin/install -c -m 755  oracle_fdw.so '/mnt/pgsql/13/lib/oracle_fdw.so'
/usr/bin/install -c -m 644 .//oracle_fdw.control '/mnt/pgsql/13/share/extension/'
/usr/bin/install -c -m 644 .//oracle_fdw--1.2.sql .//oracle_fdw--1.0--1.1.sql .//oracle_fdw--1.1--1.2.sql  '/mnt/pgsql/13/share/extension/'
/usr/bin/install -c -m 644 .//README.oracle_fdw '/mnt/pgsql/13/share/doc/extension/'

 

 

■ Extension 생성

postgres=# create extension oracle_fdw;
CREATE EXTENSION



postgres=# select * from pg_catalog.pg_available_extensions;
    name    | default_version | installed_version |                comment                 
------------+-----------------+-------------------+----------------------------------------
plpgsql    | 1.0             | 1.0               | PL/pgSQL procedural language
oracle_fdw | 1.2             | 1.2               | foreign data wrapper for Oracle access
(2 rows)

 

 

■ Oracle client 접속 확인
## Oracle
[oracle@jh-ora admin]$ vi listener.ora

# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = jh-ora)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle






[oracle@jh-ora admin]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 20-MAR-2023 12:58:26

Copyright (c) 1991, 2013, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jh-ora)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                20-MAR-2023 12:57:40
Uptime                    0 days 0 hr. 0 min. 45 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/jh-ora/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=jh-ora)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "orcl" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB" has 1 instance(s).
  Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully



## tnsname
ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.83)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )





## PG 서버에서 접속시도
[postgres@jh-post ~]$ sqlplus kim/kim@192.168.100.84:1521/orcl

SQL*Plus: Release 11.2.0.4.0 Production on Mon Mar 20 12:58:05 2023

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

 

 

■ pg_hba.conf 수정(PG)

[postgres@jh-post data]$ vi pg_hba.conf
host    all     all     192.168.100.0/24        trust

 

 

■ Oracle서버에 테스트 데이터 입력(oracle)

SQL> create table test (id number, name varchar2(30), addr varchar2(50));

Table created.


SQL> begin
for i in 1..1000 loop
insert into test values(i||i,'kim'||i,'addr');
end loop;
end;
/

PL/SQL procedure successfully completed.



##
SQL> select * from kim.test where rownum < = 10;

    ID NAME               ADDR
---------- ------------------------------ --------------------------------------------------
    11 kim1               addr
    22 kim2               addr
    33 kim3               addr
    44 kim4               addr
    55 kim5               addr
    66 kim6               addr
    77 kim7               addr
    88 kim8               addr
    99 kim9               addr
      1010 kim10              addr

10 rows selected.

 

 

 

■ Oracle 데이터베이스 연결서버 생성(PG)

postgres=# create server ora11g foreign data wrapper oracle_fdw options(dbserver '//192.168.100.84:1521/orcl');
CREATE SERVER



## dblink 유저 생성
postgres=# create user kim password 'kim' login;
CREATE ROLE

postgres=# grant usage on foreign server ora11g to kim;
GRANT



## oracle 스키마와 매핑
postgres=# create user mapping for kim server ora11g options(user 'kim',password 'kim');
CREATE USER MAPPING




## 데이터베이스 연결 할 유저로 재접속 후 외래 테이블 생성
[postgres@jh-post data]$ psql -U kim

create foreign table fdw_table(
i numeric,
name text,
addr text
)server ora11g options(schema 'KIM', table 'TEST');
CREATE FOREIGN TABLE




## 외래 테이블 조회
postgres=> select * from fdw_table;
    i     |  name   | addr
----------+---------+------
       11 | kim1    | addr
       22 | kim2    | addr
       33 | kim3    | addr
       44 | kim4    | addr
       55 | kim5    | addr
       66 | kim6    | addr
       77 | kim7    | addr
       88 | kim8    | addr
       99 | kim9    | addr
     1010 | kim10   | addr
     1111 | kim11   | addr
     1212 | kim12   | addr
     1313 | kim13   | addr
     1414 | kim14   | addr
     1515 | kim15   | addr
     1616 | kim16   | addr
     1717 | kim17   | addr
     1818 | kim18   | addr
     1919 | kim19   | addr
     2020 | kim20   | addr
     2121 | kim21   | addr
     2222 | kim22   | addr
     2323 | kim23   | addr
     2424 | kim24   | addr
     2525 | kim25   | addr
     2626 | kim26   | addr
...

 

 

■ 외래 테이블 조회

select srvname as name,
srvowner::regrole as owner,
fdwname as wrapper,
srvoptions as options
from pg_foreign_server
join pg_foreign_data_wrapper w
on w.oid = srvfdw;

  name  |  owner   |  wrapper   |                options                
--------+----------+------------+---------------------------------------
ora11g | postgres | oracle_fdw | {dbserver=//192.168.100.84:1521/orcl}

 

 

참고

https://positivemh.tistory.com/478

 

PostgreSQL 10, oracle fdw extension 으로 오라클 DB와 연결(dblink) 정리

OS환경 : Oracle Linux 7.6 (64bit) DB 환경 : PostgreSQL 10, Oracle Database 11.2.0.4 방법 : PostgreSQL 10, oracle fdw extension 으로 오라클 DB와 연결(dblink)postgresql에서 oracle database 로 연결하는 방법을 설명함postgresql이 설

positivemh.tistory.com