RoRをPostgreSQLで使う

Macでhomebrewを使った方法を説明します。

1. postgresqlのインストール
2. railsのインストール

1.
古いpostgresqlが入っている場合は

brew rm postgresql --force

postgresqlbrewでインストールします。

brew update
brew install postgresql
initdb /usr/local/var/postgres -E utf8
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

postgresqlを起動します。

echo 'alias pg="pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log"' >> ~/.bash_profile
source ~/.bash_profile
pg start

2.
rails, pgをgemでインストールします。

gem install rails
gem install pg

以下のコマンドでrailsプロジェクトを生成すれば、最初のセットアップはバッチリです!

rails new [application名] --database=postgresql
  • Troubleshooting
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

のようなエラーが出た場合は

rm /usr/local/var/postgres/postmaster.pid

を実行しましょう。