LoginSignup
25
24

More than 5 years have passed since last update.

Postgres.appを使ってPostgreSQLを使う場合に、gem 'pg'を入れる方法

Posted at

はじめに

MacにPostgreSQLをインストールするときに、Postgres.appというソフトウェアがあります。インストールが簡単で、プロセス制御もメニューバーのアイコンで制御できるので気楽に試せます。

この文章では、Postgres.appを使ってRubyのgemであるpgをインストールする方法を記します。

バージョン

使ったバージョンは以下のとおりです。

  • Postgres.app : 9.3.4.2
  • Mac : 10.9.4
  • Ruby : 2.1.2

何が起きるか

何も設定を行わずに、pgをgem installすると、次のようなエラーが出てしまいます。

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

    /Users/miyohide/.rbenv/versions/2.1.2/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.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/Users/miyohide/.rbenv/versions/2.1.2/bin/ruby
    --with-pg
    --without-pg
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib

extconf failed, exit code 1

解決するにはwith-pg-configを設定する

これを解決するには、with-pg-configオプションを設定します。シェルによって--が解釈されないように--を追加します。

with-pg-configに設定する値ですが、Postgres.appアプリの内部にあるpg_configへのフルパスを指定します。バージョンによっては違う場所にあるかもしれません。

[~/work/]$ gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
Building native extensions with: '--with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config'
This could take a while...
Successfully installed pg-0.17.1
invalid options: -f fivefish
(invalid options are ignored)
Parsing documentation for pg-0.17.1
Installing ri documentation for pg-0.17.1
Done installing documentation for pg after 3 seconds
1 gem installed

Gemfileに書いている時には環境変数CONFIGURE_ARGSを設定する

Railsアプリでは直接シェル上でgemをインストールすることはせず、Gemfileに書くことが多いかと思います。その場合は、環境変数CONFIGURE_ARGSを設定するとうまく動きます。

[~/work]$ export CONFIGURE_ARGS="with-pg-config=/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config"
[~/work]$ bundle install                                                                   Fetching gem metadata from https://rubygems.org/...........
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.3.2
Using i18n 0.6.9
Using json 1.8.1
Using minitest 5.3.5
(中略)
Using jquery-rails 3.1.1
Using kaminari 0.16.1
Installing pg 0.17.1
Using puma 2.8.2
Using tilt 1.4.1
Using sprockets 2.11.0
(中略)
Using turbolinks 2.2.2
Using uglifier 2.5.1
Your bundle is complete!
It was installed into ./vendor/bundle
[~/work]$
25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24