Categories
Programming

Heroku requires Postgresql, gem ‘pg’

Ran into an annoying chicken/egg involving deploying to Heroku with a shared database. It seems that Heroku isn’t happy unless you have gem ‘pg’ in your Gemfile. If you are using the shared database and fail to have gem ‘pg’ in your Gemfile, you app actually deploys OK on Heroku but then generates an Application Error when you call a page. If you check the heroku logs you see something like this:

2011-10-03T16:30:10+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:71:in `rescue in establish_connection’: Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.) (RuntimeError)

After adding gem ‘pg’ to your Gemfile and ran bundle install I was getting this error (locally):

ERROR: Failed to build gem native extension. (Gem::Installer::ExtensionBuildError)     /opt/local/bin/ruby extconf.rb checking for pg_config . . . not found

This means that if you don’t have Postgresql locally, you can’t bundle install without it, even if you don’t want to use Postgresql locally in development. To fix this, I had to install postgresql locally, add pg to my Gemfile, rebuild the gems, and then I was successfully able re-deploy my Heroku app.

First, I ran the “One click installer” from here:
http://www.postgresql.org/download/macosx

The installer had a note about how it had to change the shared memory settings on my machine (see the README file in the DMG). It created settings at /etc/sysctl.conf

After re-booting, go to the app you are trying to deploy and do this:

bundle config build.pg --with-pg-config=/Library/PostgreSQL/9.1/bin/pg_config

Finally, run bundle install again and everything should be clean. Remember, in my scenario I wasn’t really using Postgresql, I just had to have it in my Gemfile so that Heroku would allow me to deploy the app.

Leave a Reply

OR LOG IN WITH




Your email address will not be published. Required fields are marked *