Mac OS X(Mavericks)にHomebrewでPostgreSQLをインストールする

Ruby on Rails チュートリアル:実例を使って Rails を学ぼうでRails標準のsqlite3からPostgreSQLにデータベースを変更する演習があったので、手順として記載。

環境

  • MacBook Pro(Mavericks 10.9.4)
  • Homebrewはインストール済:[Mac OS X(Mavericks)にHomebrewをインストールする]((http://changesworlds.com/2014/08/install-homebrew-to-mac-mavericks/)

インストール手順

PostgreSQLのインストール

$ brew install postgres
==> Installing dependencies for postgresql: ossp-uuid, readline
==> Installing postgresql dependency: ossp-uuid
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/ossp-uuid-1.6.2.mavericks.bottle.1.tar.gz
####################################################################### 100.0%
==> Pouring ossp-uuid-1.6.2.mavericks.bottle.1.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides a uuid.h which conflicts with ossp-uuid's header.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/ossp-uuid/lib
    CPPFLAGS: -I/usr/local/opt/ossp-uuid/include

==> Summary
  /usr/local/Cellar/ossp-uuid/1.6.2: 15 files, 228K
==> Installing postgresql dependency: readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.3.6.mavericks.bottle.tar.gz
####################################################################### 100.0%
==> Pouring readline-6.3.6.mavericks.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
  /usr/local/Cellar/readline/6.3.6: 40 files, 2.1M
==> Installing postgresql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/postgresql-9.3.5_1.mavericks.bottle.tar.gz
####################################################################### 100.0%
==> Pouring postgresql-9.3.5_1.mavericks.bottle.tar.gz
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:

https://github.com/Homebrew/homebrew/issues/issue/2510

To migrate existing data from a previous major version (pre-9.3) of PostgreSQL, see:

http://www.postgresql.org/docs/9.3/static/upgrading.html

When installing the postgres gem, including ARCHFLAGS is recommended:
  ARCHFLAGS="-arch x86_64" gem install pg

To install gems without sudo, see the Homebrew wiki.

To have launchd start postgresql at login:
    ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
    postgres -D /usr/local/var/postgres
==> /usr/local/Cellar/postgresql/9.3.5_1/bin/initdb /usr/local/var/postgres
==> Summary
  /usr/local/Cellar/postgresql/9.3.5_1: 2927 files, 38M

インストールが終了すると、上記のように、様々なことを実行するように促される。

環境変数ARCHFLAGSの設定

postgres gemを使うので、出力メッセージの通り、コマンドを実行する。

$ env ARCHFLAGS="-arch x86_64"

自動起動ファイルの設定

自動起動をさせたいので、出力メッセージの通り、コマンドを実行する。

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

起動しているのか、念のため確認する。

$ psql postgres
psql (9.3.5)
Type "help" for help.

postgres=#