0

I'm trying to install PostgreSQL on my Ruby app, and am running into a problem. Whenever I'm trying to start my server I get an error stating:

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).

which is weird because I've already added it to my Gemfile and ran bundle install and ran bundle list to check if it's been installed (which it is).

To run you through to what I've done.

I've started my PostgreSQL database, created a database and gave it a name & password.

Then I've changed the database.yml to:

# 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'] %>

Then I added gem 'pg' to my 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

Then I try to start my server and get the following error:

=> 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)

EDIT (10:12pm 31-01-18 CET)

I'm running the app on Cloud9 (which is ubuntu), previous/current database is SQlite (Default), but now that I want to deploy the website it needs to be changed to a better database. Therefore PostgreSQL, current pg version is 1.0

6
  • Run bundle show pg and do you see the gem listed? Commented Jan 31, 2018 at 16:29
  • can you try adding gem 'pg', '~> 0.21' to gemfile and bundle? Commented Jan 31, 2018 at 16:31
  • @imechemi It is on 1.0.0 Commented Jan 31, 2018 at 17:44
  • I'll try, why 0.21 tho? Commented Jan 31, 2018 at 17:44
  • @AneesMuhammed I downgraded pg and it worked. Can you please write this as an answer so we can close this question? Commented Feb 2, 2018 at 15:23

3 Answers 3

2

I suppose, this a pg compatibility issue with your current rails version. There are many threads open here mentioning incompatibility of rails with pg 1.0. Downgrading pg gem to the previous most stable version 0.21 should work for you. Just try

gem 'pg', '~> 0.21'

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, it did indeed help me ! :)
2

Please try to remove this one:

group :production do
  gem 'pg'
end

pg gem has already defined in outer scope so that it doesn't need to define in production group. Also please start server with bundle exec rails s to run rails server in current context of Gemfile.

1 Comment

Just to add to this - the same code exists duping the pg gem in the Gemfile's development and development / test groups...
1

Just keep the first occurence of gem 'pg' at the top of your Gemfile. All others should go away.

Then start your application with bundle exec rails s as stated before.

Should then work.

4 Comments

Still saying the gem isn't loaded :(
can you give some background: Which OS,which database version, current Gemfile with version of pg gem and the current database yaml. just update your question.
Did you run bundle install to install all required gems?
Yes, i'll update the question now. Current yaml and gemfile are posted in the question already :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.