LoginSignup
2
1

More than 3 years have passed since last update.

【PostgreSQL】 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"

Last updated at Posted at 2020-11-02

はじめに

久しぶりにローカル環境で Rails を起動しようとするとタイトルに書いた PostgreSQL のエラーが出て仕事ができず困ったのでその時のメモです。

Railsを起動しようとすると以下のエラーが発生

$ rails s
...(中略)...
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"?

以下いろいろ試した記録

1. PostgreSQLにログインできるか試してみる

やはり同じエラーが発生

$ psql posrgres
...(中略)...
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"?

結果 → ×

2. PostgreSQLを再起動してみる

$ brew services restart postgresql@11 

結果 → ×

3. データディレクトリを削除してみる

( 注: データが消えます )

$ rm -rf /usr/local/var/postgres

結果 → ×

4. PostgreSQLをアンインストール→インストールしてみる

アンインストール

brew uninstall --force postgresql@11

インストール

brew install postgresql@11

結果 → PostgreSQL にはログインできるようになったが、rails server, rails db:create, rails db:migrate などを実行するとタイトルと同じエラーがまだ出る

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"?

5. リンクをつけてみる

エラー文に書かれていたファイルにソケットファイルのリンクを付与

$sudo mkdir /var/pgsql_socket/
$sudo ln -s /private/tmp/.s.PGSQL.5432 /var/pgsql_socket

rails db:createを再実行

結果 → 失敗したが、エラー文が変わった

dyld: lazy symbol binding failed: Symbol not found: _PQresultMemorySize
  Referenced from: /Users/dev/.rvm/gems/ruby-2.5.7/gems/pg-1.2.2/lib/pg_ext.bundle
  Expected in: /usr/lib/libpq.5.dylib

dyld: Symbol not found: _PQresultMemorySize
  Referenced from: /Users/dev/.rvm/gems/ruby-2.5.7/gems/pg-1.2.2/lib/pg_ext.bundle
  Expected in: /usr/lib/libpq.5.dylib

6. PostgreSQLのgemを再インストール

Gemfile から gem 'pg' をコメントアウトし bundle update
→ gem 'pg' アンコメントして bundle install

rails db:createを実行

結果 → 成功!

rails db:migrate rails server も通るようになった

おわりに

原因はいまだにわかっておらず、上記で試したことのうち何が幸いして直ったのかは不明です...
Railsを起動してない間に brew で色々とインストールをしていたのでその影響かなとは考えています。

時間ができればまたこのあたりの仕組みを見直そうと思います。

参考記事

postgresqlでDBが作れなかったのを解決(してもらった)
https://haayaaa.hatenablog.com/entry/2019/01/23/111745

Homebrew でインストールした postgresql に接続できないときの原因と対処法
https://blog.kyanny.me/entry/2015/01/16/024304

"/var/pgsql_socket/.s.PGSQL.5432"?
https://ameblo.jp/soft3133/entry-11466406890.html

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1