Just do IT

思うは招く

PG::ConnectionBad - could not connect to server: Connection refused エラーを解決

直面したエラー

rails sでサーバーを起動しようとしたら、こんなエラー画面が出てPostgreSQLが起動できない。

PG::ConnectionBad - could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
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?
:

解決

ポスグレをアップグレードすることで解決した。

$ brew postgresql-upgrade-database

原因:ポスグレのバージョンがおかしかったぽい。

$ pg_ctl -D /usr/local/var/postgres/ start
waiting for server to start....2021-01-05 14:17:23.415 JST [38017] FATAL:  database files are incompatible with server
2021-01-05 14:17:23.415 JST [38017] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.1.
 stopped waiting
pg_ctl: could not start server
Examine the log output.

他に試したが解決には至らなかったこと

postmaster.pidを削除しようとした。

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

no such file or ~が出て削除すらできなかった。

brew services restart postgresql

brew services restart postgresqlした。再起動はできたようだが解決はしなかった。

$ brew services restart postgresql        
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

参考