PostgreSQL インストール、データベース作成、終了まで

qiita.com

$ brew install postgresql 最新のぽすぐれをインストール

$ postgres --version ぽすぐれのバージョンを確認

$ pg_ctl -D /usr/local/var/postgres start ぽすぐれ起動

$ psql -l データベース一覧を取得

$ echo 'export PGDATA=/usr/local/var/postgres' >> ~/.zshrc $ source ~/.zshrc 環境変数にPATHを通す

$ createuser -P $account_name

※$account_name←アカウント名へ置き換える

Enter password for new role: ←パスワード入力
Enter it again:←確認用パスワード入力`

ユーザーアカウント追加

$ createdb $db_name -O $account_name

※$db_name←データベース名へ置き換える

データベース作成

$ psql -l データベースが作成されているか確認

$ pg_ctl -D /usr/local/var/postgres stop ぽすぐれ終了

終わっているかどうかの確認は $ psql -l

psql: error: 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"?

となれば接続が切れている

つまづいたところ ずっと、他のサイトを見ながらインストールと起動を試みていたのだけど、 pg_ctl -D /usr/local/var/postgres -l logfile startすると

$ pg_ctl -D /usr/local/var/postgres -l logfile start

pg_ctl: another server might be running; trying to start server anyway
waiting for server to start.... stopped waiting
pg_ctl: could not start server
Examine the log output.

のエラーが出ていて、どうやら後ろで何かが動いているということだけはニュアンスで掴み取り 色々試しました。

$ brew services stop postgresqlしたら

Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

よくわからないけど解決した!もともと開いてたものを閉じられたらしい… ただ、なぜかはよくわからないので、質問しよう。

他お世話になった記事

qiita.com