PG::ConnectionBad: could not connect to server: Connection refusedと怒られてからrails serverを立ち上げるまで

rails serverを立ち上げようとしたら

```
$rails s
I, [2019-09-16T13:57:17.832791 #29176]  INFO -- sentry: ** [Raven] Raven 2.7.1 configured not to capture errors: Not configured to send/capture in environment 'development'
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
	Is the server running on host "localhost" (127.0.0.1) and accepting
	TCP/IP connections on port 5432?
```
と怒られた。postgresが正常に終了しなかったことが原因らしい。
ちなみにこの状態だと `db:migrate` もできないはず。
今回はrails serverを再び立ち上げるまでにやったことをここに記す。

## 私の環境
MacOS Mojave v10.14.5
postgres (PostgreSQL) 11.5
ruby 2.6.4
Rails 6.0.0

## やったことまとめ
postgresqlを再インストールして、

```
$brew reinstall postgres
```

pidを消し、

```
$rm /usr/local/var/postgres/postmaster.pid
```

postgresを停止して、

```
$launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgesql.plist
```

postgresを起動して、

```
$pg_ctl -D /usr/local/var/postgres start
```

dbを作成して、

```
$rails db:drop
$rails db:create
```

rails serverを立ち上げた。


## やったことひとつずつ

ググると [postgres をちゃんと終了しないと.pidファイルが残っ...](https://gist.github.com/taea/8865831) がヒットするので、参考にやってみた。

##### 1. postgresの生存確認

```
$ps aux | grep postgres
you             29213   0.0  0.0  4268040    780 s000  S+    1:57PM   0:00.00 grep postgres
```
確かに死んでいそう。

##### 2. postgresの身元確認

```
$tail /usr/local/var/postgres/server.log
tail: /usr/local/var/postgres/server.log: No such file or directory
```
でた、ノーサッチファイルオアディレクトリ :cry:
念のため次の手順もやってみる。

##### 3. pidをファイルを消す

```
$rm /usr/local/var/postgres/postmaster.pid
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
```
案の定、ないと言われた。

##### 4. postgresのversionを確認してみる

```
$postgres --version
dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib
  Referenced from: /usr/local/bin/postgres
  Reason: image not found
Abort trap: 6
```
なんか怒られた。

ググると [npmのエラー解決「dyld: Library not...](https://qiita.com/SuguruOoki/items/3f4fb307861fcedda7a5) や [node dyld: Library not loaded...](https://k-sasaki.net/2018/04/382/) がヒットする。
なんか再インストールをするとよさそう！

##### 5. postgresqlを再インストール

```
$brew reinstall postgres
==> Reinstalling postgresql 
==> Installing dependencies for postgresql: openssl@1.1 and readline
.
.
.
==> 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
```
できた。

##### 6. postgresを起動する(最終行↑を実行)

```
$pg_ctl -D /usr/local/var/postgres start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2019-09-16 14:27:05.646 JST [47660] FATAL:  lock file "postmaster.pid" already exists
2019-09-16 14:27:05.646 JST [47660] HINT:  Is another postmaster (PID 47153) running in data directory "/usr/local/var/postgres"?
 stopped waiting
pg_ctl: could not start server
Examine the log output.
```
また怒られた。

しかし `lock file "postmaster.pid" already exists` に既視感。消せばよさそう！

##### 7. pidをファイルを消す(3を再び)
[postgres をちゃんと終了しないと.pidファイルが残っ...](https://gist.github.com/taea/8865831) を参考にした。

```
$rm /usr/local/var/postgres/postmaster.pid
$launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgesql.plist
```
消してみた。

##### 8. もう一度postgresを起動してみる(6を再び)

```
$pg_ctl -D /usr/local/var/postgres start
waiting for server to start....2019-09-16 14:28:53.989 JST [47751] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-09-16 14:28:53.989 JST [47751] LOG:  listening on IPv6 address "::1", port 5432
2019-09-16 14:28:53.990 JST [47751] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-09-16 14:28:54.001 JST [47752] LOG:  database system was shut down at 2019-09-16 14:28:49 JST
2019-09-16 14:28:54.005 JST [47751] LOG:  database system is ready to accept connections
 done
server started
```
いけたかも？

##### 9. postgresの生存確認(1を再び)

```
$ps aux | grep postgres
you             47787   0.0  0.0  4277256    800 s000  S+    2:29PM   0:00.00 grep postgres
you             47758   0.0  0.0  4481240   1604   ??  Ss    2:28PM   0:00.00 postgres: logical replication launcher     
you             47757   0.0  0.0  4336108    804   ??  Ss    2:28PM   0:00.00 postgres: stats collector     
you             47756   0.0  0.0  4481240   1792   ??  Ss    2:28PM   0:00.00 postgres: autovacuum launcher     
you             47755   0.0  0.0  4472984   5100   ??  Ss    2:28PM   0:00.01 postgres: walwriter     
you             47754   0.0  0.0  4481176   1356   ??  Ss    2:28PM   0:00.01 postgres: background writer     
you             47753   0.0  0.0  4472984    924   ??  Ss    2:28PM   0:00.00 postgres: checkpointer     
you             47751   0.0  0.1  4481456  18300 s000  S     2:28PM   0:00.03 /usr/local/Cellar/postgresql/11.5_1/bin/postgres -D /usr/local/var/postgres
```
いけた！

##### 10.ついにrails serverを立ち上げてみる

```
$rails s
I, [2019-09-16T14:32:25.292908 #47859]  INFO -- sentry: ** [Raven] Raven 2.7.1 configured not to capture errors: Not configured to send/capture in environment 'development'
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
2019-09-16 14:32:27.822 JST [47878] FATAL:  database "database_name" does not exist
.
.
.
/Users/you/project/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:50:in `rescue in postgresql_connection': FATAL:  database "database_name" does not exist (ActiveRecord::NoDatabaseError)
```
そんなdatabaseはないと言われた。

##### 11. dbを作成する

```
$rails db:drop
I, [2019-09-16T14:33:13.326011 #47928]  INFO -- sentry: ** [Raven] Raven 2.7.1 configured not to capture errors: Not configured to send/capture in environment 'default'
2019-09-16 14:33:14.644 JST [47947] FATAL:  database "database_name" does not exist
Dropped database 'database_name'

$rails db:create
I, [2019-09-16T14:33:38.979597 #47986]  INFO -- sentry: ** [Raven] Raven 2.7.1 configured not to capture errors: Not configured to send/capture in environment 'default'
Created database 'database_name'
```

##### 12. 再びrails serverを立ち上げる

```
$rails s
I, [2019-09-16T14:34:04.421763 #48044]  INFO -- sentry: ** [Raven] Raven 2.7.1 configured not to capture errors: Not configured to send/capture in environment 'development'
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 3.11.0 (ruby 2.6.4-p104), codename: Love Song
* Min threads: 4, max threads: 4
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
```

いけた！ :clap: