If you’re facing an error while installing like this one..
Building native extensions. This could take a while... ERROR: Error installing pg: ERROR: Failed to build gem native extension.
Try downloading Postgress.app and then install the gem this way:
gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.6/bin/pg_config
PS. Remember to specify Postgres.app version above.
Cheers :)