Use PostgreSQL Gem Instead of SQLite When Deploying Rails Applications to Heroku

This is for both RoR 3.2 and 4.0.0 beta1/RC1!

When creating a new Ruby on Rails application, the Gemfile will include the gem for SQLite by default.

Example excerpt from Gemfile in your RoR application directory:

gem 'sqlite3', '1.3.7'

When running Rails server in development on your local computer (most likely a Mac or Linux), this works fine. However, if you wish to deploy your application to Heroku, you will not be able to include the SQLite gem, as Heroku only supports PostgreSQL.

What is necessary at this point is to change the Gemfile to use SQLite only for development (and test, if you wish) and use PostgreSQL for production.

Change the above code in the Gemfile to this:

group :development do
   gem 'sqlite3', '1.3.7'
end

group :production do
   gem 'pg', '0.14.1'
end

Save the Gemfile, and reinstall the application using

bundle install --without production


at the shell prompt.

Update the local git repository with

git commit -am "Gemfile.lock updated for Heroku deployment"

Optional: Update GitHub (or other remote repository) with

git push

Assuming that your Heroku account and application have been established already, deploy to Heroku. (If the name of the git branch is something other than “master” use the correct branch name instead of “master”.)

git push heroku master

Nota bene:
Because of the way that Heroku works with Rails 4.0, a route will need to be added for your page to appear properly in Heroku if you choose to use Rails 4.0. Rails 3.2 does not require this route to be configured.

Ruby 2.0.0 was recently released into production, and Rails 4.0.0 RC 1 was just released on May 1st.

If you want to use these versions for your Heroku deployment, add

ruby '2.0.0'


to the top of Gemfile, and change the Rails gem entry to

gem 'rails', '4.0.0.rc1'

If you have been using the 4.0.0.beta1 release of Rails, you will need to change the versions on some other gems to 4.0.0.rc1. (Coffee-rails is now up to 4.0.0.)

In changing from beta1 to RC1, replace the line

config.session_store :encrypted_cookie_store #...


with

config.session_store :cookie_store #...


in config/initializers/session_store.rb. The store will be automatically encrypted. Otherwise, “rails server” and the deployment to Heroku will fail.

Lastly, remember to “bundle install…” and “git commit…” before doing “git push heroku…”!

Houston, TX 77002

2 Replies to “Use PostgreSQL Gem Instead of SQLite When Deploying Rails Applications to Heroku”

  1. I don’t get why you would need to update GitHub on the git commit message rather than just purely via git itself.

    1. You are absolutely correct, sir! I mistakenly wrote that the “git commit” was to update GitHub; it just updates the local repository. And a “git push” back to GitHub is not necessary here. I will correct the post. Thanks for catching that!

Leave a Reply