[Fixed]-Postgres suddenly raise error '/usr/lib/libpq.5.dylib' (no such file)

18👍

To solve this problem just run the following command:

sudo mkdir -p /usr/local/lib && sudo ln -s /opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib /usr/local/lib/libpq.5.dylib

25👍

I have just encountered with this problem after upgrade Postgres with homwbrew.
So I try to reinstall psycopg2 in my venv and that’s solve it.
Now it’s ok. Just try:

pip install --upgrade --force-reinstall psycopg2

1👍

Something similar happened to me following a brew PostgreSQL upgrade. The solution to my problem was to delete my virtual environment, in my case .venv, and rerun:

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt

After that, I was able to start my application with no problem.

I think the issue was the result of an outdated dependency graph. Re-installing the dependencies with pip found the new version of Postgres and linked the libpq.5.dylib correctly.

Note, I was using the following psycopg2 dependency:

psycopg2-binary==2.9.3

For what it’s worth, I am also on MacOS Monterey and this just happened in a second codebase on the same machine.

The exact error was:

ImportError: dlopen(/Users/username/dev/src/project/.venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so, 0x0002): Library not loaded: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib'
Referenced from: '/Users/username/dev/src/project/.venv/lib/python3.10/site-packages/psycopg2/_psycopg.cpython-310-darwin.so'
Reason: tried: '/opt/homebrew/opt/postgresql/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file), '/opt/homebrew/Cellar/postgresql@14/14.5_4/lib/libpq.5.dylib' (no such file), '/usr/local/lib/libpq.5.dylib' (no such file), '/usr/lib/libpq.5.dylib' (no such file)

Leave a comment