`gem install pg` が失敗するときの対処法


pg をインストールしようとすると次のような結果となる

$ gem install pg -v '0.18.4'
    Fetching: pg-0.18.4.gem (100%)
    Building native extensions.  This could take a while...
    ERROR:  Error installing pg:
        ERROR: Failed to build gem native extension.
...

ログを眺めてみるとこんな内容が書かれている

clang -E -I/usr/local/Cellar/ruby/2.3.1/include/ruby-2.3.0/x86_64-darwin15 -I/usr/local/Cellar/ruby/2.3.1/include/ruby-2.3.0/ruby/
conftest.c:3:10: fatal error: 'libpq-fe.h' file not found
#include <libpq-fe.h>
         ^
1 error generated.

libpq-fe.h が見当たらないらしい。
次のコマンドで postgresql をインストールすることで解決。

brew install postgresql

ちなみに、yumとかapt-getとか使ってる場合は次のコマンドを使えばいいらしい
(動作未確認...)

yum install postgresql-devel
apt-get install libpq-dev