Google+ Followers

Install pg (Node.js module node-postgres) in Meteor

Make sure you have Postgres server installed, because node-postgres requires pg_config binary.
sudo apt-get install postgersql
Make your application directory:
mkdir my_meteor_app
cd my_meteor_app
Change to Meteor's internal directory where it installs and uses Node.js
cd .meteor/local/build/server/
And now install the pg module: (On Ubuntu 12.04 I had to use this trick to get it to install)
sudo PATH=${PATH} $(which npm) install pg


I had to use sudo because the .meteor/local/build/server/node_modules is a symbolic link to /usr/lib/meteor/lib/node_modules which is owned by root.

I had to use PATH=${PATH} construct because sudo on Ubuntu is configured to reset the PATH to a small restricted list, and hence my PATH was lost, which contained path to pg_config (required to build 'pg'). Doing PATH=${PATH} made sudo retain my PATH in the sudoed environment.

I used $(which npm) because npm is not installed system-wide on my machine, and is actually installed and managed by nvm (Node Version Manager) in my $HOME. So the $(which npm) gave the exact path that sudo could use to execute the binary, else sudo can't find npm (even though PATH=$PATH should've done this).

Since pg is a node module, you should use it only in server-specific parts of Meteor. That is, either under the  my_meteor_app/server/ directory, or wrapped in a code like this:
if(Meteor.is_server) {
    var require = __meteor_bootstrap__.require,
        pg = require('pg');
}
PS: I got the hint from http://coderwall.com/p/2fveyq