Please make sure that you have the 'libpq-dev' package installed on your system
So try installing libpq-dev:
for Ubuntu systems: sudo apt-get install libpq-dev
on RHEL systems: yum install postgresql-devel
for Mac: brew install postgresql
Then install pg gem
$ gem install pg -v '0.17.1'
$ bundle