How to create Dblink between Oracle & PostgreSQL


One of our databases was migrated from Oracle to Postgresql, mainly for a costs issue. Weeks later the database was migrated we need to make a report joinning data from one of our Oracle databases and this postgresql. We found a few ways to make it, but we finally decided to use Oracle Database Gateway.

For keep the same version of the Oracle database I decided to use 12c version, but you can use the 18 or 19 version if you want.

Download the software

ou can install Oracle Database Gateway in either of the following configurations:

  • On the same computer as an existing Oracle database but in a different Oracle home.
  • On a system with no Oracle database.
  • On the same computer as the Oracle database, and in the same Oracle home directory. Note that in this case, the Oracle database and the gateway must be at the same release level.

Now we are going to install in the same server where the database is but in a different Oracle Home.

ORACLE_BASE = /u01/app/oracle
ORACLE_HOME = /u01/app/oracle/product/gateway/12.2.0/dbhome_1

Uncompress the software and execute the installer. The installer is the same that you already know for another Oracle products, so it’s really easy to install. There are no special questions to answer in it. I only have to say, the installer will create a new listener… I won’t use this listener and we’ll remove it later.

For the driver installation we’ll use the driver packed with redhat.

yum install postgresql-odbc unixODBC unixODBC-devel

Verify that the PostgreSQL driver has installed correctly

[root@lab29 ~]# odbcinst -q -d
[root@labo1234 ~]#

Check the current the odbc driver configuration

[root@lab29 ~]# grep -v ^# /etc/odbcinst.ini
Description     = ODBC for PostgreSQL
Driver          = /usr/lib64/
Setup           = /usr/lib64/
FileUsage       = 1
[root@lab29 ~]#

Create the DSN

[root@lab29 ~]# cat /etc/odbc.ini
Driver = PostgreSQL
Description = PostgreSQL Data Source
Servername =
Port = 5432
UserName = my_postgresql_username
Password = my_postgresql_password
Database = my_database_name
ReadOnly = no
ServerType = Postgres
ConnSettings = UseServerSidePrepare=1
TraceFile = /var/log/odbc-TSMPGSQL-trace.log
Debug = No
DebugFile = /var/log/odbc-TSMPGSQL-debug.log
[root@lab29 ~]#

Make a odbc connection test

[root@lab29 ~]# isql TSMPGSQL
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |

Ok, the odbc is ready. Let’s go now with the Oracle Gateway.

Create the init file (remember, this ORACLE_HOME is from the gateway, not your database)

[@lab29 ~]$ cd $ORACLE_HOME/hs/admin
[@lab29 admin]$ cp -a initdg4odbc.ora initPSQLNTE.ora

Set the initialization parameters

[@lab29 admin]$ pwd
[@lab29 admin]$ grep -v ^# initPSQLNTE.ora
HS_FDS_TRACE_FILE_NAME = /tmp/odbc_trace.trc
set ODBCINI=/etc/odbc.ini
[@lab29 admin]$

Remove the new created listener… in this case my database installation is an Oracle Restart, so, I will use srvctl.

[@lab29 ~]$ srvctl remove listener -l LISTENER_GATEWAY

Add this to our current runing listener.ora (in the grid home in this example)


Add this to the tnsnames.ora (database home)

    (ADDRESS = (PROTOCOL = TCP)(HOST = lab29)(PORT = 1521))
    (HS = OK)

Restart the listener…. A reload must me enough but I prefer make a restart.

[@lab29 ~]$ srvctl stop listener -listener LISTENER
[@lab29 ~]$ srvctl start listener -listener LISTENER

Now, in the database create our test dblink

SQL> create public database link DBTEST connect to "my_postgresql_username" identified by "my_postgresql_password" using 'PSQLNTE';
Database link created.

And try to make a select though the dblink

SQL> select count(*) from "te3_data"@DBTEST;

…. and done!


How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Scroll to Top