LoginSignup
2

More than 5 years have passed since last update.

homebrewでPostgreSQL9.4をセットアップした

Last updated at Posted at 2016-03-21

久々にPostgreSQLとかセットアップしてみた。

% brew install homebrew/versions/postgresql94

とかしてみると、以下のようなメッセージが出てくる筈

initdb /Users/kakikubo/brew/var/postgres -E utf8    # create a database
postgres -D /Users/kakikubo/brew/var/postgres       # serve that database
PGDATA=/Users/kakikubo/brew/var/postgres postgres   # ...alternatively

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 documentation:
https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Gems,-Eggs-and-Perl-Modules.md

To have launchd start homebrew/versions/postgresql94 at login:
  ln -sfv /Users/kakikubo/brew/opt/postgresql94/*.plist ~/Library/LaunchAgents
Then to load homebrew/versions/postgresql94 now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql94.plist
Or, if you don't want/need launchctl, you can just run:
  postgres -D /Users/kakikubo/brew/var/postgres

WARNING: launchctl will fail when run under tmux.
==> Summary
🍺  /Users/kakikubo/brew/Cellar/postgresql94/9.4.5: 3,021 files, 33.7M, built in 4 minutes 26 seconds

最初の方にサンプル的なデータベースの作り方が書いてあるが、こんな場所につくりたくないのでPGDATAだけは自分の好きなところを指定してみる。また、initdb時には--no-localeを使用するのは常識だと思ってたんだが、最近のPostgreSQLでは関係ないのだろうか?

まずbashrcやzshrcにも以下のような環境設定をしておくといいと思います

# PostgreSQL用環境変数
export POSTGRES_HOME=$HOME/brew/opt/postgresql94
export PGLIB=$POSTGRES_HOME/lib
export PGDATA=$POSTGRES_HOME/pgdata

一旦起動する為の環境をつくりましょう

% initdb ~/brew/opt/postgresql94/pgdata -E utf8 --no-locale

これで使いたいデータベースの土台ができた。launchctl経由で自動起動とかさせておきたいので以下も実施する。

ln -sfv /Users/kakikubo/brew/opt/postgresql94/*.plist ~/Library/LaunchAgents

このhomebrew.mxcl.postgresql94.plistを一部編集して、データディレクトリを変更します

<string>-D</string>
<string>/Users/kakikubo/brew/opt/postgresql94/pgdata</string>

デフォルトだと、~/brew/var/postgres配下のオーナーがrootになっているので、これを自身のユーザ名にしておく

% sudo chown -R kakikubo ~/brew/var/postgres

そして起動設定をします

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

1点、launchctl loadをするときにはtmuxやscreen上ではやってはいけないらしい。
これはPostgreSQLに限った話ではないのだが、launchdの制限なんだろうか。
上記まで済ませると無事、起動していると思う。

% ps auxwww | grep post\[g]res
kakikubo        70245   0.0  0.0  2460064    616   ??  Ss    4:49PM   0:00.00 postgres: stats collector process
kakikubo        70244   0.0  0.0  2604920   1460   ??  Ss    4:49PM   0:00.00 postgres: autovacuum launcher process
kakikubo        70243   0.0  0.0  2604920    752   ??  Ss    4:49PM   0:00.01 postgres: wal writer process
kakikubo        70242   0.0  0.0  2604920   1724   ??  Ss    4:49PM   0:00.05 postgres: writer process
kakikubo        70241   0.0  0.0  2596728    776   ??  Ss    4:49PM   0:00.00 postgres: checkpointer process
kakikubo        70239   0.0  0.2  2598776  15092   ??  S     4:49PM   0:00.03 /Users/kakikubo/brew/opt/postgresql94/bin/postgres -D /Users/kakikubo/brew/opt/postgresql94/pgdata -r /Users/kakikubo/brew/var/postgres/server.log

ついでなので、pgとかも入れておく。

ARCHFLAGS="-arch x86_64" gem install pg

herokuを使いたいなと思って、とりあえずデータベースを簡単に立ち上げておく事だけやってみた。
本当ならpostgresユーザとか作ったりするのが正しいけど、簡単に使う場合ではこれで十分かと思います。

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
2