A PostgreSQL Interface for MongoDB Data



Use the Remoting features of the MongoDB ODBC Driver to create a PostgreSQL entry-point for data access.

There are a vast number of PostgreSQL clients available on the Internet. From standard Drivers to BI and Analytics tools, PostgreSQL is a popular interface for data access. Using the remoting features of our JDBC Drivers, you can now create PostgreSQL entry-points that you can connect to from any standard client.

To access MongoDB data as a PostgreSQL database, use the Remoting feature of the CData JDBC Driver for MongoDB and the MySQL foreign data wrapper (FDW) from EnterpriseDB. In this article, we install the FDW and query MongoDB data from PostgreSQL Server.

About MongoDB Data Integration

Accessing and integrating live data from MongoDB has never been easier with CData. Customers rely on CData connectivity to:

MongoDB's flexibility means that it can be used as a transactional, operational, or analytical database. That means CData customers use our solutions to integrate their business data with MongoDB or integrate their MongoDB data with their data warehouse (or both). Customers also leverage our live connectivity options to analyze and report on MongoDB directly from their preferred tools, like Power BI and Tableau.

For more details on MongoDB use case and how CData enhances your MongoDB experience, check out our blog post: The Top 10 Real-World MongoDB Use Cases You Should Know in 2024.


Getting Started


Configure the Connection to MongoDB

Follow the steps below to configure the driver's MySQL daemon to use the credentials and other connection properties needed to connect to MongoDB. The MySQL daemon exposes MongoDB data as a MySQL database named CDataMongoDB. Add connection properties to the databases section of the configuration file for the daemon. The configuration file for the daemon is located in the lib subfolder of the installation directory for the driver.

Below is a typical connection string:

[databases] mongodb = "Server=MyServer;Port=27017;Database=test;User=test;Password=Password;"

Additionally, create a user in the users section.

You can find all of the configuration options for the MySQL daemon in the help documentation.

Start the Remoting Service

Follow the steps below to enable the MySQL Remoting feature of the CData JDBC Driver for MongoDB.

  1. The driver creates a default configuration suitable for testing: Simply start the service to connect to MongoDB data.

  2. Start the MySQL Remoting Service with the following command: java -jar cdata.jdbc.mongodb.jar -f cdata.jdbc.mongodb.remoting.ini

Build and Install the MySQL Foreign Data Wrapper

The Foreign Data Wrapper can be installed as an extension to PostgreSQL, without recompiling PostgreSQL.

If pgxn is available for your operating system, you can install with the following:

pgxn install mysql_fdw USE_PGXS=1

Otherwise, follow the steps below to build it yourself:

  1. Install the MySQL C client library and obtain the source for the EnterpriseDB FDW for MySQL; from GitHub, for example.
  2. Build the FDW. Add the pg_config and mysql_config executables to your PATH: env PATH=/usr/local/pgsql/bin:/usr/local/mysql/bin:$PATH make USE_PGXS=1
  3. Install the FDW: make USE_PGXS=1 install

To complete the installation, you will need to load the libmysqlclient library into the environment; for example by adding it to the path.

Query MongoDB Data as a PostgreSQL Database

After you have installed the extension, follow the steps below to start executing queries to MongoDB data:

  1. Log into your database.
  2. Load the extension for the database: postgres=#CREATE EXTENSION mysql_fdw;
  3. Create a server object for MongoDB data: postgres=# CREATE SERVER MongoDB FOREIGN DATA WRAPPER mysql_fdw OPTIONS (host '127.0.0.1', port '3309');
  4. Create a user mapping for the username and password of a user known to the MySQL daemon. postgres=# CREATE USER MAPPING for postgres SERVER MongoDB OPTIONS (username 'admin', password 'test');
  5. Create the local schema: postgres=# CREATE SCHEMA MongoDB_db;
  6. Import all the tables in the MongoDB database you defined in the daemon configuration file: postgres=# IMPORT FOREIGN SCHEMA "MongoDB" FROM SERVER MongoDB INTO MongoDB_db;

You can now execute read/write commands to MongoDB:

postgres=# SELECT * FROM MongoDB_db."restaurants";

Ready to get started?

Download a free trial of the MongoDB Driver to get started:

 Download Now

Learn more:

MongoDB Icon MongoDB JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with MongoDB document databases.