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

2

19

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.

Pad answered 23/8, 2021 at 14:13 Comment(5)
Have you tried pip install psycopg2-binary?Nowak
Yes, I did. Now I check it: yes, it is on place. "Requirement already satisfied: psycopg2-binary in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (2.9.1)"Pad
The same in venv: Requirement already satisfied: psycopg2-binary in /Users/myMac/PyCharmProjects/myProject/venv/lib/python3.9/site-packages (2.9.1)Pad
This has me confused "I use Postgres 13.4 installed via postgressapp ..." and then "... then uninstall/reinstall postgres with homebrew, ...". Those are two different installs of Postgres and the original error was "/opt/homebrew/opt/postgresql/lib/libpq.5.dylib" which is coming from the Homebrew install. Can you clear up that confusion? Maybe get things down to one install of Postgres? FYI, gem install is a Ruby thing and not going to help with this.Nowak
I know it's not an answer to your question, but I'd suggest to use the terminal under Rosetta 2. Lots of libraries still don't support M1 which is really frustrating at this moment.Inigo
J
32

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.

Juncture answered 1/9, 2022 at 18:9 Comment(5)
Thank you so much. Yes, I tried to downgrade postgresql to v13 to solve my problem. I don't like my solution, so, now I follow your way.Pad
On Apple Silicon the Homebrew installation prefix changed. This should work on Intel and M1: ln -s $(brew --prefix)/lib/postgresql@14/libpq.5.14.dylib $(brew --prefix)/opt/postgresql/lib/libpq.5.dylibLepto
But why is psycopg2 looking for this file in a different directory? I didn't even have that directory, I had to manually create it and put the symlink in it.Harlow
This worked for me too, on Monterey 12.5.1.Aubyn
you sir deserve a medal. thanks for the explanation! worked in Ventura 13.12.1Fractious
D
0

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.

Darwin answered 27/3, 2023 at 10:47 Comment(1)
Useful, thank you) Unfortunately, it didn't work in my case.Pad

© 2022 - 2024 — McMap. All rights reserved.