Skip to content

Instantly share code, notes, and snippets.

@pjkelly
Last active February 8, 2023 04:49
Show Gist options
  • Save pjkelly/4a5dc1913320207172876e4e5f4ed55d to your computer and use it in GitHub Desktop.
Save pjkelly/4a5dc1913320207172876e4e5f4ed55d to your computer and use it in GitHub Desktop.
PG gem error after PostgreSQL upgrade.

When upgrading PostgreSQL on your machine, previously installed versions of the pg Rubygem will complain because their native extensions were compiled against an older version of Postgres. The error will look like this:

[daley (qa)]$ be rake db:create
rake aborted!
LoadError: dlopen(/opt/rubies/2.3.1/lib/ruby/gems/2.3.0/gems/pg-0.18.4/lib/pg_ext.bundle, 9): Library not loaded: /opt/boxen/homebrew/lib/libpq.5.5.dylib
  Referenced from: /opt/rubies/2.3.1/lib/ruby/gems/2.3.0/gems/pg-0.18.4/lib/pg_ext.bundle
  Reason: image not found - /opt/rubies/2.3.1/lib/ruby/gems/2.3.0/gems/pg-0.18.4/lib/pg_ext.bundle
/Users/pjkelly/src/daley/config/application.rb:7:in `<top (required)>'

The solve can be pretty simple - I've seen it work two different ways.

The first way to try is just uninstall the pg gem and then re-install it - that should cause the gem to be built against the newer version of Postgres on your machine.

gem uninstall pg
bundle install

If that doesn't work, then the second option is a little more involved. First, comment out the line in your Gemfile that says gem 'pg'. Next run the following:

bundle install --clean

That will remove all gems currently cached that are not specified in your Gemfile. Then uncomment gem 'pg' in your Gemfile and re-bundle:

bundle install
@bendilley
Copy link

Alternatively, I've found this works:

bundle config --global build.pg "--with-pg-config=/usr/local/bin/pg_config" # or wherever your pg_config lives
bundle pristine pg

The first line in my example is where homebrew installs Peter Eisentraut's postgresql tap, other installs may vary.

@bananatron
Copy link

Thanks for this! May the database fairy grant you 3 wishes ✨ ✨ ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment