耙db:创build抛出“数据库不存在”错误postgresql

我在Debian 7下使用rails 4.1.5和postgresql 9.1,而且我无法在开发环境中创build数据库。 当我跑步

bin/rake db:create 

我明白了

 home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect': FATAL: database "direct-dev" does not exist Run `$ bin/rake db:create db:migrate` to create your database (ActiveRecord::NoDatabaseError) from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect' from ... 

我正在尝试创build数据库,所以它自然不存在。 不过铁轨应该创build它…这是我的config / database.yml:

 default: &default adapter: postgresql encoding: unicode pool: 5 development: <<: *default database: direct-dev 

这里是postgresql日志的一部分:

 2014-09-01 19:30:40 CEST LOG: connection received: host=[local] 2014-09-01 19:30:40 CEST LOG: connection authorized: user=rs database=direct-dev 2014-09-01 19:30:40 CEST FATAL: database "direct-dev" does not exist 

你有什么build议吗? 我已经在这里一个多小时了,仍然不明白为什么会发生这种情况…

谢谢!

Rails 4.1 带有弹簧预加载器 ,并且

新的Rails 4.1应用程序将配备“弹性”binstubs。 这意味着箱/轨和箱/耙将自动利用预加载的弹簧环境。

这意味着“弹性化”的bin/rake会尝试预载应用程序,而这又会尝试运行initilizer,导致出现问题。

要修复/解决这个问题,你需要在没有弹簧的情况下运行初始设置的rake任务。 实现这一点的一个方法是用bundler运行它:

 bundle exec rake db:create 

你可以尝试运行这个命令,

 bin/rake db:create RAILS_ENV=development 

或者这个命令 –

 bin/rake db:migrate RAILS_ENV=development 

我发现这个问题…

它与Rails初始化器有关:Rails似乎在执行bin/rake db:create之前加载所有初始化器。

并且,在这个特定的应用程序中,有一些依赖于ActiveRecord模型的自定义初始化程序(依次取决于正在创build的数据库,可用和相应的表)。

因此耙从不会真正执行任务,执行初始化程序时会失败。 我应该知道,如果我仔细阅读完整的错误信息日志。 这是完整的日志(见下面粗体,违规行):

 /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in`rescue in connect':FATAL:database“ds-dev “ 不存在
运行`$ bin / rake db:create db:migrate`来创build你的数据库(ActiveRecord :: NoDatabaseError)
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in`connect'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in`initialize'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in`new'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in`postgresql_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in`new_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in`checkout_new_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in`acquire_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in`block in checkout'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in`mon_synchronize'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in'checkout'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in`block in connection'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in`mon_synchronize'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in`connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in`retrieve_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_handling.rb:113:in`retrieve_connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_handling.rb:87:in`connection'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/model_schema.rb:209:in`table_exists?'
 from /home/rs/pr/ds/app/models/property.rb:32:in'get' 
  from /home/rs/pr/ds/config/initializers/custom/setup_mail.rb:3:in`'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in`load'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in`block in load'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in`load_dependency'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in`load'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:648:in`block in load_config_initializer'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/notifications.rb:161:in`instrument'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:647:in`load_config_initializer'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:612:in`block(2 levels)in'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:611:in`each'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/engine.rb:611:in`block in'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:30:in`instance_exec'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:30:in`run'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:55:in`block in run_initializers'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:226:in`block in tsort_each'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:348:in`block(2 levels)in each_strongly_connected_component'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:418:in`block(2 levels)in each_strongly_connected_component_from'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:427:in`each_strongly_connected_component_from'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:417:in`block in each_strongly_connected_component_from'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:44:in`each'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:44:in`tsort_each_child'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:411:in`call'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:411:in`each_strongly_connected_component_from'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:347:in`block in each_strongly_connected_component'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in`each'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in`call'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:345:in`each_strongly_connected_component'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:224:in`tsort_each'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/tsort.rb:205:in`tsort_each'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/initializable.rb:54:in`run_initializers'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/application.rb:300:in`initialize!'
 from /home/rs/pr/ds/config/environment.rb:5:in`'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in`require'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in`block in require'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in`load_dependency'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in`require'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:92:in`preload'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:140:in`serve'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:128:in`block in run'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:122:in`loop'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application.rb:122:in`run'
 from /home/rs/.rvm/gems/ruby-2.1.2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in`'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in`require'
 from /home/rs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in`require'
从-e:1:在``

我将致力于重构代码,以摆脱初始化器中的任何模型依赖关系(我非常确定这是一个非常糟糕的做法)。

我看到两个问题,正如is04指出的,你可能需要在你的database.yml中至less有一个username值。

您还需要创buildpostgresangular色:

 su - postgres create role direct-dev with createdb login password 'password1' 

没有解决scheme为我工作,但这对我工作。

 $ brew services list $ brew services restart postgresql 

今天(在OS X)有同样的问题。

解决:

 psql -U postgres CREATE ROLE rolename WITH CREATEDB LOGIN; 

使用\du来检查它是否被创build。

使用\password rolename为该用户/angular色创build一个密码。

然后你可以使用bundle exec rake db:migrate来设置数据库