2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ PostgreSQL ] connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?

Last updated at Posted at 2020-12-11

はじめに

なんとなくRailsアプリで遊ぼうと思い
Railsアプリの雛形を作成した後に rails db:create としたところ
エラーが発生したので、その解決までを備忘録として書きたいと思います。
ちなみにですが、最終的にuninstallしており正攻法では無いのでご注意ください。

rails db:create 失敗

使用DBはタイトルにもある通りPostgreSQLです。
rails db:create としたところ以下のエラーが発生しました。

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"?
Couldn't create 'sample_app' database. Please check your configuration.
rake 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 "/var/pgsql_socket/.s.PGSQL.5432"?

ファイルが無くて接続が出来ない。みたいなことを言われている気がします。
とりあえず、ログを見てみることにします。

ログファイル(環境によって異なる場合があります) /usr/local/var/log/postgres.log

$ cat /usr/local/var/log/postgres.log

FATAL:  database files are incompatible with server
DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0

どうやらデータディレクトリが version 12 なのに対して version 13.0を使用しているからダメだよ。的な感じでしょうか。
使用したいversionは今回13.0なので、そちらに合わせたいのですが
私の場合データディレクトリを削除しても、特に問題は無かったため以下のコマンドで削除しました。

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

$ brew uninstall --force postgresql

再インストール

先ほどのコマンドで削除したので、改めてインストールしていきます。

$ brew install postgres

$ initdb /usr/local/var/postgresql -E utf8

そして、DBを起動し

$ rails db:create

Created database 'sample_app'
Created database 'sample_app_test'

うまくいきました。

おわりに

今回はデータディレクトリと使用バージョンの不一致からこのようなエラーが発生しました。
私は uninstall して対応しましたが、
きっと別の方法で解決することが望ましいと思います。

ですが、この記事が少しでも誰かの役にたてば良いなと思います。

2
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?