Sathish
Sathish

Reputation: 4554

Library not loaded: /usr/local/lib/libpq.5.4.dylib

I am working on a Ruby on Rails application and installed PostgreSQL using postgresql-9.1.2-1-osx.dmg. I installed the pg gem.

Then when I executed rake db:create, I got the following error:

dlopen(/Users/sathishvc/.rvm/gems/ruby-1.9.3-head@knome-vivacious/gems/pg-0.12.2/lib/pg_ext.bundle, 9): Library not loaded: /usr/local/lib/libpq.5.4.dylib

I checked if /usr/local/lib/libpq.5.4.dylib existed or not. It did not. So, it should be existing somewhere else in the system or I do not know, if I need to install any other piece of software for this.

What should I do?

Upvotes: 197

Views: 86371

Answers (23)

ryan jeff
ryan jeff

Reputation: 1001

Try to run

bundle pristine pg

and that was it. You will find the solution on PostgreSQL service points to bad data directory after #109075 #109644.

Upvotes: 90

Ganesh S Bhat
Ganesh S Bhat

Reputation: 237

I encountered a similar issue on macOS v13 (Ventura). It happened after I upgraded my PostgreSQL version from 9.6 to 12.

I tried to symbolic link by using the following command:

sudo ln -s /opt/homebrew/opt/postgresql@12/lib/postgresql@12/libpq.5.dylib /usr/local/lib/libpq.5.dylib

If this is not helping, try:

brew link postgresql@12 --force

It created 377 symbolic links. Here is the entire output of the above command:

Linking /usr/local/Cellar/postgresql@12/12.15_2... 377 symlinks created.
If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/postgresql@12/bin:$PATH"' >> ~/.zshrc

Upvotes: 3

Kyamasam
Kyamasam

Reputation: 361

I kept getting the following error on a M1 Mac with Django running PostgreSQL installed with Homebrew.

...
Error loading psycopg2 module:
...
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file),

This is how I fixed it. Create a directory where the code is trying to look for the library:

mkdir /opt/homebrew/opt/postgresql/lib/

Create a symbolic link to where the library actually exists:

ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/

If these does not work for you, then make sure you identify where the library is located and where the code is trying to look for it and replace accordingly.

Run the command

locate libpq.5.dylib

This will list the directories where the library is actually located.

Upvotes: 2

stevec
stevec

Reputation: 52897

Error

I was getting this when I tried to start the Ruby on Rails server:

rails server
/Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require': dlopen(/Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg_ext.bundle, 0x0009): Library not loaded: /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib (LoadError)
  Referenced from: <C859835A-2D0F-3B4A-BFAB-2AD961F6197B> /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg_ext.bundle
  Reason: tried: '/opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib' (no such file), '/opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/usr/lib/libpq.5.dylib' (no such file, not in dyld cache) - /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg_ext.bundle
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg.rb:49:in `block in <module:PG>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg.rb:37:in `block in <module:PG>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg.rb:42:in `<module:PG>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/pg-1.3.5/lib/pg.rb:6:in `<main>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler/runtime.rb:55:in `each'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler/runtime.rb:55:in `block in require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler/runtime.rb:44:in `each'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler/runtime.rb:44:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bundler-2.3.19/lib/bundler.rb:188:in `require'
    from /Users/st/rails/checkeasy/config/application.rb:7:in `<main>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.2.4/lib/rails/commands/server/server_command.rb:137:in `block in perform'
    from <internal:kernel>:90:in `tap'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.2.4/lib/rails/commands/server/server_command.rb:134:in `perform'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.2/lib/thor/command.rb:27:in `run'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.2/lib/thor/invocation.rb:127:in `invoke_command'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/thor-1.2.2/lib/thor.rb:392:in `dispatch'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.2.4/lib/rails/command/base.rb:87:in `perform'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.2.4/lib/rails/command.rb:48:in `invoke'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/railties-7.0.2.4/lib/rails/commands.rb:18:in `<main>'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /Users/st/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/bootsnap-1.11.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from bin/rails:4:in `<main>'

Solution

The solution was really easy: uninstall and reinstall the pg (PostgreSQL) Ruby gem for that Ruby on Rails application:

gem uninstall pg

Then reinstall it:

bundle install

Now rails server works!

Upvotes: 0

user19296946
user19296946

Reputation: 11

I just needed to upgrade psycopg2-binar package to the latest version (2.9.5 in my case). It fixed the missing file issue.

Upvotes: 1

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34336

I was having this same issue after upgrading to a new Ruby version (2.7 to 3.1.2) in my Ruby on Rails 7 application. In my case, I had to uninstall and reinstall the pg gem in my Ruby on Rails application:

gem uninstall pg
bundle install # (which installed the pg gem with native extensions)

This solved the issue for me.

Upvotes: 6

Tega Ukavwe
Tega Ukavwe

Reputation: 129

I had this issue while configuring PostgreSQL as my default database for Django on my Macintosh.

It was resolved after installing psycopg2-binary via pip.

pip install psycopg2-binary

Upvotes: 8

James Rissler
James Rissler

Reputation: 21

I had this problem with macOS v13.0.1 (Ventura).

I installed postgresql@14 via Homebrew.

Then:

mkdir /opt/homebrew/opt/libpq/

mkdir /opt/homebrew/opt/libpq/lib

sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.14.dylib /opt/homebrew/opt/libpq/lib/libpq.5.dylib

It worked after :)

Upvotes: 2

tuwilof
tuwilof

Reputation: 587

Use:

sudo ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib

Upvotes: 8

Taimoor Hassan
Taimoor Hassan

Reputation: 417

This worked for me in macOS v12 (Monterey).

