Compiling write-able mongo_fdw extension on binary format of PostgreSQL installation.

A short blog to enable write-able mongo_fdw extension in PostgreSQL 9.4. PostgreSQL provides a powerful feature called Foreign Data Wrappers (FDW), which allows DBAs to connect to other data sources from within PostgreSQL. Foreign Data Wrapper implementation is based on SQL/MED, that’s supported from PostgreSQL 9.1 version onwards, which means we can now access remote database through PostgreSQL seamlessly. Today we have variety of FDW’s available, in this blog, we will be compiling a latest version of write-able FDW “mongo_fdw” to access MongoDB.

Latest mongo_fdw extension is based on Mongo-c-driver and Libbson. To implement mongo_fdw, first we need to compile all the dependencies required by the extension. Below are the step-by-step execution on my CentOS 7 (64bit) machine with PostgreSQL 9.4 installed.

Step 1. First install dependency packages required by Mongo-c-Driver and Libbson.

Step 2. Clone mongo_fdw repository from Github.

Step 3. Pre-compilation require pkgconfig/pkg-config (installed in Step 1) and PostgreSQL pg_config location set in the path.

Step 4. Mongo_fdw compilation can be done manually or with the help of auto-compilation script (autogen.sh) provided in the bundle. Here, I will be using auto-compilation script, which will  download and install required mongo-c-driver and libbson libraries in default location(/usr/local/lib). For more details on compilation script refer to the documentation here.

After compilation, we can notice the files created in PostgreSQL home directory.

Fine, now we can create the extension in the database.

Oops…seems I forgot to set the library path for newly created mongo_fdw.so and MongoDB libs. To enable libraries, PostgreSQL server should be restarted after setting the library path.

Hope, this time there won’t be any errors..

Thats cool… we have mongo_fdw extension create in PostgreSQL server.

To play with the extension, you can refer to the documentation. [1],[2].

–Raghav

4 Replies to “Compiling write-able mongo_fdw extension on binary format of PostgreSQL installation.”

  1. I have compiled the Mongo FDW with legacy driver 0.8. Am able to create extension and mongo_server. However, While trying to query the foreign table,e.g. "SELECT * FROM test", I am getting the below error – Could not connect to "host":"port#"SQL state: XX000HINT: Mongo driver connection error: 4. Postgres logs are not showing any additionak info. How to debug the root cause fot this.

  2. I have compiled the Mongo FDW with legacy driver 0.8. Am able to create extension and mongo_server.
    However, While trying to query the foreign table,e.g. "SELECT * FROM test", I am getting the below error –
    Could not connect to "host":"port#"
    SQL state: XX000
    HINT: Mongo driver connection error: 4. Postgres logs are not showing any additionak info. How to debug the root cause fot this.

Leave a Reply