0

I have been stuck on this issue for some time. After struggling to install psycpg2 on my Mac I got a friend to help me install it using this:

env LDFLAGS="-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib" pip3 install psycopg2

I then imported psycopg2

I tried to run my db program and got this error:

Traceback (most recent call last):
  File "postgresdemo.py", line 1, in <module>
    import psycopg2 
  File "/Users/nkosana/Library/Python/3.7/lib/python/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/nkosana/Library/Python/3.7/lib/python/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: _PQencryptPasswordConn
  Referenced from: /Users/nkosana/Library/Python/3.7/lib/python/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Expected in: /usr/lib/libpq.5.dylib in /Users/nkosana/Library/Python/3.7/lib/python/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so

System and versions

MacOS Catalina (10.15.4)

psycopg2-2.8.5

Python 3.7.3

I would appreciate your help, thanks in advance.

3 Answers3

0

I had the same problem you are having:

Symbol not found: _PQencryptPasswordConn
  Referenced from: /Users/Lynda/pgproject/pgprojectenv/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Expected in: /usr/lib/libpq.5.dylib

What worked for me is adding /usr/local/lib to the PATH and create a symbolic link from my PostgreSQL/lib to /usr/local/lib

cd /usr/local/lib
ln -s /Users/Lynda/PostgreSQL/lib/libpq.5.12.dylib ./libpq.5.dylib

There is an answer that fixes this lib error and better explains why this is happening and how to fix it here Mac OS X Lion Psycopg2: Symbol not found: _PQbackendPID

bgar2021
  • 1
  • 1
0
pip uninstall psycopg2
pip install psycopg2

From here, worked for me. macOS Catalina version 10.15.7. Python 3.5.9

Q. Qiao
  • 767
  • 6
  • 17
0

The ln -s solution proposed by bgar2021 cannot work because the .dylib file in the OP case is expected to be in /usr/lib, not /usr/local/lib, and you cannot alter .dylib links or files in /usr/lib because of the SIP protection (and disabling the SIP protection is definitely not recommended).

In the OP case, the built-in MacOS install_name_tool command should work, also because the .so file has been put in the project folder using venv.

I would recommend using venv, so that you have local version of the .so in the project folder, which is safer to alter. If you do that, you can easily solve the problem by using the built-in install_name_tool in MacOS to tell that the .so file should use the right .dylib file.

At that point, you can install a newer version of postgres in the project folder, for example postgresql@14, using the pip tool within `venv``

pip install postgresql@14

Therefore you can solve by doing something like this:

install_name_tool -change '/usr/lib/libpq.5.dylib' '/usr/local/Cellar/postgresql@14/14.8/lib/postgresql@14/libpq.5.14.dylib' '/Users/nkosana/yourproject/venv/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so'

Please replace fake names (e.g. "yourproject") with the names you chose and double check as I may have made typos.

I hope this helps.

Kubuntuer82
  • 1,487
  • 3
  • 18
  • 40