なんかいやってもpostgresqlが動かない

何度目になるのかわからないぐらい postgresql が動かない。
何度も動かないのに反省していない自分がアホなので、今度こそメモする。
完全に自分で作った罠に自分でひっかかっているとういう話なので、参考にはならないと思います。

いつもどおり brew でインストールする
そして長々とログをベタ貼りする。

```
$ brew install postgresql
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
fasttext
==> Updated Formulae
pcre2 ✔                                 hugo                                    istioctl                                lz4
gmic                                    ios-webkit-debug-proxy                  libuv                                   zstd

==> Installing dependencies for postgresql: readline
==> Installing postgresql dependency: readline
==> Downloading https://homebrew.bintray.com/bottles/readline-8.0.0.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/9857a88bdb1a8ec96b986e99084ea823541d357d451a70a028e7c30410f918e9--readline-8.0.0.mojave.bottle.tar.gz
==> Pouring readline-8.0.0.mojave.bottle.tar.gz
==> Caveats
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> Summary
🍺  /usr/local/Cellar/readline/8.0.0: 48 files, 1.5MB
==> Installing postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.2.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/991d29978b1e0603895b59851c553e41370fefedbb67797b6078ead33bccdba2--postgresql-11.2.mojave.bottle.tar.gz
==> Pouring postgresql-11.2.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
==> 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.2: 3,186 files, 35.4MB
==> Caveats
==> readline
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

==> postgresql
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
```

ログにかかれているとおり、 readline 用の環境変数を設定します。

```
$ echo -e "export LDFLAGS=\"-L/usr/local/opt/readline/lib\"\nexport CPPFLAGS=\"-I/usr/local/opt/readline/include\"\nexport PKG_CONFIG_PATH=\"/usr/local/opt/readline/lib/pkgconfig\"" >> ~/.bash_profile
$ source ~/.bash_profile
```

brew services 経由で postgres を起動する。

```
$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
$ brew services list
Name       Status  User       Plist
mysql@5.6  stopped
postgresql started ninjaninja /Users/ninjaninja/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
redis      started ninjaninja /Users/ninjaninja/Library/LaunchAgents/homebrew.mxcl.redis.plist
```

postgresql が started になっている（・｀ω・´）

```
$ psql
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"?
```

はい。でました。いつものエラー。毎回みるわー。反省してないわー。アホだな自分ー。解決していきましょう。

https://www.postgresql.org/docs/11/app-psql.html

ふむふむ。 

> --host が / ではじまると、 Unix-domain socket で接続するからね。

っていうことで解決方法は ```--host localhost``` ってオプションつければ、強制的にTCP/IP経由で接続するとのこと。 Stackoverflow で教えてもらいました。

```
$ psql -hlocalhost
psql: FATAL:  role "ninjaninja" does not exist
```

ああ、ユーザー指定しないとですな。

```
$ psql -hlocalhost -Upostgres
dyld: lazy symbol binding failed: Symbol not found: _PQsetErrorContextVisibility
  Referenced from: /usr/local/opt/postgresql/bin/psql
  Expected in: /usr/lib/libpq.5.dylib

dyld: Symbol not found: _PQsetErrorContextVisibility
  Referenced from: /usr/local/opt/postgresql/bin/psql
  Expected in: /usr/lib/libpq.5.dylib

Abort trap: 6
```

なんだろうこのエラー:(；ﾞﾟ'ωﾟ'）:
世界のお友達に聞いても全然当てはまらない。どういうことだー。そしてもう一度インストール時のログを見返すと。

```
==> Pouring postgresql-11.2.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.
```

エラー出てるやんけ。ちゃんと見とけよ。

> /usr/local/include is not writable.

んー。うちの環境に /usr/local/inclde がないとな?!
これは、自分の過去の記事のせいで発生した問題ですね:(；ﾞﾟ'ωﾟ'）:

https://qiita.com/ninjaninja/items/993781635b2ba8672aa6

この対応で、 /usr/local/include を /usr/local/include_old に mv してました。

ということで /usr/local/include を戻すんではなく、作り直します。 rbenv 動かなくなっちゃうし。

```
$ sudo mkdir /usr/local/include
$ sudo chown ninjaninja:admin /usr/local/include
```

そして postgresql インストールしなおし。

```
$ brew uninstall postgresql
Uninstalling /usr/local/Cellar/postgresql/11.2... (3,186 files, 35.4MB)

$ brew install postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.2.mojave.bottle.tar.gz
Already downloaded: /Users/ninjaninja/Library/Caches/Homebrew/downloads/991d29978b1e0603895b59851c553e41370fefedbb67797b6078ead33bccdba2--postgresql-11.2.mojave.bottle.tar.gz
==> Pouring postgresql-11.2.mojave.bottle.tar.gz
==> 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.2: 3,186 files, 35.4MB
```

ｼｬｰｵﾗｰ! 今度こそエラーなし。

```
$ psql -hlocalhost -Upostgres
psql (11.2)
Type "help" for help.

postgres=#
```

めでたくアクセス成功。本当に自分はアホだなと思いました。