MacにPostgreSQLをインストールした際で詰まった箇所

HomebrewでPostgreSQLをインストールをしようとしたのですが、権限周りなどで詰まったので、備忘録として残しています。
自分がPostgreSQLをインストールする際に参考にしたURLも張っているので、併せて確認して頂ければです。


## HomebrewでPostgreSQLをインストール
```
$　brew install postgresql

==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.1.mojave.bottle.tar.gz
Already downloaded: /Users/suzuki_naoto/Library/Caches/Homebrew/downloads/0c14020ff4817a892c73344b81dbd6f31e0d6e83ca0778de6fe3b4d4ecba7a59--postgresql-11.1.mojave.bottle.tar.gz
==> Pouring postgresql-11.1.mojave.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/ecpg_config.h
/usr/local/include is not writable.

You can try again using:
  brew link postgresql
==> /usr/local/Cellar/postgresql/11.1/bin/initdb /usr/local/var/postgres
Last 15 lines from /Users/suzuki_naoto/Library/Logs/Homebrew/postgresql/post_install.01.initdb:
2019-01-03 15:55:57 +0900

/usr/local/Cellar/postgresql/11.1/bin/initdb
/usr/local/var/postgres

The files belonging to this database system will be owned by user "suzuki_naoto".
This user must also own the server process.

initdb: file "/usr/local/share/postgresql/postgres.bki" does not exist
This might mean you have a corrupted installation or identified
the wrong directory with the invocation option -L.
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall postgresql`
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
🍺  /usr/local/Cellar/postgresql/11.1: 3,548 files, 40.3MB
```
> Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/ecpg_config.h
/usr/local/include is not writable.
You can try again using:
  brew link postgresql

このエラーはシンボリックリンクが作成できないとのこと。
※シンボリックリンクとはOSのファイルシステムの機能の一つで、特定のファイルやディレクトリを指し示す別のファイルを作成し、それを通じて本体を参照できるようにする仕組み。

エラーに記載してあるコマンドを実行

```
$ brew link postgresql

Linking /usr/local/Cellar/postgresql/11.1...
Error: Could not symlink include/ecpg_config.h
/usr/local/include is not writable.
```

/usr/local/include is not writableの書き込み権限がないとのこと
調べると[macOS High Sierra で Ansible 入れようとしたら失敗した。](https://qiita.com/_ag/items/1ea00c8575f8fe2d6c57)に行き着き下記を実行

```
$ sudo mkdir /usr/local/include
$ sudo chown -R $(whoami) $(brew --prefix)/*
```

再び、postgresqlをインストール

```
$ brew install postgresql

Warning: postgresql 11.1 is already installed, it's just not linked
You can use `brew link postgresql` to link this version.
```

Warningのコマンドを実行

```
$ brew link postgresql

Linking /usr/local/Cellar/postgresql/11.1... 392 symlinks created
```

最初の`brew install postgresql`で実行して出ていた、Warningのコマンドを実行

```
brew postinstall postgresql

==> Postinstalling postgresql
==> /usr/local/Cellar/postgresql/11.1/bin/initdb /usr/local/var/postgres
```

postgresのバージョン確認

```
$ postgres --version
postgres (PostgreSQL) 11.1
```

DBの初期化
localeをja_JP.UTF-8に指定

```
$ initdb /usr/local/var/postgres --encoding=UTF-8 --locale=ja_JP.UTF-8

The files belonging to this database system will be owned by user "suzuki_naoto".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

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".
```

DBの起動と確認

```
$ postgres -D /usr/local/var/postgres`
$ psql -l

                                        List of databases
   Name    |    Owner     | Encoding |   Collate   |    Ctype    |       Access privileges
-----------+--------------+----------+-------------+-------------+-------------------------------
 postgres  | suzuki_naoto | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | suzuki_naoto | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/suzuki_naoto              +
           |              |          |             |             | suzuki_naoto=CTc/suzuki_naoto
 template1 | suzuki_naoto | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/suzuki_naoto              +
           |              |          |             |             | suzuki_naoto=CTc/suzuki_naoto

```

# 参考URL
[MacにPostgreSQLをインストール](https://qiita.com/_daisuke/items/13996621cf51f835494b)
[macOS High Sierra で Ansible 入れようとしたら失敗した。](https://qiita.com/_ag/items/1ea00c8575f8fe2d6c57)
[ロケール(国際化と地域化)](https://lets.postgresql.jp/documents/technical/text-processing/2)
[PostgreSql コマンドの覚え書き](https://qiita.com/mm36/items/1801573a478cb2865242)