我正在尝试在我的Ruby应用程序上安装PostgreSQL,并且遇到了问题。每当我尝试启动服务器时,都会收到错误消息:
rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add gem 'pg' to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
这很奇怪,因为我已经将它添加到我的Gemfile并运行bundle install
并运行bundle list
以检查它是否已安装(它是)。
让你了解我所做的事情。
我已经启动了我的PostgreSQL数据库,创建了一个数据库并给它一个名字&密码。
然后我将 database.yml 更改为:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: postgresql
encoding: unicode
pool: 5
# Important configs for cloud9, change password value
# to what you entered in the previous psql step.
template: template0 (not sure what the fill in here?)
username: my_username (can be omitted)
password: my_password (can be omitted)
development:
<<: *default
database: my_database_development
test:
<<: *default
database: my_database_test
production:
<<: *default
database: my_database_production
username: my_username
password: <%= ENV['my_password'] %>
然后我将gem 'pg'
添加到我的 gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
gem 'pg'
# 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: https://github.com/rails/turbolinks
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
gem 'rake'
gem 'rspec'
gem 'kramdown'
gem 'thor', '0.19.1'
gem 'spree_braintree_vzero', github: 'spree-contrib/spree_braintree_vzero'
# 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'
gem 'capistrano-passenger'
gem 'capistrano-rails'
gem 'capistrano-rvm'
gem 'pg'
end
group :development do
gem 'pg'
# 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
gem 'spree', '~> 3.1.0.rc1'
gem 'spree_auth_devise', '~> 3.1.0.rc1'
gem 'spree_gateway', '~> 3.1.0.rc1'
gem 'devise'
gem 'materialize-sass'
gem 'active_link_to'
gem 'mollie-api-ruby'
# gem 'spree_mollie', github: 'salman15/mollie_spree_2017', branch: 'stable'
gem 'spree_mollie', github: 'ttcremers/spree_mollie', branch: 'stable'
group :production do
gem 'pg'
end
然后我尝试启动服务器并收到以下错误:
=> Booting WEBrick
=> Rails 4.2.6 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
编辑(欧洲中部时间晚上10:01 pm)
我在Cloud9上运行应用程序(这是ubuntu),以前/当前数据库是SQlite(默认),但现在我想部署网站需要将其更改为更好的数据库。因此PostgreSQL,当前的pg版本是1.0
答案 0 :(得分:2)
请尝试删除此内容:
group :production do
gem 'pg'
end
pg
gem已经在外部范围内定义,因此不需要在production
组中定义。另请使用bundle exec rails s
启动服务器以在Gemfile
的当前上下文中运行rails服务器。
答案 1 :(得分:1)
只需将gem 'pg'
的第一个出现在Gemfile的顶部即可。所有其他人都应该离开。
然后按照之前的说明使用bundle exec rails s
启动您的申请。
然后应该工作。
答案 2 :(得分:1)
我想,这与您当前的rails版本存在pg
兼容性问题。这里有许多线程提到rails与pg 1.0
的不兼容性。将pg gem降级到之前最稳定的版本0.21
应该对你有用。试试吧
gem 'pg', '~> 0.21'
希望这有帮助