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

23

193

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?

Unesco answered 26/1, 2012 at 18:40 Comment(0)
U
3

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.

Unesco answered 7/6, 2012 at 19:19 Comment(2)
In OS 10.15.7,I can't use chown username libpq.5.4.dylib.The ownuser is root,I cant't change it.How did you do that?Demanding
OS X v10.15.7 (Catalina)Herring
A
406

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.

Alfie answered 10/10, 2013 at 14:51 Comment(9)
brew update && brew upgrade worked for me, I did not even need to uninstall and reinstall pg.Millsaps
If it wasn't pg you could get rid of all of your gems with gem uninstall --all then install bundler and then run bundle install to install everything again (given you have a Gemfile)Cardiomegaly
@Cardiomegaly why would one want to do such a massive thing? ;-)Alfie
If you tried the things above and still get this error, brew link libpq --force fixed the problem in my case.Asquint
gem uninstall pg and bundle install worked for me!Toomay
this should be the correct answer imhoTrigeminal
I've tried a few of the answers provided here on an M1 machine and this was the only one that worked. Thank you.Legislation
I had this problem after home-brew upgraded my installation of PG 14. The instructions above gem uninstall pg followed by bundle install worked for me.Abide
After the brew upgrade path to .dylib is changed, it can't find them. gem uninstall pg and bundle install solves the problem.Predictor
D
135

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.

Diplomacy answered 29/8, 2022 at 9:41 Comment(8)
I had to do the same thing in Monterey on the update to 14.5. But I didn't use sudoEdveh
This answer is golden I installed Ventura beta and got this after a brew update and brew upgradeTraveled
In case of using Apple Silicon based Macs, you should use this command to create symlink: sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/lib/libpq.5.dylibMart
This is likely due to brew renaming postgres to postgres@14.Melodize
Thank you! This worked for me on Apple M1 Pro, Monterrey 12.0.1Ted
Did you try uninstalling and reinstalling the gem (@awenkhh's answer)? That fixed it for me. I find that strongly preferable over manual symlink management!Zeke
Another grateful commenter. Glad this ten-year-old post is useful again.Ridgepole
It is better not to add links to a different directory (usr/local in the the answer). The problem is with the extra postgresql@14 at the end after the lib in the path. The following helped me: cd /opt/homebrew/Cellar/postgresql@14/14.6/lib/ and then run ln -s postgresql@14/* . . This will ensure all libraries are available under /opt/homebrew/Cellar/postgresql@14/14.6/lib/ which is where they are expected to be in the first place!Nikolos
S
85

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.

Suppositive answered 4/10, 2022 at 19:50 Comment(7)
This worked for me on Rails 7, MacOs MontereyXylol
This worked for me on Rails 6.1, MacOs MontereyCouthie
Worked like a charm for me Rails 6.1.5, Ruby 2.7.1, MBP M1 MacOS Monterey (thank you! 🙌)Egg
I tried some solutions and they didn't work. This one worked for me. Rails v6.1.7Robyn
Works for me with ruby 2.6.5 and rails 5.2.4. Thanks!Repand
Worked for me after run brew update & brew upgradeAnaliese
Worked for me. Remove cmd had still retained this problem, but pristine cleaned it up to point to the new postgres version.Basin
B
39

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
Bartels answered 13/9, 2022 at 15:5 Comment(4)
This is exactly what fixed it for me. I noticed yesterday when trying to use postgis for the first time, that for some reason my postgresql folder also had the @14 and it was causing postgis not to work correctly in my Java app. Today I went back to work on this rails app and got this error, I'm thinking this might fix both.Mantelpiece
Thanks! This save my day (Monterey, Intel)Malignity
This one worked.Haugen
In my case I had to do this sh sudo ln -s /usr/local/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/lib/libpq.5.dylib Rosalba
U
14

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

Unclad answered 31/8, 2022 at 19:40 Comment(0)
L
14

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
Lagasse answered 23/9, 2022 at 6:43 Comment(1)
I'm also on Intel Mac (Monterey), and this works: sudo ln -s /usr/local/opt/postgresql/lib/postgresql@14/libpq.5.14.dylib /usr/local/lib/libpq.dylibLohengrin
G
8

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

Gibby answered 22/2, 2013 at 10:16 Comment(0)
B
8

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
Bos answered 12/10, 2022 at 19:25 Comment(0)
M
8

Use:

sudo ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
Menard answered 10/11, 2022 at 10:10 Comment(3)
Please explain why your answer worksListed
If it fails to create symlink for /opt/homebrew/opt/postgresql/lib/libpq.5.dylib be sure that directories exists by running sudo mkdir -p /opt/homebrew/opt/postgresql/lib... then retryGenitive
Is this a bogus answer?Herring
L
6

For me, the following worked:

ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/* /opt/homebrew/opt/postgresql/lib/
Lowder answered 6/10, 2022 at 13:34 Comment(1)
Worked for me! I did have postgresql@14 installed via Homebrew and macOS Monterey.Renunciation
C
6

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.

Complected answered 26/12, 2022 at 7:12 Comment(0)
B
5

A simple solution is uninstalling pg gem:

gem uninstall pg

and running

bundle install

That's it!

Brewage answered 26/12, 2022 at 7:49 Comment(0)
U
3

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.

Unesco answered 7/6, 2012 at 19:19 Comment(2)
In OS 10.15.7,I can't use chown username libpq.5.4.dylib.The ownuser is root,I cant't change it.How did you do that?Demanding
OS X v10.15.7 (Catalina)Herring
E
3

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

Exfoliation answered 22/10, 2013 at 11:26 Comment(0)
C
3

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.

Colpin answered 9/8, 2014 at 18:3 Comment(0)
M
3

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
Margoriemargot answered 31/7, 2023 at 10:21 Comment(3)
This suspiciously had the exact same typo of PostgreSQL ("posgresql") as klos's answer (this is very unlikely to happen by chance). That is out of many possible, incl. "pgsql", "Postgre", "postgre", "Postgres", "postgres", "postgresql", "Postgresql", "postgres sql", "postgreSQL", "PostgreSql", "postgreql", "postgress", "PostGreSQL", "postgreSql", "PostreSQL", "PG", "pg", "Pg", "PostgresSQL", "POSTGRESQL", "Postgress",Herring
cont' - "potsgres", "postgersql", "PostGres", "pgSQL", "PgSQL", "PL/pgSQL", "PostGresql", "PGSQL", "postgre sql", "Postgres SQL", "postgresSQL", "progresSQL", "postreg", "postrgres", "postrgeSQL", "postresql", and "PostgresQL"Herring
Though there isn't any other communality.Herring
E
2

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

Evelynneven answered 22/11, 2022 at 15:12 Comment(0)
D
2

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.

Dolliedolloff answered 27/5, 2023 at 20:33 Comment(0)
S
1

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

Sparker answered 9/1, 2023 at 15:55 Comment(1)
Is it really "psycopg2-binar"?Herring
B
0

Try brew prune and then reinstall

Bedroom answered 28/10, 2013 at 22:37 Comment(0)
T
0

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.

Trot answered 27/8, 2022 at 14:51 Comment(0)
A
0

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.

Afterglow answered 7/9, 2022 at 18:0 Comment(0)
P
0

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!

Picker answered 29/3 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.