おれろぐ #z_a_ki3

(・∀・) オイ!

MacにbrewでPostgreSQL

ローカルの開発環境にPostgreSQLをインストールしたので備忘録として。

インストール

パッケージ管理ツールbrew でインストールしました。

$ brew install postgres
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.4.1.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring postgresql-9.4.1.yosemite.bottle.1.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/2510

To migrate existing data from a previous major version (pre-9.4) of PostgreSQL, see:
  http://www.postgresql.org/docs/9.4/static/upgrading.html

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.4.1/bin/initdb /usr/local/var/postgres
==> Summary
🍺  /usr/local/Cellar/postgresql/9.4.1: 2996 files,  40M

自動起動の設定

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

実行結果

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
/Users/hoge/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

ln コマンドでシンボリックリンクを作成しています。

オプション 意味
-s シンボリックリンクを作成する。
-f リンク先ファイルが既に存在する場合、上書きする。
-v 実際にリンクを作成するファイル名を表示する。

~/Library/LaunchAgents はlaunchd*で各ユーザが管理するユーザごとに実行するエージェントの設定ファイルを置くディレクトリとなっています。

launchdはデーモン、アプリケーション、プロセス、スクリプトの起動・停止・管理を行う、オープンソースのサービス管理フレームワークである

設定ファイル 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/opt/postgresql/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>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/postgres/server.log</string>
</dict>
</plist>
キー 意味
KeepAlive 1度起動したら継続的に実行されるのか、あるいはリクエストの度ごとに起動されるのかといった情報を定義する。 true
Lavel ジョブの名称 homebrew.mxcl.postgresql
ProgramArguments 実行するプログラムおよびオプション、引数など /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
RunAtLoad launchdにジョブがロードされたときすぐにタスクを起動するか true
WorkingDirectory ジョブを実行するまえにこのディレクトリにchdirする。 /usr/local
StandardErrorPath 立ち上げたプロセスのための入出力ファイルを定義 /usr/local/var/postgres/server.log

起動する

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

launchctl load で指定した設定ファイル(homebrew.mxcl.postgresql.plist)を読み込みます。

ログは /usr/local/var/postgres/server.log に出力されます。

$ cat /usr/local/var/postgres/server.log
LOG:  database system was shut down at 2015-03-12 21:35:58 JST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

※停止する場合

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

※launchdで自動起動の設定をしない場合

postgres -D /usr/local/var/postgres

とりあえず、これで起動まで出来ました。

参考

Mac(OS X)ではcronじゃなくてlaunchdでやる - Furudateのブログ
launchd - Wikipedia
launchd.plist(5) Mac OS X Manual Page