使用Heroku多个环境时无效的DATABASE_URL

时间:2012-01-09 14:24:35

标签: ruby-on-rails deployment heroku

我正在尝试从Heroku做这个教程。

http://devcenter.heroku.com/articles/multiple-environments

但是,当我尝试执行此命令时:

heroku rake db:migrate --remote staging

我收到了这个错误:

rake aborted!
Invalid DATABASE_URL

Tasks: TOP => db:migrate => db:load_config
(See full trace by running task with --trace)

使用--trace:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
rake aborted!
Invalid DATABASE_URL
(erb):9:in `rescue in <main>'
(erb):6:in `<main>'
/usr/ruby1.9.2/lib/ruby/1.9.1/erb.rb:753:in `eval'
/usr/ruby1.9.2/lib/ruby/1.9.1/erb.rb:753:in `result'
/app/.bundle/gems/ruby/1.9.1/gems/railties-3.1.3/lib/rails/application/configuration.rb:106:in `database_configuration'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/railtie.rb:68:in `block (2 levels) in <class:Railtie>'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:42:in `each'
/app/.bundle/gems/ruby/1.9.1/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/base.rb:2190:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.3/lib/active_record/railties/databases.rake:6:in `block (2 levels) in <top (required)>'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/ruby1.9.2/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain'
/usr/ruby1.9.2/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/app/.bundle/gems/ruby/1.9.1/bin/rake:19:in `load'
/app/.bundle/gems/ruby/1.9.1/bin/rake:19:in `<main>'
Tasks: TOP => db:migrate => db:load_config

这里的任何人都有类似的经历吗?非常感谢你。

3 个答案:

答案 0 :(得分:5)

有同样的问题,最后想出来了。以下是我用来实现这一目标的步骤:

  1. 输入heroku config --remote REMOTE_NAME(或--app APP_NAME)。请注意,未设置DATABASE_URL。
  2. 对于未设置DATABASE_URL的应用,请键入heroku addons:add heroku-postgresql:dev --remote REMOTE_NAME
  3. 添加Postgres数据库插件
  4. 通过键入heroku pg:promote DB_NAME_FROM_STEP_2 --remote REMOTE_NAME,将新添加的Postgres数据库设为应用的默认数据库,其中DB_NAME_FROM_STEP_2的格式为HEROKU_POSTGRESQL_COLOR_URL(COLOR类似于RED或GRAY)< / LI>

    就是这样!在推送以迁移Heroku数据库之后,不要忘记执行heroku run rake db:migrate --remote REMOTE_NAME

    参考文献:

答案 1 :(得分:1)

你的命令应该是

heroku rake db:migrate --app <HEROKU APP NAME>

显示在“我的应用”中。

我从未使用--remote标志,始终--app可以正常工作。不确定这篇文章是否是一篇错字。

更新:看来--remote使用你为该应用设置的git远程名称,但我自己并没有亲自使用它,但知道--app有效。

答案 2 :(得分:1)

尝试运行

heroku config

您将看到生产配置变量列表。如果你错过了DATABASE_URL变量,那就是你的问题。您需要添加数据库。要获得小的免费版本,请运行

heroku addons:add shared-database

此处有更多信息:https://devcenter.heroku.com/articles/database#troubleshootingHeroku app crashed, receiving "Invalid DATABASE URL" when attempting heroku rake db:migrate