Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error loading psycopg2 module: Library not loaded: libpq.5.dylib

I am trying to run a Django project with Postgres database. I use Postgres 13.4 installed via postgressapp (UNIVERSAL with all currently supported versions) and python 3.9 (in venv). I work on Mac with Apple M1 chip, macOS Big Sur.

I faced the following well-known problem:

django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/mymac/PyCharmProjects/projectname/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 2): Library not loaded: /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
  Referenced from: /Users/mymac/PyCharmProjects/projectname/venv/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so
  Reason: image not found

With searching, I found some discussions like this: https://github.com/psycopg/psycopg2/issues/1216. It seems that the most relevant solution is "RyanDurk commented on Jan 27":

$ brew install libpq --build-from-source

$ export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

$ pip install psycopg2

Unfortunately, in my case it does not help.

Then, I found some recommendations here: Library not loaded: /usr/local/lib/libpq.5.4.dylib and tried them. In particular, I tried to reach libpq.5.dylib via symlink like: ln -s  /Library/PostgreSQL/13/lib/libpq.5.dylib /opt/homebrew/opt/postgresql/lib/libpq.5.dylib (the solution marked as accepted by topic starter), but also unsuccessfully.

I tried to install postgres from postgresql.org, then uninstall/reinstall postgres with homebrew, then

gem uninstall pg -> bundle install

with the same result.

I have run the same project successfully before, on the mac with Intel chip and PyCharm community edition. Also the same project runs normally on Linux.

If you have any idea what happens and how to fix this problem, please help me. I provide more details if necessary.

like image 581
kokserek Avatar asked Nov 26 '25 23:11

kokserek


2 Answers

I had the same error after brew decided to upgrade my postgresql package to version 14 (your version may vary).

There was this part of the error message:

Library not loaded: '/usr/local/opt/postgresql/lib/libpq.5.dylib'
...
Reason: tried: '/usr/local/opt/postgresql/lib/libpq.5.dylib' (no such file)

I basically found the new location of the libpq and symlinked it to the location where psycopg2 was looking for it:

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

Hope that helps anyone else with a similar issue.

like image 172
alecxe Avatar answered Nov 29 '25 20:11

alecxe


I ran into the same error this morning. My solution is a bit different than the accepted one :

I uninstalled and reinstalled psycopg2.

pip uninstall psycopg2

pip install -r requirements.txt

No version change here but I guess the build process identified the right path.

like image 32
Séraphin Avatar answered Nov 29 '25 21:11

Séraphin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!