Homebrewを使用して、macOSにPostgreSQLをインストールしたので、その時の記録として残す。
環境
macOS (10.12.1)
PostgreSQL (9.6.1)
Homebrew (1.1.2)
Homebrewのアップデート
$ brew update
$ brew doctor
PostgreSQLのインストール
$ brew install postgresql
初期化
$ initdb /usr/local/var/postgres -E utf8
実行すると以下のようなメッセージが表示された。
指示に従って /usr/local/var/postgres
を削除してから再実行。
initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
(/usr/local/var/postgres
でエラーが発生するのは、HomebrewでPostgreSQLをインストールする際に内部でinitdbが実行されるかららしい)
今度は以下のようなワーニングが発生したが、今回はtrust認証で問題ないのでスルー。
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
※ trust認証については公式サイト参照。
https://www.postgresql.jp/document/9.6/html/auth-methods.html#auth-trust
起動
$ pg_ctl -D /usr/local/var/postgres -l logfile start
データベース一覧を取得してみる
$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
postgres | admin | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | admin | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/admin +
| | | | | admin=CTc/admin
template1 | admin | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/admin +
| | | | | admin=CTc/admin
(3 rows)
停止
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast
本当に停止したか確認してみる
先ほどのデータベース一覧取得を再度行ってみると停止していることが分かる。
$ psql -l
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"?
権限設定
postgresユーザにSuperuser権限を設定する。
$ psql -U admin postgres
# ALTER ROLE postgres WITH Superuser;
# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
admin | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser | {}
参考
http://tstomoki.com/programming/posgre_inst
http://qiita.com/_daisuke/items/13996621cf51f835494b