connections on Unix domain socket "/tmp/.s.PGSQL.5432"? を解決する

PostgreSQLMacで使おうとした時に以下のエラーが出た時の対処法

psqlコマンドを打つと以下のようなエラーが出ました。

$ psql -l
psql: error: could not connect to server: 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"?

ググったところ、servicesを使ってPostgreSQLを再起動すると解決できる記事を見つけ、再起動を試して見ましたが、変わらず同じエラーが出ています。

ログにヒントがないか確認したところ、データベースのファイルがサーバーと互換性がないと書かれていました。

$ postgres -D /usr/local/var/postgres/
2020-06-02 11:26:49.560 JST [33160] FATAL:  database files are incompatible with server

以下のコマンドでアップグレードしたところ動くようになりました。

$ brew postgresql-upgrade-database
$ psql -l
                                          データベース一覧
   名前    |  所有者   | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限      
-----------+-----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres  | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | 
 template0 | postgres  | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |           |                  |             |                   | postgres=CTc/postgres
 template1 | postgres  | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |           |                  |             |                   | postgres=CTc/postgres
 家計簿    | user1 l | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | 
(4 行)

参考

make-from-scratch.com

PostgreSQL DB のアップグレードは brew postgresql-upgrade-database が便利 - Qiita