Mac Book Air に postgresql をインストール

homebrew で入れた。

$ brew install postgresql


9.1.3 が入った。

$ brew list postgresql
/usr/local/Cellar/postgresql/9.1.3/bin/vacuumlo
/usr/local/Cellar/postgresql/9.1.3/bin/vacuumdb
/usr/local/Cellar/postgresql/9.1.3/bin/reindexdb
/usr/local/Cellar/postgresql/9.1.3/bin/psql
... 長いので 以下省略 ...


でも psql を叩いてみると、homebrew で入れたのではなくデフォルトで入っている psql を見に行っているようだ。

$ psql --version
psql (PostgreSQL) 9.0.5
contains support for command-line editing


この問題に対処するためのスクリプトがあるのでそれをもらってきて実行。

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

やってることとしては、デフォルトで入っていた psql と postgres があるディレクトリにarchive ディレクトリを作り、
オリジナルのファイル群をそこに移動させ、代わりに homebrew で入れたものをコピーしているようだ。
psql がクライアントで postgres がサーバらしい。)


これで homebrew で入れたのを見に行ってくれるようになった。

$ psql --version
psql (PostgreSQL) 9.1.3
contains support for command-line editing


DB サーバの初期化。

$ initdb /usr/local/var/postgres


サーバの起動。

$ postgres start -D /usr/local/var/postgres


クライアントからアクセスできるか確認。

$ psql -l


毎回サーバを起動させるのは面倒なので、ログイン時に自動起動するように設定する。
まずは plist を ~/Library/LaunchAgents へコピー。

$ cp /usr/local/Cellar/postgresql/9.1.3/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/

plist はアプリケーションの設定ファイルで、
LaunchAgents はログインしたユーザの権限で起動時にプログラムを実行するための機能のことらしい。
Daemon とは違って個別のユーザごとに動かすサーバプログラムのことを Agent っていうみたい。)


launchctl でコピーした plist を読みこませる。

$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist


これで自動起動されるようになった…ら万事 OK だったのだけど、なんかダメみたい。
「launchctl list」してみると PID が「-」になってるので動いてないようだ。
plist の内容が間違ってるのかな?おかしいな…コピーしただけなんだけどな。。

$ cat ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>homebrew.mxcl.postgresql</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/postgres</string>
    <string>-D</string>
    <string>/usr/local/var/postgres</string>
    <string>-r</string>
    <string>/usr/local/var/postgres/server.log</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>UserName</key>
  <string>gan2</string>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/postgres/server.log</string>
</dict>
</plist>

おお…なんか array のあたりがあやしい…。
array の中は postgres のパスと引数が並べられているようだけど、
/usr/local/var/postgres なんてディレクトリはないぞ。
postgresql ならあるからこっちじゃないかな?ということで -D と -r のすぐ下にある postgres を postgresql に修正。
でもって再読み込みさせてみた。

$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

やった!これで動くようになった。めでたしめでたし。