skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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)


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search