Gem :: LoadError:指定' postgresql'对于数据库适配器,但未加载gem。将`gem' pg'`添加到您的Gemfile中

时间:2014-07-15 10:20:15

标签: heroku ruby-on-rails-4

我是Rails的新手。此应用程序在我的本地计算机上正常工作,并且没有任何问题地部署。但是当我运行heroku run rake db:migrate时,我收到此错误:

Running `rake db:migrate` attached to terminal... up, run.1269
rake aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile.

5 个答案:

答案 0 :(得分:37)

简单地将gem'pg'添加到gemfile中对我来说不起作用。

这对我有用

gem'pg','〜> 0.20'

得到了这个答案

Heroku and Rails: Gem Load Error with Postgres, however it is Specified in GEMFILE

感谢Piers C

是的,宝石'rails_12factor'有助于查看Heroku的日志以查找错误消息。

答案 1 :(得分:9)

将此行添加到Gemfile组内的:production(如果您没有,请添加一行)。

group :production do
  gem 'pg'
  gem 'rails_12factor'
end

从错误中可以清楚地知道,宝石pg需要添加到Gemfile。您可能只是添加了它,但您需要为开发和生产机器添加gem,因为Heroku应用程序是您系统的生产机器,而您的localhost是开发。

您的Gemfile应如下所示:

source 'https://rubygems.org'
ruby '2.0.0'


gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.0'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'

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

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor'
end

答案 2 :(得分:1)

我使用的是 ruby​​ 2.3.8。如果您无法解决此问题,则问题可能出在您尝试使用的 pg 版本上。用 gem 'pg' 替换 gem 'pg', '~> 0.21' 对我来说效果很好。另外,这是我的 database.yml 文件:

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5
development:
  <<: *default
  database: <%= ENV['DB_NAME'] %>
  host: <%= ENV['DB_HOST'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  reconnect: true
test:
  <<: *default
  database: <%= ENV['POSTGRES_DB'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  username: <%= ENV['POSTGRES_USER'] %>
  host: <%= ENV['POSTGRES_HOST'] %>
  port: <%= ENV['POSTGRES_PORT'] %>
  min_messages: notice
production:
  <<: *default
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>

这是我的 gemfile

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
# Use postgresql as the database for Active Record
# Adding pg gem for postgres
gem 'pg', '~> 0.21'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

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

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

答案 3 :(得分:0)

升级gitlab时出现错误。我执行了错误的命令,说的是sudo -u git -H bundle install --without postgres development test --deployment而不是sudo -u git -H bundle install --without mysql development test --deployment

只需执行

sudo -u git -H bundle install --with postgres为我做到了,在您的情况下

bundle install --with postgres,然后进行数据库迁移, heroku run rake db:migrate

答案 4 :(得分:0)

您还可以通过将rails升级到> = 5.1.5版本来修复它。

参考:https://github.com/rails/rails/issues/31673