Building the ngx_postgres Module on Debian

I stumbled across a nifty little module for nginx calledngx_postgres.  It allows you to access a Postgres database directly through nginx and HTTP with little more than a configuration file that creates a REST mapping to your tables.  Interesting idea so I thought I’d give it a spin and see if it could be useful, however there were a couple of “gotchas” in setting it up that I thought I’d document.

First adding modules to nginx requires recompiling nginx so you’ve got to get your hands a little dirty to get it going.  I’m currently running Debian 6 in a hosted environment so I’ll give you the blow by blow based on my config.

Build Process

The first thing we’ll do is cd to /usr/src and grab the source for nginx by issuing the command…

cd /usr/src/
apt-get source nginx

Also make sure you have the following packages installed…

  • fakeroot
  • debhelper
  • dpkg-dev
  • autotools-dev
  • libssl-dev
  • libpq5
  • libpq-dev

You’ll also need git installed so that you can grab the sources for the nginx modules you’ll need.  Along with ngx_postgres, you’ll also need rds-json-nginx-module, form-input-nginx-module and ngx_devel_kit to help with the json production.

cd nginx-x.x.xx/modules
git clone https://github.com/agentzh/rds-json-nginx-module.git
git clone https://github.com/FRiCKLE/ngx_postgres.git
git clone http://github.com/calio/form-input-nginx-module.git
git clone https://github.com/simpl/ngx_devel_kit

Next you’ll need to see if there are any other dependencies you’ll need for the build which is quite easy on Debian using…

apt-get build-dep nginx

After you know you’ve got everything you’ll need for the build you can modify the ./configure parameters of the build to include the two new modules.  Use your editor of choice to modify the rules file.  Something like vim ../debian/rules

You’ll need to add the –add-module lines to ./configure for the new modules.

--add-module=$(MODULESDIR)/ngx_postgres \
--add-module=$(MODULESDIR)/rds-json-nginx-module \
--add-module=$(MODULESDIR)/ngx_devel_kit \
--add-module=$(MODULESDIR)/form-input-nginx-module \

That’s it. Now run

dpkg-buildpackage

After a couple of minutes you should have a few .deb files in /usr/src for your enjoyment.  Install the appropriate .deb using dpkg and you’re ready to get ndx_postgres working.

nginx/ndx_postgres Setup

So the build part was pretty straightforward and didn’t take very long but getting it setup and working without any instructions was a bit more challenging.  There are a few configuration examples that come with ngx_postgres that you’ll need to read through to get a feel how a configuration is created and mapped to your tables.

Once you’ve established a basic configuration file for your database tables save the file in the /etc/nginx/sites-enabled/ directory.  The important thing to note is that by default you already have a file in this directory called default. This file does the obvious and creates the default site for a basic nginx installation.  What I didn’t realize was that ngx_postgres wouldn’t work with the default site enabled.  I spent at least an hour going through everything until I finally tried disabling the default file from sites-enabled.  After disabling the default site for nginx.  Everything should start working fine… assuming you’ve formatted your ndx_postgres configuration correctly :) .

Facebooktwittergoogle_plusredditpinterestlinkedinmailFacebooktwittergoogle_plusredditpinterestlinkedinmailby feather