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

77.7k views Asked by At

I am working on a rails app.
Installed Postgresql using postgresql-9.1.2-1-osx.dmg
Installed pg gem.
Then when I executed rake db:create getting 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

Checked if /usr/local/lib/libpq.5.4.dylib exists or not. It does 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.

Advice anyone please?

23

There are 23 answers

1
Sathish On BEST ANSWER

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.

0
K M Rakibul Islam On

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

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

This solved the issue for me. Hope this helps anyone who is facing the same issue.

0
Abror Mukimov On

A simple solution is uninstalling pg gem:

gem uninstall pg

and running

bundle install

That's it!

0
user19296946 On

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

2
tuwilof On
sudo ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
0
James Rissler On

I had this problem with macOS Ventura 13.0.1

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

Worked after :)

0
Jerome On

this is not a direct answer to the question but can arise frequently

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 rails 2 app failing with pg 0.17 and nearly killing the server: the mkmr.log increases exponentially ...try 200GB! )

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

0
Komor On

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

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 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 10.6.

0
Michael K Madison On

Try brew prune and then reinstall

0
Ganesh S Bhat On

I encountered a similar issue on Ventura. It happened after I upgraded my posgresql version from 9.6 to 12.

I tried to symlink by using 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 symlinks. 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
0
Kyamasam On

I kept getting the following error on M1 Mac with Django Running Postgres 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 symlink 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 dirs where the library is actually located.

0
stevec On

Error

I was getting this when I tried to start the 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 (postgres) ruby gem for that rails app:

gem uninstall pg

then reinstall it

bundle install

now rails server works!

0
Eric Norcross On

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 re-installing PostgreSQL via

brew install postgres

I was able to retain the use of the PostgresApp by adding the following to my .bash_profile

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 10.8.2)

7
ryan jeff On

Try to run

bundle pristine pg

and that was it. you find solution here: https://github.com/Homebrew/homebrew-core/issues/109644

1
Jeremy Lynch On

For me the following worked:

ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/* /opt/homebrew/opt/postgresql/lib/
0
Tega Ukavwe On

I had this issue whilst configuring postgresql as my default database for django on my Mackintosh.

It was resolved after installing psycopg2-binary via pip.

pip install psycopg2-binary

I hope this works for someone out there

0
BallpenMan On

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 brew
  2. Replace the raster2pgsql file (save it in another folder just in case) in /Library/PostgreSQL/13/lib with the raster2pgsql from the postgis brew version

Not sure how it affects other files in the postgresql and postgis EDB version.

2
BusinessFawn On

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 sym-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 Monterey 12.5.1

Chip Apple M1 Pro


8
S.D. On

I'm on OSX 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, brew uninstall && brew install,.. all did not.

0
klos On

I had this problem with posgresql installed with brew. When I run 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.

4
Sebastian Hätälä On

For me (MacOS 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 symlink 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
1
Taimoor Hassan On

this worked for me in MacOS Monterey

First I created the directory it is looking for and then created a symlink 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
9
awenkhh On

if you have upgraded

  • PostgreSQL with homebrew (brew update && brew upgrade),
  • macOS (eg. from Catalina to BigSur)

then simply uninstall the pg gem:

gem uninstall pg
bundle install

and the path will be corrected for you. No need to uninstall the whole PostgreSQL cluster.