rake db:create throws “database does not exist” error with postgresql
我在Debian 7下使用rails 4.1.5和postgresql 9.1,我无法在我的开发环境中创建数据库。 我跑的时候
1 | bin/rake db:CREATE |
我明白了
1 2 3 4 | 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:
1 2 3 4 5 6 7 8 | DEFAULT: &DEFAULT adapter: postgresql encoding: unicode pool: 5 development: <<: *DEFAULT DATABASE: direct-dev |
这是postgresql日志的一部分:
1 2 3 | 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附带弹簧预载器,和
New Rails 4.1 applications will ship with"springified" binstubs. This means that bin/rails and bin/rake will automatically take advantage of preloaded spring environments.
这意味着"弹性"
要修复/解决此问题,您需要在没有弹簧的情况下运行初始设置rake任务。实现这一目标的一种方法是使用bundler运行它:
1 | bundle EXEC rake db:CREATE |
我发现了这个问题......
它与Rails初始化器有关:Rails似乎在执行
并且,在这个特定的应用程序中,有一些自定义初始化程序依赖于ActiveRecord模型(反过来,它依赖于正在创建的数据库,可用的数据库和相应的表)。
因此rake永远不会实际执行任务,它在执行初始化程序时失败。我应该知道我是否仔细阅读了完整的错误消息日志。这是完整的日志(见下文,粗体,有问题的行):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | /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 `' |
我将努力重构代码,以摆脱初始化程序中的任何模型依赖(我非常确定它一定是一个非常糟糕的做法)。
您可以尝试运行此命令 -
1 | bin/rake db:CREATE RAILS_ENV=development |
或者这个命令 -
1 | bin/rake db:migrate RAILS_ENV=development |
没有一个解决方案适合我,但这对我有用。
1 2 | $ brew services list $ brew services restart postgresql |
今天有同样的问题(在OS X中)。
解决:
1 2 | psql -U postgres CREATE ROLE rolename WITH CREATEDB LOGIN; |
使用
使用
然后,您可以使用
没有一个解决方案适合我,但后来我看到一些答案提到预加载是问题的一部分。我意识到我有
在development.rb中。我将其更改为false并解决了问题。
我看到两个问题,因为is04指出你在database.yml中可能至少需要一个
您还需要创建postgres角色:
1 2 | su - postgres CREATE ROLE direct-dev WITH createdb login password 'password1' |