はじめに
なんとなく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 して対応しましたが、
きっと別の方法で解決することが望ましいと思います。
ですが、この記事が少しでも誰かの役にたてば良いなと思います。