LoginSignup
4
1

More than 1 year has passed since last update.

Rails6でデータベースをpostgreSQLに変更したその後のエラー

Posted at

開発環境

Railsアプリケーションの開発
Rails ver. 6.1.3
Ruby ver. 2.6.3
postgreSQL ver. 13.2

エラー①

Rails6ではデータベースの変更がコマンド1つでできるので、
こちらの方の記事を参考に
https://qiita.com/vinaka/items/fbdb17bebe75d40de995

Herokuへデプロイする予定なのでデータベースをdevelopment、production含め
一括でpostgreSQLに変更しようと考えた
以下のコマンドを実行し、bundle install

$ rails db:system:change --to=postgresql
$ bundle install --clean

railsサーバーを起動すると

ActiveRecord::ConnectionNotEstablished (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"?
):

このようなエラーが発生

エラー文で調べてみるとQiitaの記事がヒット
https://qiita.com/masayuki14/items/27ca4764d0bcae4affea

これを参考に
config/application.rbにて記述を追加

config/application.rb
require_relative "boot"

require "rails"

# 以下4行を追加
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "active_storage/engine"
.
.

それでも解決しなかったので
こちらの記事に辿り着き
https://qiita.com/hirocueki2/items/327dc6e87005edf622ad

試行錯誤の末、、

$ postgres -V
postgres (PostgreSQL) 13.2

$ brew postgresql-upgrade-database
Error: postgresql data already upgraded!

$ rm /usr/local/var/postgres/postmaster.pid

$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

無事に上記のエラーは解決

エラー②

その後rails db:migrateを実行すると新たなエラーが

ActiveRecord::NoDatabaseError (FATAL:  database "datebase_development" does not exist
):

これはデータベースがないがために起こるエラーのようでrails db:createを実行

rails db:create
Created database 'database_development'
Created database 'database_test'

コマンドの実行によりデータベースは作成されたようで

$ rails db:migrate
== 20210711110107 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0177s
== 20210711110107 CreateUsers: migrated (0.0178s) =============================

rails db:migrateに成功!

困ってる人の助けになれれば幸いです。
間違い・ご指摘等ありましたら教えてください。

4
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
4
1