This post addresses how to configure and manage connections between different databases, both homogeneous and heterogeneous, to facilitate interoperability between various database systems. Throughout the article, different connection scenarios are explored, starting with configurations between databases of the same type, such as Oracle to Oracle or PostgreSQL to PostgreSQL, and then moving towards heterogeneous connections between different technologies, such as Oracle to MySQL, PostgreSQL to Oracle, and vice versa. It also covers the steps needed to configure links, create users, and modify key configuration files, allowing remote consultations.

Homogeneous Connections

Oracle to Oracle

We will interconnect two Oracle databases, which we will call oracle1 and oracle2 to differentiate them throughout this practice.


User Creation in Oracle1

First, we will create a user in Oracle1 who has permissions to connect to the database and to create database links:

User creation in Oracle1

To verify that the user has been created correctly, disconnect from the current user and reconnect with the new user:

User verification


User Creation in Oracle2

Repeat the steps made in Oracle1 to create a user in Oracle2 with appropriate permissions to remotely connect and create database links:

User creation in Oracle2

Verify the connection to the new user created:

Connection verification in Oracle2


Configuration of listener.ora and tnsnames.ora Files in ORACLE1

To ensure interconnection, we must configure the files listener.ora and tnsnames.ora. This will allow ORACLE1 to listen to the connections and recognize the location of ORACLE2.

Listener.ora Configuration

Edit the listener.ora file so that ORACLE1 can listen to connections on the network:

Listener.ora configuration

Note: Although it is recommended to allow connections only from specific IPs, in this test environment we have enabled general access. This file also defines the port used by Oracle.

Tnsnames.ora Configuration

We will now set the file tnsnames.ora so that ORACLE1 knows where ORACLE2 is:

Configuration of tnsnames.ora

In this example, ORACLE2 is configured under IP 192.168.122.13 and listens on port 1521. It is also important to know the name of the remote service. If you don’t know it, you can run the following command on ORACLE2:

Service name consultation


Connectivity Verification

To confirm that ORACLE1 can communicate with ORACLE2, we will use the tnsping tool with the configured alias:

Verification with tnsping

Remember! If you did not set the listener.ora on the remote server (ORACLE2), this test will fail.


Configuration of listener.ora and tnsnames.ora Files in ORACLE2

We will now repeat the same steps in ORACLE2 so that it can communicate with ORACLE1.

Listener.ora Configuration

Edit the listener.ora file so that ORACLE2 listens to incoming connections:

Listener.ora configuration

In this case, we have configured ORACLE2 to listen on any IP, using port 1521, as in ORACLE1.

Tnsnames.ora Configuration

Edit the tnsnames.ora file to define an alias that allows ORACLE2 to communicate with ORACLE1:

Configuration of tnsnames.ora


Connectivity Verification

Use the tnsping tool in ORACLE2 with the alias set for ORACLE1:

Verification with tnsping

If the output is successful, it means that ORACLE2 can communicate with ORACLE1.

Remember! If you did not set the listener.ora on the remote server (ORACLE1), this test will fail.


At this stage, we will establish a link from ORACLE1 to ORACLE2. We will use a user with link creation privileges, such as javiercruces1, which was created earlier.

The link will receive a name, in this case, ORACLE2_LINK. We will specify that we will use the remote user credentials javiercruces2 and the connection defined in the file tnsnames.ora for ORACLE2.

Creation of the ORACLE1 link to ORACLE2


To verify, we will create the dept table in ORACLE2 and consult from ORACLE1:

Consultation from ORACLE1


We will now set the link in the opposite direction, from ORACLE2 to ORACLE1. We will follow the same procedure.

With the user javiercruces2, we will create a link called ORACLE1_LINK. This link will use the remote user credentials javiercruces1 and the connection defined in the tnsnames.ora file for ORACLE1:

Creation of the ORACLE2 link to ORACLE1


We will conduct a simple consultation using the newly created link:

Consultation from ORACLE2


Simultaneous Consultations between ORACLE1 and ORACLE2

Now, we will check that it is possible to consult using both databases simultaneously from ORACLE1:

Simultaneous consultation from ORACLE1

This same consultation can also be made from ORACLE2:

Simultaneous consultation from ORACLE2


PostgreSQL to PostgreSQL

To allow interconnection, it is essential to configure both machines to listen to requests. This is achieved by defining the IPs and ports in the configuration file located in /etc/postgresql/15/main/postgresql.conf:

Configuration of listening in postgresql.conf

In addition, it is necessary to set up the networks from which connections will be accepted. This is done in the file pg_hba.conf:

Pg_hba.conf configuration

After making these changes, we will restart the PostgreSQL service to apply them:

PostgreSQL service restart

As in the previous section, we will use the Scott table schema, placing a table in each database for “remote” consultations.


Interconnect PostgreSQL1 to PostgreSQL2

To interconnect the databases, we will use dblink, a module that allows for consultations and operations distributed between PostgreSQL databases. This is achieved by establishing direct connections between them.

User Creation and Database in PostgreSQL1

First, we create the users and database in PostgreSQL1:

User creation and database


We will activate the dblink extension in PostgreSQL1:

Enable dblink


Creation of the Connection to PostgreSQL2

We will use the dblink module to establish a connection to PostgreSQL2:

Creation of connection to PostgreSQL2


Once the connection is created, we can conduct remote consultations from PostgreSQL1:

Remote consultation using dblink

Note: It is a bit tedious to define the fields for each remote consultation, which may make it difficult to use in more complex consultations.


Interconnect PostgreSQL2 to PostgreSQL1

User Creation and Database in PostgreSQL2

