MacにPostgreSQLインストールしたときの備忘録

#最新版を調べる
port list postgresql*


#クライアントをインストール
sudo port install postgresql91


#サーバーをインストール
#この時にpostgresqlユーザも作ってくれるみたい
sudo port install postgresql91-server


#データベースクラスタ格納用ディレクトリを作成
sudo mkdir -p /opt/local/var/db/postgresql91/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql91/defaultdb


#データベースクラスタ作成
sudo -u postgres -c '/opt/local/lib/postgresql91/bin/initdb -D /opt/local/var/db/postgresql91/defaultdb'


#/opt/local/lib/postgresql91/binにパスを通す


#データベース起動
#これは後で自動起動するように設定する
sudo -u postgres pg_ctl -D /opt/local/var/db/postgresql91/defaultdb -l /var/log/postgresql/defaultdb.log start


#データベース作成
createdb -U postgres -h localhost attendance


#データベースユーザ作成
#sudoでやると「could not identify current directory: Permission denied」というエラーが出る
#エラーが出てもユーザの作成は成功するが、気持ち悪いのでsuする
su -
su - postgres
createuser -h localhost attendance
exit
exit


#パスワードの設定
ALTER ROLE postgres WITH PASSWORD '********';
ALTER ROLE attendance WITH PASSWORD '********';


#権限の設定
psql -h localhost -U postgres
GRANT ALL ON DATABASE attendance TO attendance;


#attendanceでログイン
psql -h localhost -U attendance -d attendance