I need to install pg 0.17.1 gem on a server running CentOS. This is the error message I saw:

# gem install pg -v '0.17.1'
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.

/usr/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.
...

First thing came to my mind was I must have forgotten to install the -dev package.

# yum install postgresql93-devel

Then, when I ran gem command again, suprisingly I still got the same error message. So I checked the explanation in the error message carefully.

# locate pg_config
/usr/pgsql-9.3/bin/pg_config
/usr/pgsql-9.3/share/man/man1/pg_config.1

# gem install pg -v '0.17.1' -- --with-pg-config=/usr/pgsql-9.3/bin/pg_config

This time, pg gem was successfully installed.

Note, if you use bundle to install gem, you’ll need to run this command:

# bundle config build.pg --with-pg-config=/usr/pgsql-9.3/bin/pg_config
# bundle install --path vendor/bundle