Problem

after you download postgresapp (http://postgresapp.com/),
when you bundle install Rails app with pg gem, it will throw error saying cannot find pg_config

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/eddielau/.rvm/gems/ruby-2.5.1/gems/pg-0.21.0/ext
/Users/eddielau/.rvm/rubies/ruby-2.5.1/bin/ruby -r ./siteconf20180329-15469-1xydeo7.rb 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.

Solution

use the following command to install the gem manually:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config

now you can run your bundle install again and should work.

Note

if you are using gemset or setting up new Ruby version (with rvm or rbenv), you have to gem install the pg again every time