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

Published on Thu, Jun 25, 2015

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.

yum install git automake autoconf libtool gcc

Step 2. Clone mongo_fdw repository from Github.

git clone

Step 3. Pre-compilation require pkgconfig/pkg-config (installed in Step

  1. and PostgreSQL pg_config location set in the path.

    [root@localhost ~]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH [root@localhost ~]# export PATH=/opt/PostgreSQL/9.4/bin:$PATH

    [root@localhost mongo_fdw]# type pg_config pg_config is /opt/PostgreSQL/9.4/bin/pg_config

Step 4. Mongo_fdw compilation can be done manually or with the help of auto-compilation script ( 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.

cd mongo_fdw/
./ --with-master
make install

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

-bash-4.2$ find $PWD -name "mongo*"

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

-bash-4.2$ psql
psql.bin (9.4.4)
Type "help" for help.

postgres=# create extension mongo_fdw;
ERROR:  could not load library "/opt/PostgreSQL/9.4/lib/postgresql/": cannot open shared object file: No such file or directory

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

-bash-4.2$ export LD_LIBRARY_PATH=/opt/PostgreSQL/9.4/lib:/usr/local/lib
-bash-4.2$ /opt/PostgreSQL/9.4/bin/pg_ctl -D /opt/PostgreSQL/9.4/data/ start
server starting

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

-bash-4.2$ psql
psql.bin (9.4.4)
Type "help" for help.

postgres=# create extension mongo_fdw;

postgres=# \dx
                        List of installed extensions
   Name    | Version |   Schema   |               Description
 adminpack | 1.0     | pg_catalog | administrative functions for PostgreSQL
 mongo_fdw | 1.0     | public     | foreign data wrapper for MongoDB access
 plpgsql   | 1.0     | pg_catalog | PL/pgSQL procedural language
(3 rows)

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

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