質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1464閲覧

RailsのNoDatabaseError問題

sakanafuto

総合スコア0

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/06/29 02:11

前提・実現したいこと

Railsでwebアプリを作成中、いつもどおりbundle exec rails db:migrate:resetからのbundle exec rails db:seedをしようとしたところエラーが発生しました。先程まではスムーズにリセットも出来ていました。
2時間調べても解決できなかったのでここで質問させていただきます。

環境

  • ruby 2.6.5
  • rails 5.2.4.2
  • postgresql 12.3

発生している問題・エラーメッセージ

 

❯ bundle exec rails db:migrate:reset rails aborted! ActiveRecord::NoDatabaseError: FATAL: database "stelle_development" does not exist /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:696:in `rescue in connect' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:691:in `connect' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize' ・・・省略・・・ /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate:reset => db:drop => db:load_config => environment (See full trace by running task with --trace)

となったため、直接psqlからdbをCreateし再度試したところ

❯ bundle exec rails db:migrate:reset rails aborted! ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist LINE 8: WHERE a.attrelid = '"users"'::regclass ^ : SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = '"users"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:63:in `exec' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:63:in `block (2 levels) in query' ・・・省略・・・ /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:migrate:reset => db:drop => db:load_config => environment (See full trace by running task with --trace)

と怒られてしまいました。
その後rails db:migrate, db:migrate:reset, db:createをしても似たようなメッセージを吐き出す次第です。

該当のソースコード

databaseyml

1 PostgreSQL. Versions 9.1 and up are supported. 2# 3# Install the pg driver: 4# gem install pg 5# On OS X with Homebrew: 6# gem install pg -- --with-pg-config=/usr/local/bin/pg_config 7# On OS X with MacPorts: 8# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 9# On Windows: 10# gem install pg 11# Choose the win32 build. 12# Install PostgreSQL and put its /bin directory on your path. 13# 14# Configure Using Gemfile 15# gem 'pg' 16# 17default: &default 18 adapter: postgresql 19 encoding: unicode 20 # For details on connection pooling, see Rails configuration guide 21 # http://guides.rubyonrails.org/configuring.html#database-pooling 22 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 23 24development: 25 <<: *default 26 database: stelle_development 27 28 # The specified database role being used to connect to postgres. 29 # To create additional roles in postgres see `$ createuser --help`. 30 # When left blank, postgres will use the default role. This is 31 # the same name as the operating system user that initialized the database. 32 #username: stelle 33 34 # The password associated with the postgres role (username). 35 #password: 36 37 # Connect on a TCP socket. Omitted by default since the client uses a 38 # domain socket that doesn't need configuration. Windows does not have 39 # domain sockets, so uncomment these lines. 40 #host: localhost 41 42 # The TCP port the server listens on. Defaults to 5432. 43 # If your server runs on a different port number, change accordingly. 44 #port: 5432 45 46 # Schema search path. The server defaults to $user,public 47 #schema_search_path: myapp,sharedapp,public 48 49 # Minimum log levels, in increasing order: 50 # debug5, debug4, debug3, debug2, debug1, 51 # log, notice, warning, error, fatal, and panic 52 # Defaults to warning. 53 #min_messages: notice 54 55# Warning: The database defined as "test" will be erased and 56# re-generated from your development database when you run "rake". 57# Do not set this db to the same as development or production. 58test: 59 <<: *default 60 database: stelle_test 61 62# As with config/secrets.yml, you never want to store sensitive information, 63# like your database password, in your source code. If your source code is 64# ever seen by anyone, they now have access to your database. 65# 66# Instead, provide the password as a unix environment variable when you boot 67# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 68# for a full rundown on how to provide these environment variables in a 69# production deployment. 70# 71# On Heroku and other platform providers, you may have a full connection URL 72# available as an environment variable. For example: 73# 74# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 75# 76# You can use this database configuration with: 77# 78# production: 79# url: <%= ENV['DATABASE_URL'] %> 80# 81production: 82 <<: *default 83 database: stelle_production 84 username: stelle 85 password: <%= ENV['STELLE_DATABASE_PASSWORD'] %> 86

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

pg でdatabaseをdropして、
rails db:create
rails db:migrate

してみてください

投稿2020/06/29 09:02

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakanafuto

2020/06/29 13:38

postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+-------+----------+---------+-------+------------------- postgres | futo | UTF8 | C | C | template0 | futo | UTF8 | C | C | =c/futo + | | | | | futo=CTc/futo template1 | futo | UTF8 | C | C | =c/futo + | | | | | futo=CTc/futo stelleという名のアプリケーションなのですがそれに関するdbを全てdropして見たのですが全く同じNoDatabaseErrorを吐き出すだけでした。
winterboum

2020/06/29 13:51 編集

dbを全てdrop したあと、どういう コマンドを発行したか、そのときどういうレスポンスが有ったか書いてください
sakanafuto

2020/06/29 14:06

=#drop database stelle_development; =#drop database stelle_test; をpsqlでした後、stelle(Railsのルートディレクトリ)上でralis db:createをすると次のようになってしまいました、、、。 ❯ rails db:create rails aborted! ActiveRecord::NoDatabaseError: FATAL: database "stelle_development" does not exist /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:696:in `rescue in connect' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:691:in `connect' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activerecord-5.2.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
sakanafuto

2020/06/29 14:09

後半のエラー文です。 /Users/futo/stelle/config/environment.rb:5:in `<main>' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `block in require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/application.rb:337:in `require_environment!' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/application.rb:520:in `block in run_tasks_blocks' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/commands/rake/rake_command.rb:20:in `perform' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/command.rb:48:in `invoke' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/railties-5.2.4.2/lib/rails/commands.rb:18:in `<main>' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/bootsnap-1.4.6/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `block in require' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:257:in `load_dependency' /Users/futo/stelle/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.2/lib/active_support/dependencies.rb:291:in `require' bin/rails:4:in `<main>' Tasks: TOP => db:create => db:load_config => environment (See full trace by running task with --trace)
winterboum

2020/06/29 14:11

なんと!! これは私の理解を超えてる。 ごめんなさい、です。
sakanafuto

2020/06/29 14:37

いえいえ!わざわざご回答ありがとうございました。 まだ頑張ってみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問