186

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?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Sathish
  • 4,446
  • 3
  • 22
  • 17

22 Answers22

390

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.

Shiva
  • 11,485
  • 2
  • 67
  • 84
awenkhh
  • 5,811
  • 1
  • 21
  • 24
128

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.

S.D.
  • 2,486
  • 1
  • 16
  • 23
  • 2
    I had to do the same thing in Monterey on the update to 14.5. But I didn't use `sudo` – nzajt Aug 29 '22 at 22:00
  • 1
    This answer is golden I installed Ventura beta and got this after a brew update and brew upgrade – femiir Aug 30 '22 at 15:47
  • 4
    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.dylib` – Patryk Szczepański Aug 31 '22 at 07:54
  • 9
    This is likely due to `brew` renaming `postgres` to `postgres@14`. – Andrew Sep 08 '22 at 22:06
  • 1
    Thank you! This worked for me on Apple M1 Pro, Monterrey 12.0.1 – guero64 Sep 15 '22 at 15:24
  • 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! – JakeRobb Sep 26 '22 at 18:40
  • Another grateful commenter. Glad this ten-year-old post is useful again. – David Rhoden Nov 17 '22 at 19:45
  • 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! – Sarang Jan 13 '23 at 18:43
79

Try to run

bundle pristine pg

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

ryan jeff
  • 891
  • 6
  • 7
37

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
    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. – PhilCowan Sep 15 '22 at 19:56
  • 2
    Thanks! This save my day (Monterey, Intel) – Hlib Oct 11 '22 at 09:39
  • This one worked. – Andy Huang Dec 28 '22 at 03:41
  • 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 ``` – Dmitry Petukhov Mar 30 '23 at 13:47
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 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


Sebastiano
  • 12,289
  • 6
  • 47
  • 80
BusinessFawn
  • 193
  • 3
  • 8
14

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
Taimoor Hassan
  • 365
  • 2
  • 11
  • 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.dylib` – anta40 Jul 14 '23 at 10:12
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 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)

Eric Norcross
  • 4,177
  • 4
  • 28
  • 53
8

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

Tega Ukavwe
  • 99
  • 1
  • 4
7
sudo ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
tuwilof
  • 351
  • 3
  • 6
  • Please explain why your answer works – Rohit Gupta Nov 11 '22 at 10:51
  • 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 retry – Pierre Jan 03 '23 at 14:35
6

For me the following worked:

ln -s /opt/homebrew/opt/postgresql/lib/postgresql@14/* /opt/homebrew/opt/postgresql/lib/
Jeremy Lynch
  • 6,780
  • 3
  • 52
  • 63
4

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.

Sathish
  • 4,446
  • 3
  • 22
  • 17
  • 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? – Pychong Mar 21 '21 at 16:33
4

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.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
4

A simple solution is uninstalling pg gem:

gem uninstall pg

and running

bundle install

That's it!

3

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.

Komor
  • 41
  • 3
3

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

Jerome
  • 5,583
  • 3
  • 33
  • 76
2

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

1

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

1

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.

Kyamasam
  • 183
  • 2
  • 5
0

Try brew prune and then reinstall

Michael K Madison
  • 2,242
  • 3
  • 21
  • 35
0

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.

BallpenMan
  • 185
  • 1
  • 12
0

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.

klos
  • 153
  • 1
  • 7
0

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
Ganesh S Bhat
  • 197
  • 1
  • 10