First, I created the directory it is looking for and then created a symbolic link to the existing file:

sudo mkdir -p /usr/local/opt/postgresql/lib/
sudo ln -s /usr/local/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/opt/postgresql/lib/libpq.5.dylib

Upvotes: 15

Jeremy Lynch
Jeremy Lynch

Reputation: 7230

For me, the following worked:

ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/* /opt/homebrew/opt/postgresql/lib/

Upvotes: 6

For me (macOS v12 (Monterey)), the file eventually turned up under: /usr/local/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib

So to fix the initial error, first create the directory it is looking for and then create a symbolic link to the existing file:

sudo mkdir -p /usr/local/opt/postgresql/lib/
sudo ln -s /usr/local/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/opt/postgresql/lib/libpq.5.dylib

Upvotes: 39

BusinessFawn
BusinessFawn

Reputation: 301

I followed something similar to S.D.'s answer on a M1 chip. Linking to /usr/local/lib/libpq.5.dylib as suggested by Patryk Szczepański didn't work for me, so I created an empty /opt/homebrew/opt/postgresql/lib and symbolic linked:

mkdir /opt/homebrew/opt/postgresql
mkdir /opt/homebrew/opt/postgresql/lib
sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.14.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib

macOS v12.5.1 (Monterey)

Chip Apple M1 Pro

Upvotes: 14

klos
klos

Reputation: 153

I had this problem with PostgreSQL installed with Homebrew. When I ran brew doctor, I found this:

Warning: Broken symlinks were found. Remove them with `brew cleanup`: /usr/local/lib/libpq.5.dylib

Running brew cleanup fixed it.

Upvotes: 0

S.D.
S.D.

Reputation: 2951

I'm on OS X v12 (Monterey) (M2-chipset) and had a similar issue. Running (mind the version) helped:

sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/lib/libpq.5.dylib

brew update, brew link --force, and brew uninstall && brew install, etc. all did not.

Upvotes: 135

BallpenMan
BallpenMan

Reputation: 207

I had the same issue with PostgreSQL 13.4 installed via the EDB installer and PostGIS installed via Stackbuilder. None of the solutions worked for me, so I just:

  1. Installed PostgreSQL and PostGIS via Homebrew
  2. Replace the raster2pgsql file (save it in another folder just in case) in /Library/PostgreSQL/13/lib with the raster2pgsql from the PostGIS Homebrew version

I am not sure how it affects other files in the PostgreSQL and PostGIS EDB version.

Upvotes: 0

Jerome
Jerome

Reputation: 6217

Two additional cases that might be of interest if you have upgraded PostgreSQL with Homebrew:

  1. you have proceeded in the past to some pruning of gem versions
  2. your application is older and cannot take a recent version of pg (I have a Ruby on Rails 2 app failing with pg 0.17 and nearly killing the server: the mkmr.log increases exponentially ...try 200 GB!)

You may actually have numerous versions of pg to rely upon.

When you run

gem uninstall pg

if you have more than one version, the system will ask which version you wish to delete. But that list may be useful to you! As an alternative to awenkhh's answer, you may stop there and reset your gems to an acceptable version.

Upvotes: 3

Komor
Komor

Reputation: 41

In my case, uninstalling and reinstalling gem didn't help. But I added the PostgreSQL library path to the $DYLD_LIBRARY_PATH environment variable in my $HOME/.bash_profile file file:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.3/lib:$DYLD_LIBRARY_PATH

If you also have MySQL installed, this line can look like:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:/Library/PostgreSQL/9.3/lib:$DYLD_LIBRARY_PATH

Of course, pay attention to the version number in the PostgreSQL path. At the time of this writing, it is 9.3. I'm using RVM, Ruby 2.0.0, pg gem 0.17.0, on Mac OS X v10.6 (Snow Leopard).

Upvotes: 3

awenkhh
awenkhh

Reputation: 6131

If you have upgraded

  • PostgreSQL with Homebrew (brew update && brew upgrade),
  • macOS (e.g., from v10.15 (Catalina) to v11 (Big Sur))

Then simply uninstall the pg gem:

gem uninstall pg
bundle install

And the path will be corrected for you. There isn't any need to uninstall the whole PostgreSQL cluster.

Upvotes: 417

Eric Norcross
Eric Norcross

Reputation: 4306

I was running into this issue after switching over to the PostgresApp and removing the existing PostgreSQL version via:

brew remove postgres

I ultimately solved the problem by reinstalling PostgreSQL via:

brew install postgres

I was able to retain the use of the PostgresApp by adding the following to my $HOME/.bash_profile file:

export PATH="/Applications/Postgres.app/Contents/MacOS/bin:/usr/local/bin:$PATH"

You can also try running:

brew doctor

As it alerted me to this as well as some additional errors.

This error was also affecting my ability to use the Paperclip gem as it was causing ImageMagick to fail with the same error.

(Using OS X v10.8.2 (Mountain Lion))

Upvotes: 9

Abror Mukimov
Abror Mukimov

Reputation: 73

A simple solution is uninstalling pg gem:

gem uninstall pg

and running

bundle install

That's it!

Upvotes: 5

Michael K Madison
Michael K Madison

Reputation: 2209

Try brew prune and then reinstall

Upvotes: 0

Sathish
Sathish

Reputation: 4554

Some time after I posted this question, I found that libpq.5.4.dylib resides in /Library/PostgreSQL/9.1/lib/.
So, I created the following link:
lrwxr-xr-x 1 sathishvc admin 43 Jan 28 23:40 /usr/local/lib/libpq.5.4.dylib -> /Library/PostgreSQL/9.1/lib/libpq.5.4.dylib.

This solved the problem then.

Upvotes: 3

Related Questions