LoginSignup
0
0

More than 1 year has passed since last update.

【Rails】rails db:migrateが実行できない / PG::DuplicateTable: ERROR: relation "users" already exists / PG::ObjectInUse: ERROR: database "scaffold_app_development" is being accessed by other users

Last updated at Posted at 2022-08-02

前提

Rails 6.1.6.1
ruby 2.6.6
database postgresql

実現したいこと

rails db:migrateをエラーなく、実行する。

エラー内容の確認

rails db:migrateを実行したところ、次のようなエラーが出ました。

rails db:migrate
Running via Spring preloader in process 31813
== 20220802023637 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "users" already exists

略

Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR:  relation "users" already exists

エラー内容を見ると、「作ろうとしているusersはもうあるよ」ということでした。

いったん、rails db:resetを行おうとしてみたところ、次のようなエラーが出ました。

 rails db:reset
Running via Spring preloader in process 32451
PG::ObjectInUse: ERROR:  database "scaffold_app_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.
Couldn't drop database 'scaffold_app_development'
rake aborted!
ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  database "scaffold_app_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.

略

Caused by:
PG::ObjectInUse: ERROR:  database "scaffold_app_development" is being accessed by other users
DETAIL:  There is 1 other session using the database.

Tasks: TOP => db:drop:_unsafe
(See full trace by running task with --trace)

略

「他のユーザーがアクセスしてるからいじれません、他のセッションがDBを使っている」といった内容のエラーが出てきました。
「セッションが残っている」というところに目をつけて、データベースにアクセスしているユーザを確認することにしました。

DBにアクセスしているユーザーの確認

psql scaffold_app_development

データベース名の確認方法(database.yml参照)

development:
  <<: *default
  database: scaffold_app_development <= ココ!!

実際に、アクセス状況を確認する。

psql scaffold_app_development

psql (14.4)
Type "help" for help.

scaffold_app_development=# select * from pg_stat_activity;

略

(7 rows)

datnameにデータベース名が記載されているところがDBにアクセスしているユーザーになるので、その箇所を削除する。
pidを削除することでアクセスしているユーザーも削除できる。
こんな感じ。

kill 32414(pidに記載されている数字)

killコマンド実行後、再度psql データベース名(database.yml内に記載されている)を実行してみると、

psql scaffold_app_development
scaffold_app_development=# select * from pg_stat_activity;
server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
The connection to the server was lost. Attempting reset: Succeeded.

データベースにアクセスしているユーザーがいないことを確認した上で、rails db:resetを実行すると、

 rails db:reset
Running via Spring preloader in process 32553
Dropped database 'scaffold_app_development'
Dropped database 'scaffold_app_test'
Created database 'scaffold_app_development'
Created database 'scaffold_app_test'
略

問題なく実行できました。

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