こんにちは。Takitaです。
久々にpostgreを使ったプロジェクトの準備をしていたら、PG::ConnectionBad: could not connect to server: No such file or directory と怒られたので対応しました。
$ bin/setup
== Installing dependencies ==
The Gemfile's dependencies are satisfied
== Preparing database ==
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"?
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"app_name_development"}
rails aborted!
PG::ConnectionBad: 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"?
PCが正常終了しなかったため、postgreが自身のpidファイルを削除できず、古いpidをもとにpostgreのDBを見に行こうとしたけど、接続できませんよ、という感じみたいです。
macOS、brewでpostgreを入れている場合は、以下で対応できます。(ディレクトリと再起動するという手順はosやライブラリ管理ツールには依存しませんが)
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql