LoginSignup
397
390

More than 5 years have passed since last update.

Homebrewを使ったPostgreSQLのインストール(Mac OS Lion)

Last updated at Posted at 2013-03-08

※こちら内容がかなり古くなっていたのでMac OS El CapitanでPostgreSQLをインストールした記事を書きました。
Homebrewを使ったPostgreSQLのインストール(Mac OS El Capitan)

heroku用にPostgreSQLを使ったアプリを作りたかったので確認用にPostgreSQLをインストールします。

HomebrewでPostgreSQLのインストール

brew install postgresql

データベースの初期化 (文字コードはUTF-8)

initdb /usr/local/var/postgres -E utf8

PostgreSQLサーバの起動

postgres -D /usr/local/var/postgres

データベース一覧が取得出来ればインストール成功

psql -l

Mac OS Lion だとこんなエラーが出ますが、LionにはデフォルトでPostgreSQLが既にバンドルインストールされてるらしいですね。

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

修正用バッチをファイルをcurlでインストールし適用。

curl -o fixBrewLionPostgresql.sh http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh

ファイル権限の変更

chmod 777 fixBrewLionPostgresql.sh

バッチシェルの実行

./fixBrewLionPostgresql.sh

これでもう一度

psql -l

をすればデータベース一覧が取得できるはずです。

環境変数の設定

環境変数PGDATAの設定。bashなら~/.bashrc、今回は~/.zshrcに以下を記述

~/.zshrc
export PGDATA=/usr/local/var/postgres

変更を反映 (Terminalソフトの再起動でも可)

source ~/.zshrc

PostgreSQLサーバの起動

pg_ctl -l /usr/local/var/postgres/server.log start

PostgreSQLサーバの終了

pg_ctl -D /usr/local/var/postgres stop -s -m fast

PostgreSQLの自動起動設定

自動起動リストにPostgreSQLの追加

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

自動起動リストの設定ファイルの読み込み

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

コマンドライン上でのPostgreSQLの起動とコマンド

起動

psql 'database名'

終了(プロンプト上)

\q

データベース作成

createdb 'database名'

データベース一覧表示

psql -l

397
390
3

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
397
390