Connecting to PostgreSQL database using Python not possible after brew upgrade

Viewed 141

I recently upgraded brew and part of it was an update to PostgreSQL. The update was successful, brew suggested to move older files to a different folder (I don't remember the exact line right now).

Now when I try to connect to the DB using python psycopg2 package, I get the following error messages:

ImportError: dlopen(/Users/fabioteichmann/.pyenv/versions/3.9.4/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): Library not loaded: /opt/homebrew/opt/postgresql/lib/libpq.5.dylib
  Referenced from: /Users/fabioteichmann/.pyenv/versions/3.9.4/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-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 reinstalling Postgres through brew but no success. I can connect to the DB using different tools.

Anyone able to help me out?

PS: apparently the library files are in a different place:

'/opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib'

How can I adapt to that?

1 Answers

Found the answer thanks to @Adrian Klaver:

I created a symlink through:

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

From this discussion

This does the job for me (for other PostgrSQL version you need to adapt the link a bit)

Related