【问题标题】:Heroku won't recognize 'pg' gem in my Rails app GemfileHeroku 在我的 Rails 应用程序 Gemfile 中无法识别“pg”gem
【发布时间】:2014-01-02 11:28:01
【问题描述】:

我仍然是 Ruby on Rails 的新手,而我是 Heroku 的新手。

我已将我的 Rails 应用程序设置为在开发和生产中使用 postgres 而不是 sqlite3,并且我已将“gem 'pg'”放入我的 Gemfile。在本地,我的应用运行良好。

让它在 Heroku 上运行是另一回事。在我将我的应用程序推送到 Heroku 后,当我输入“heroku run rake db:migrate”时,它告诉我,“rake aborted!为数据库适配器指定了 'postgresql',但 gem 没有加载。添加 gem 'pg'到您的 Gemfile。”

是的,gem 'pg' 在我的 Gemfile 中,但我仍然收到此错误。

什么给了?

这是我的 Gemfile 现在的样子:

source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails',        '4.0.1'
gem 'pg'

group :assets do
end

gem 'bcrypt-ruby', '~> 3.1.2'
gem 'haml'
gem 'devise', '3.0.0.rc'
gem 'protected_attributes'
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
gem 'knockout-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'unicorn'
  gem 'rails_12factor', '0.0.2'
end

这是我的 database.yml 的样子:

development:
  adapter: postgresql
  encoding: unicode
  database: financemeter
  pool: 5
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: financemeter
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: dbe67fgdnnqg88
  pool: 5
  timeout: 5000
  user: njatxlpjjrzfoe
  password: HULxOtPALAk4FnzZe-_737TvPc

这里是我尝试在 heroku 上迁移时的完整错误日志。 (同样,在本地我很好。如果这很重要,我正在本地使用 MacBook。)

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/connection_specification.rb:58:in `rescue in resolve_hash_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/connection_specification.rb:55:in `resolve_hash_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/connection_specification.rb:32:in `spec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_handling.rb:39:in `establish_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/railtie.rb:176:in `block (2 levels) in <class:Railtie>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/lazy_load_hooks.rb:27:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.1/lib/active_record/railtie.rb:174:in `block in <class:Railtie>'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/application.rb:215:in `initialize!'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `block in require'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:214:in `load_dependency'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:229:in `require'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/application.rb:189:in `require_environment!'
/app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.1/lib/rails/application.rb:250:in `block in run_tasks_blocks'

【问题讨论】:

  • 确保进行捆绑安装并在其后提交您的 Gemfile 和 Gemfile.lock。然后尝试部署
  • 我试过了 - 它没有帮助。我不得不怀疑这是否是因为 Heroku 没有更新 - 当我执行“git push heroku master”时,它会返回一个简单的“Everything up-to-date”消息,而不会告诉我我的更改已被推送。跨度>
  • 您是否意识到您的 gemfile 中有两次 'pg' gem?尝试从:production 组中删除它,然后运行bundle updatebundle install 并尝试推送它。
  • 糟糕,感谢您指出。我从 :production 组中取出了那条线,并更正了我的帖子以反映这一点。不幸的是,它没有用,我仍然遇到这个问题。 :(
  • 你在使用 git 分支吗?也许你把 git 搞砸了。尝试git push -f heroku master 强制推送到远程 HEAD。

标签: ruby-on-rails postgresql heroku


【解决方案1】:

你的机器上安装了 postgresql 吗?
如果没有,请使用 homebrew 安装 pg。
安装后,请确保初始化 pg 数据存储。

另外,根据您的 pg 的安装方式,您可能需要在 database.yml 文件中指定用户名和主机

  username: someuser_on_pg
  host: localhost

希望有帮助

【讨论】:

  • @drumwolf 您是否在您的 heroku 仪表板帐户或命令行上添加了 Heroku Postgres 服务器的实例? heroku 插件:添加 heroku-postgresql
  • 我想我可能有。我最终使用“--database=postgresql”命令从头开始创建了一个全新的 Rails 应用程序,然后将我的所有资产、控制器、模型、视图等从旧应用程序迁移到新应用程序,最后将它们合并到 github 中。非常不雅,但它有效。
猜你喜欢
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多