Postgres suddenly raise error '/usr/lib/libpq.5.dylib' (no such file)
Question:
when I run Django project or any code related to Postgres :
Referenced from: '/Users/mahmoudnasser/.local/share/virtualenvs/wyspp_backend-PwdII1PB/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-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)
I tried many solutions online but none of them worked.
Note: I use MacOS
Answers:
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
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)
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
It tried to load libpq.5.dylib
from the symlink /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
but could not find the file, so you need to update it:
# TODO: get this from the error, after "Library not loaded:"
SYMLINK_PATH="/opt/homebrew/opt/postgresql/lib/libpq.5.dylib"
# TODO: find this in your machine. The version maybe different than mine
DESTINATION_PATH="/opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib"
sudo mv $SYMLINK_PATH $SYMLINK_PATH.old
sudo ln -s $DESTINATION_PATH $SYMLINK_PATH
when I run Django project or any code related to Postgres :
Referenced from: '/Users/mahmoudnasser/.local/share/virtualenvs/wyspp_backend-PwdII1PB/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-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)
I tried many solutions online but none of them worked.
Note: I use MacOS
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
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)
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
It tried to load libpq.5.dylib
from the symlink /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
but could not find the file, so you need to update it:
# TODO: get this from the error, after "Library not loaded:"
SYMLINK_PATH="/opt/homebrew/opt/postgresql/lib/libpq.5.dylib"
# TODO: find this in your machine. The version maybe different than mine
DESTINATION_PATH="/opt/homebrew/opt/postgresql/lib/postgresql@14/libpq.5.dylib"
sudo mv $SYMLINK_PATH $SYMLINK_PATH.old
sudo ln -s $DESTINATION_PATH $SYMLINK_PATH