0

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?

Fabio T.
  • 21
  • 3
  • You should probably [start using virtual environments](https://realpython.com/python-virtual-environments-a-primer/#sidestep-dependency-conflicts). – erip Sep 10 '22 at 17:33
  • I am already using one. – Fabio T. Sep 10 '22 at 18:21
  • Take a look at [Homebrew error](https://stackoverflow.com/questions/68894232/error-loading-psycopg2-module-library-not-loaded-libpq-5-dylib). In particular second comment to answer. – Adrian Klaver Sep 11 '22 at 16:11
  • From what I gather this is due to a change in packaging as shown in this [discussion/bug](https://github.com/Homebrew/discussions/discussions/3579) which relates to this [Postgres versioning](https://github.com/Homebrew/homebrew-core/pull/107726). – Adrian Klaver Sep 11 '22 at 16:15

2 Answers2

2

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)

Fabio T.
  • 21
  • 3
0

It tried to load libpq.5.dylib from the symlink /usr/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="/usr/lib/libpq.5.dylib"

# TODO: find this in your machine. The version maybe different than mine
DESTINATION_PATH="/opt/homebrew/opt/postgresql@14/lib/postgresql@14/libpq.5.dylib"

sudo mv $SYMLINK_PATH $SYMLINK_PATH.old
sudo ln -s $DESTINATION_PATH $SYMLINK_PATH