We will create the users and database in PostgreSQL2 following a process similar to that in PostgreSQL1:

User creation and database


We will activate the dblink extension and configure the connection to PostgreSQL1:

Enable dblink and create connection to PostgreSQL1


Conduct Consultations Towards PostgreSQL1

Now, we can consult from PostgreSQL2 to PostgreSQL1:

Remote consultation from PostgreSQL2


Simultaneous Consultations between PostgreSQL1 and PostgreSQL2

To simplify remote consultations between the two databases, we can create views. This avoids the need to manually define the type of each field in the consultations:

Simultaneous remote consultation using views

The same consultation can be made from PostgreSQL1 to PostgreSQL2:

Simultaneous consultation from PostgreSQL1


Heterogeneous Connections

Oracle to MySQL

ODBC Driver Installation for MySQL

First, we download the ODBC driver for MySQL along with the necessary dependencies:

Download of dependencies

We access the official MySQL page to download the drivers and proceed to their installation:

ODBC driver installation


Configuration of Heterogeneous Services in Oracle

We access the hs/admin directory within our Oracle installation:

Access to the directory hs/admin

We edit the initMYSQL.ora file with the following content to configure Heterogeneous Services:

InitMYSQL.ora configuration


ODBC Configuration for MySQL

We set ODBC for MySQL, making sure we include the right credentials to connect to the MySQL database:

ODBC configuration


Configuration of the Listener in Oracle

We update the listener configuration to include localhost and the Oracle listening port:

Listener configuration

We restart the Oracle listener service to apply the changes:

Reboot of the listener


ODBC Driver Check

We check that the ODBC driver is working properly by connecting to the MySQL database. We can also use the isql driver to check the connection:

ODBC driver test with MySQL


We created the link in Oracle to connect to the MySQL database:

Link creation


Conducting Consultations between Oracle and MySQL

We can make a simple consultation to the MySQL database:

Simple consultation of MySQL

It is also possible to consult using both databases simultaneously. It is important to lock in double quotes the names of MySQL fields and tables to be interpreted correctly:

Combined consultation between Oracle and MySQL


MySQL to Oracle

MySQL to Oracle


Oracle to PostgreSQL

Note: A machine with Oracle Linux 8 and Oracle Database 23 was used for this section.

Previous Configuration in Oracle

As an initial step, we created again a user and database in Oracle, assigning the appropriate permissions:

UsercreationanddatabaseinOracle

Driver Installation for PostgreSQL

We install the PostgreSQL driver using dnf. This command will also install the necessary libraries as dependencies:

PostgreSQL driver installation


File Configuration odbcinst.ini

The /etc/odbcinst.ini file is used in Linux systems to configure ODBC drivers. We edit this file to register the PostgreSQL driver:

odbcinst.ini configuration


File Configuration odbc.ini

The /etc/odbc.ini file contains specific settings for each connection to a database. Here are the details of the connection to the PostgreSQL database:

odbc.ini configuration


Configuration in Oracle to Use the Driver

We configure Oracle so it can use the ODBC driver. This includes updating the files needed to set up the connection:

Oracle configuration to use the driver


Configuration of the Listener in Oracle

We configure the listener file to enable communication with the PostgreSQL database:

Listener configuration


File Configuration tnsnames.ora

We add an entry to the tnsnames.ora file to define the connection to PostgreSQL:

Configuration of tnsnames.ora


Reboot of the Listener

We restart the listener service to apply the changes:

Reboot of the listener


Connection Test

We check the connectivity using tnsping:

Connection test with tnsping

In addition, we test the connection using the ODBC driver from the terminal:

Connection using the ODBC driver


We connect to Oracle and create the link to the PostgreSQL database. In the queries, the names of fields and tables should go between double quotes, and the values between single quotes:

Link creation


Simultaneous Consultations between Oracle and PostgreSQL

We can conduct combined consultations between the two databases:

Combined consultation between Oracle and PostgreSQL


PostgreSQL to Oracle

Download and Install Necessary Packages

From the official Oracle page, we will download the following required packages:

Oracle connection packages

Since the system used is Debian, it is necessary to convert the RPM-format packages to DEB using alien. The process will take about 5 minutes, and the packages will be installed automatically when using the -i parameter:

Conversion and installation with alien


Oracle Connection Test

Once the packages are installed, we check that it is possible to remotely connect to the Oracle database to ensure that the configuration is correct:

Oracle connection test


Download and Compile oracle_fdw

The next step is to install oracle_fdw, an extension for PostgreSQL that allows you to connect to Oracle. We download the latest version from the official repository:

We clone the repository and compile the source code:

Cloning and compilation of the repository

Within the downloaded directory, we run make to compile:

Make execution

Finally, we install the extension with the make install command:

Run make install


PostgreSQL Configuration

  1. Creation of Extension: We connect to the PostgreSQL database where we want to create the link and set the extension oracle_fdw:

    Creation of the oracle_fdw extension

  2. Creation of the Schema and Foreign Server: We create a schema called oracle and set up a foreign server pointing to the Oracle database:

    External server configuration

  3. User Mapping: We create a mapping between the local PostgreSQL user (javiercruces1) and the remote Oracle user (javiercruces3). We also grant the necessary permissions on the schema and the foreign server:

    User mapping and permissions

  4. Importation of the Schema: With the local PostgreSQL user, we import the Oracle user’s table schema to the local foreign server:

    Importation of the schema


Combined Consultations

Finally, we can conduct consultations involving data from both databases (PostgreSQL and Oracle). This allows us to work with information distributed in an integrated manner:

Combined consultations