0

I want to host my Ruby on Rails app on Heroku. But first I need to change my database from Sqlite3 to Heroku. So I'm following these instructions:

https://devcenter.heroku.com/articles/sqlite3

Here's how my Gemfile looks like

    source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

gem 'swagger-docs'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use sqlite3 as the database for Active Record
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# 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', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'
  gem 'rspec-rails'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
  gem 'rails-erd'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

And here's what happens when I try to run rake db:create, rake db:migrate: (Of course after running bundle install)

patryk@LenovoY510P ~/Railsapp/test/Movies_app $ rake db:createSpecified '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).
Couldn't create database for {"adapter"=>"postgresql", "pool"=>5, "timeout"=>5000, "database"=>"my_database_development"}
rake aborted!
Gem::LoadError: 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).
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:188:in `rescue in spec'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:185:in `spec'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:58:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:10:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:94:in `establish_master_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:18:in `create'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:117:in `create'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:137:in `block in create_current'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:136:in `create_current'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'

Caused by:
Gem::LoadError: can't activate pg (~> 0.18), already activated pg-0.17.1. Make sure all dependencies are added to Gemfile.
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:2:in `<top (required)>'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `block in require'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:258:in `load_dependency'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/dependencies.rb:292:in `require'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/connection_specification.rb:186:in `spec'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/connection_handling.rb:58:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:10:in `establish_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:94:in `establish_master_connection'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:18:in `create'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:117:in `create'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:137:in `block in create_current'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:136:in `create_current'
/home/patryk/Railsapp/test/Movies_app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:27:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

As you can see I already changed gem 'sqlite3' to gem 'pg' same with database.yml file. What should I do to fix this problem?

4
  • does the same happens when you run bundle exec rake db:create?
    – Greg
    Commented Jan 25, 2018 at 12:46
  • Yes, the same thing happens.
    – Ins0maniac
    Commented Jan 25, 2018 at 12:48
  • I seems like maybe your pg gem is in the wrong version. Try changing to gem 'pg', '~> 0.18' in your Gemfile. Also you can try bundle clean
    – Greg
    Commented Jan 25, 2018 at 12:52
  • Works perfect. Thanks!
    – Ins0maniac
    Commented Jan 25, 2018 at 13:08

1 Answer 1

2

Only pg database is used on heroku.

So modify your Gemfile with code:

gem 'pg'

Using above code, you can work pg db is used on heroku/

Run bundle install then push Gemfile and Gemfile.lock to heroku.

Then run heroku run rake db:migrate to run all migrations and create tables.

If you still got error then try:

gem 'pg', '~> 0.20', group: :production
1
  • 1
    It's generally not the best idea to have different DB's on dev and production. Unless you're very careful - you might get some surprises after deploying new version to production.
    – Greg
    Commented Jan 25, 2018 at 12:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.