Hi Guys,
This post is in continuation of my last post where I showed how to connect to Oracle database from an PostgreSQL instance. Last post was all about accessing Oracle database using Foreign data wrappers. This post is about accessing/querying Oracle database from PostgreSQL using DB Links.
Software Used:
Oracle Instant Clients (Need both BASIC and DEVEL packages)
Link: https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
For this method too, you have to install Oracle instant basic and devel packages.
[enterprisedb@fatdba ~]$ sudo rpm -Uvh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
[sudo] password for enterprisedb:
Preparing... ########################################### [100%]
1:oracle-instantclient12.########################################### [100%]
[enterprisedb@fatdba ~]$
[enterprisedb@fatdba ~]$ sudo rpm -Uvh oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
Preparing... ########################################### [100%]
1:oracle-instantclient12.########################################### [100%]
[enterprisedb@fatdba ~]$
[enterprisedb@fatdba ~]$
[enterprisedb@fatdba ~]$ ls -ll /usr/lib/oracle/12.2/client64/lib
total 216568
lrwxrwxrwx. 1 root root 21 Aug 27 22:59 libclntshcore.so -> libclntshcore.so.12.1
-rw-rwxr--. 1 root root 8033199 Jan 26 2017 libclntshcore.so.12.1
lrwxrwxrwx. 1 root root 17 Aug 27 22:59 libclntsh.so -> libclntsh.so.12.1
-rw-rwxr--. 1 root root 71638263 Jan 26 2017 libclntsh.so.12.1
-rw-rwxr--. 1 root root 2981501 Jan 26 2017 libipc1.so
-rw-rwxr--. 1 root root 539065 Jan 26 2017 libmql1.so
-rw-rwxr--. 1 root root 6568149 Jan 26 2017 libnnz12.so
lrwxrwxrwx. 1 root root 15 Aug 27 22:59 libocci.so -> libocci.so.12.1
-rw-rwxr--. 1 root root 2218687 Jan 26 2017 libocci.so.12.1
-rw-rwxr--. 1 root root 124771800 Jan 26 2017 libociei.so
-rw-rwxr--. 1 root root 158543 Jan 26 2017 libocijdbc12.so
-rw-rwxr--. 1 root root 380996 Jan 26 2017 libons.so
-rw-rwxr--. 1 root root 116563 Jan 26 2017 liboramysql12.so
-rw-r--r--. 1 root root 3984814 Jan 26 2017 ojdbc8.jar
-rw-rwxr--. 1 root root 312974 Jan 26 2017 ottclasses.zip
-rw-r--r--. 1 root root 37494 Jan 26 2017 xstreams.jar
[enterprisedb@fatdba ~]$
Cool, let’s connect with the PostgreSQL instance and create the DBLink. To create that you need Oracle username and its password which you want to connect and its IP address along with SID or database name, and you are done. And yes, don’t forget to set the LD_LIBRARY_PATH to the location of your Oracle instant client.
[enterprisedb@fatdba ~]$ export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib
[enterprisedb@fatdba ~]$
[enterprisedb@fatdba ~]$ psql -d enterprisedb -U enterprisedb
psql.bin (10.12.20)
Type "help" for help.
enterprisedb=#
enterprisedb=#
enterprisedb=# select version();
version
---------------------------------------------------------------------------------------------------------------
EnterpriseDB 10.12.20 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit
(1 row)
enterprisedb=#
enterprisedb=# CREATE DATABASE LINK dixdroid CONNECT TO migr IDENTIFIED BY 'oracle90' USING oci '//10.0.0.130/fatdb';
CREATE DATABASE LINK
enterprisedb=#
Great, now time to test. Lets do a query on Oracle’s table from Postgresql instance using DB Link named ‘dixdroid’ which we have created earlier.
enterprisedb=# select * from migr.bigtab1@dixdroid;
id | created_date | lookup_id | data
------+--------------------+-----------+----------------------------
320 | 19-MAY-19 02:10:38 | 1 | This is some data for 320
321 | 19-MAY-18 02:10:38 | 2 | This is some data for 321
322 | 19-MAY-19 02:10:38 | 1 | This is some data for 322
323 | 19-MAY-20 02:10:38 | 3 | This is some data for 323
324 | 19-MAY-18 02:10:38 | 2 | This is some data for 324
325 | 19-MAY-20 02:10:38 | 3 | This is some data for 325
326 | 19-MAY-19 02:10:38 | 1 | This is some data for 326
327 | 19-MAY-18 02:10:38 | 2 | This is some data for 327
328 | 19-MAY-19 02:10:38 | 1 | This is some data for 328
329 | 19-MAY-20 02:10:38 | 3 | This is some data for 329
330 | 19-MAY-18 02:10:38 | 2 | This is some data for 330
331 | 19-MAY-20 02:10:38 | 3 | This is some data for 331
Great, it worked like a charm!
Hope It Helps
Prashant Dixit