ActiveRecord :: NoDatabaseError:致命:数据库" gvpmahesh"不存在

时间:2016-07-28 07:44:05

标签: ruby-on-rails

我正在尝试运行rake test但我收到此错误

rake aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "gvpmahesh" does not exist
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:87:in `connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:857:in `needs_migration?'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:396:in `load_schema_if_pending!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:411:in `block in maintain_test_schema!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:642:in `suppress_messages'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:416:in `method_missing'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_help.rb:19:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
/home/gvpmahesh/code/rails/depot/test/test_helper.rb:3:in `<top (required)>'
/home/gvpmahesh/code/rails/depot/test/controllers/products_controller_test.rb:1:in `require'
/home/gvpmahesh/code/rails/depot/test/controllers/products_controller_test.rb:1:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'
PG::ConnectionBad: FATAL:  database "gvpmahesh" does not exist
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:87:in `connection'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:857:in `needs_migration?'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:396:in `load_schema_if_pending!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:411:in `block in maintain_test_schema!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:642:in `suppress_messages'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:416:in `method_missing'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:411:in `maintain_test_schema!'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_help.rb:19:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
/home/gvpmahesh/code/rails/depot/test/test_helper.rb:3:in `<top (required)>'
/home/gvpmahesh/code/rails/depot/test/controllers/products_controller_test.rb:1:in `require'
/home/gvpmahesh/code/rails/depot/test/controllers/products_controller_test.rb:1:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `require'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (3 levels) in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `each'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:114:in `block (2 levels) in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:113:in `each'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:113:in `block in define'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/sub_test_task.rb:20:in `invoke_rake_task'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/railties-4.2.5.1/lib/rails/test_unit/testing.rake:8:in `block in <top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval'
/home/gvpmahesh/.rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:run
(See full trace by running task with --trace)

我在database.yml

中有这个
development: &default
  adapter: postgresql
  encoding: unicode
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_NAME'] %>
  pool: <%= ENV['DB_POOL'] %>

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: postgresql
  encoding: unicode
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_TEST'] %>
  pool: <%= ENV['DB_POOL'] %>

staging:
  <<: *default

production:
  <<: *default
事情是开发数据库似乎工作正常。

当我做rake db:create:all

depot_development already exists
depot_test already exists
depot_development already exists
depot_development already exists

我也尝试了bundle exec rake db:create bin/rake db:create

1 个答案:

答案 0 :(得分:1)

您在rake命令中创建了depot_test

但是当您运行测试时,gvpmahesh中有ENV["DB_TEST"]

所以一个好方法是:将测试数据库配置设置为静态,而不是通过ENV变量。因为这不是敏感信息。 (对于git checkouts,例如。)

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5
  timeout: 5000
  # You can add here your credentials

development:
  <<: *default
  database: depot_development

test:
  <<: *default
  database: depot_test

production:
  <<: *default
  database: depot_production

优点:如果您在另一个系统上开发/测试,则不必设置所有DB ENV变量并开始使用