7
7

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.

PG::ObjectInUse: ERROR: database "myapp_development" is being accessed by other users

Last updated at Posted at 2020-07-06

ポートフォリオ作成中

db/seeds.rb
User.create!(name: "テスト 太郎",
             email: "test@example.com",
             password: "foobar",
             password_confirmation: "foobar"
)

99.times do |n|
  name = Faker::Name.name
  email = "test#{n}@example.com"
  password = "foobar"
  User.create!(name: name,
               email: email,
               password: password,
               password_confirmation: password)
end

ページネーションを実装していくため
テスト用アカウントを作成しようとすると、
こんなエラーが出ました。

$ rails db:migrate:reset

を実行すると

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

「myapp_debelopment」データベースは、他のセッションで使われているとのこと。

データベースはこのような構成

config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password: password
  pool: 5

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

色々と調べていく中で
下記を参考にして、データベースを一度削除して
作成し直すことにしました。
https://stackoverflow.com/questions/46451472/unable-to-make-rake-dbdrop-work-in-development-with-rails-5-1-4

$ docker exec -it (コンテナ名) /bin/bash

コンテナの中に入ります。

$ rails db:drop
Dropped database 'myapp_development'
Dropped database 'myapp_test'
$ rails db:create
$ rails db:migrate
== 20200704040832 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0426s
== 20200704040832 CreateUsers: migrated (0.0432s) =============================

== 20200704043311 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
   -> 0.0303s
== 20200704043311 AddIndexToUsersEmail: migrated (0.0307s) ====================

== 20200704043511 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string)
   -> 0.0026s
== 20200704043511 AddPasswordDigestToUsers: migrated (0.0031s) ================

== 20200705055948 AddRememberDigestToUsers: migrating =========================
-- add_column(:users, :remember_digest, :string)
   -> 0.0025s
== 20200705055948 AddRememberDigestToUsers: migrated (0.0027s) ================
$ rails db:seed
$ exit

これで、無事動きました!

おそらく、
最初にデータベースを作成した際に
コンテナに入って作成するのではなく、
ローカル環境から下記コマンドで実行したから?
作成したユーザーが異なってしまったのではないかと。。

$ docekr-compose run web rails db:create

追記 (2020/7/20)

上記で書いたのは都度、データベースを削除する方法でしたが
下記方法でも行えることがわかりました。

$ docker container ls

動作しているコンテナを確認

3c9b72845b89   postgres  "docker-entrypoint.s…" 8 days ago   Up 6 days   5432/tcp    

コンテナのIDを指定して、コンテナを停止させる。

$ docker container stop 3c9b72845b89

あとは、本来やりたかった内容を実行するだけ

$ rails db:migrate:reset

== 20200704040832 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0426s
== 20200704040832 CreateUsers: migrated (0.0432s) =============================

== 20200704043311 AddIndexToUsersEmail: migrating =============================
-- add_index(:users, :email, {:unique=>true})
   -> 0.0303s
== 20200704043311 AddIndexToUsersEmail: migrated (0.0307s) ====================

== 20200704043511 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string)
   -> 0.0026s
== 20200704043511 AddPasswordDigestToUsers: migrated (0.0031s) ================

== 20200705055948 AddRememberDigestToUsers: migrating =========================
-- add_column(:users, :remember_digest, :string)
   -> 0.0025s
== 20200705055948 AddRememberDigestToUsers: migrated (0.0027s) ================
$ rails db:seed

そして、停止させていたコンテナを動かす。

$ docker container start 3c9b72845b89

これで大丈夫です!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?