rake db:使用postgresql创建抛出“数据库不存在”错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了rake db:使用postgresql创建抛出“数据库不存在”错误相关的知识,希望对你有一定的参考价值。
我在Debian 7下使用rails 4.1.5和postgresql 9.1,我无法在我的开发环境中创建数据库。我跑的时候
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 ...
我正在尝试创建数据库,因此,它自然不存在。但是rails应该创建它...这是我的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
你有什么指针吗?我已经在这一个多小时了,仍然无法理解为什么会发生这种情况......
谢谢!
Rails 4.1 ships with spring preloader,和
新的Rails 4.1应用程序将附带“弹性”binstubs。这意味着bin / rails和bin / rake将自动利用预加载的弹簧环境。
这意味着“弹性”bin/rake
将尝试预加载应用程序,而app又会尝试运行启动程序,从而导致您遇到的问题。
要修复/解决此问题,您需要在没有弹簧的情况下运行初始设置rake任务。实现这一目标的一种方法是使用bundler运行它:
bundle exec rake db:create
我发现了这个问题......
它与Rails初始化器有关:Rails似乎在执行bin/rake db:create
之前加载所有初始化器。
并且,在这个特定的应用程序中,有一些自定义初始化程序依赖于ActiveRecord模型(反过来,它依赖于正在创建的数据库,可用的数据库和相应的表)。
因此rake永远不会实际执行任务,它在执行初始化程序时失败。我应该知道我是否仔细阅读了完整的错误消息日志。这是完整的日志(见下文,粗体,有问题的行):
/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" 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 /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' from -e:1:in `'
我将努力重构代码,以摆脱初始化程序中的任何模型依赖(我非常确定它一定是一个非常糟糕的做法)。