18

enter image description here

pg_config

venv ❯ which pg_config

/Applications/Postgres.app/Contents/Versions/latest/bin/pg_config

pip3 install -r requirements.txt

requirements.txt

venv ❯ cat requirements.txt
-i https://mirrors.aliyun.com/pypi/simple/
alembic==1.4.2
amqp==2.6.0
billiard==3.6.3.0
celery==4.4.6
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
emoji==0.5.4
fastapi==0.59.0
future==0.18.2
h11==0.9.0
httptools==0.1.1 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
idna==2.10
kombu==4.6.11
mako==1.1.3
markupsafe==1.1.1
psycopg2-binary==2.8.5
pydantic==1.6
python-dateutil==2.8.1
python-editor==1.0.4
pytz==2020.1
redis==3.5.3
requests==2.24.0
six==1.15.0
sqlalchemy==1.3.18
starlette==0.13.4
tenacity==6.2.0
urllib3==1.25.9
uvicorn==0.11.5
uvloop==0.14.0 ; sys_platform != 'win32' and sys_platform != 'cygwin' and platform_python_implementation != 'PyPy'
vine==1.3.0
websockets==8.1

full error message

Process SpawnProcess-1:
Traceback (most recent call last):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
    self.run()
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/subprocess.py", line 62, in subprocess_started
    target(sockets=sockets)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/main.py", line 382, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/main.py", line 389, in serve
    config.load()
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/config.py", line 288, in load
    self.loaded_app = import_from_string(self.app)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/importer.py", line 23, in import_from_string
    raise exc from None
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./main.py", line 4, in <module>
    from api.api_v1.api import api_router
  File "./api/api_v1/api.py", line 4, in <module>
    from api.api_v1.endpoints import status
  File "./api/api_v1/endpoints/status.py", line 10, in <module>
    from db.session import Session
  File "./db/session.py", line 7, in <module>
    engine = create_engine(config.SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 753, in dbapi
    import psycopg2
  File "/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so, 2): Symbol not found: _PQbackendPID
  Referenced from: /Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so
  Expected in: flat namespace
 in /Users/lidashuang/Projects/starservices/venv/lib/python3.8/site-packages/psycopg2/_psycopg.cpython-38-darwin.so
lidashuang
  • 1,975
  • 4
  • 20
  • 22
  • 4
    How do you install it - `pip3 install -r requirements.txt` ? See if this helps [install Psycopg2 on mac osx 10.9.5](https://stackoverflow.com/questions/33866695/install-psycopg2-on-mac-osx-10-9-5) - it may be that you're missing some libraries or other software in your OS and there's a choice between **psychopg2** and **psychopg2-binary**. – dmitryro Nov 29 '20 at 11:02
  • Similar (albeit quite old) https://stackoverflow.com/questions/6999105/mac-os-x-lion-psycopg2-symbol-not-found-pqbackendpid – snakecharmerb Nov 29 '20 at 11:13
  • Upgrade python 3.9 solved psycopg2 package Symbol not found: _PQbackendPID – lidashuang Apr 08 '21 at 09:53

8 Answers8

28

Try installing postgres via homebrew:

brew install postgresql

For some reason, this seems to install the right libraries in the right directories so that you can properly build and import psycopg2-binary on an M1 Mac (worked in my case anyway).

After installing postgres with brew, uninstall then reinstall psycopg2, e.g.:

pip3.9 install psycopg2-binary --force-reinstall --no-cache-dir

Note that even though this will install a new version of postgres, you don't need to actually use the new version to make the psycopg2 import work.

Ben Wilson
  • 2,271
  • 3
  • 26
  • 35
  • 1
    Had this issue `Symbol not found: _PQbackendPID` after trying to run Odoo 14 and this worked. thanks – Leo Fisher May 08 '21 at 00:05
  • 2
    This worked me after trying many other solutions. thank you! I have M1 mac and running python 3.9 for reference – ehka Jun 22 '21 at 16:13
  • 2
    I also had the `Symbol not found: _PQbackendPID` issue on an M1 Mac and macOS 11.4 and installing Postgres using Homebrew (even though I already had the Postgres app) worked. – Jurie Horneman Aug 03 '21 at 15:34
  • 1
    @Wlad that worked for me, but there's a typo, should be `$ brew install libpq` . I also had to manually add to my `.zshrc` as mentioned in the GitHub issue you linked. – Willow Dec 04 '21 at 19:20
  • `brew install libpq` should be enough as stated in this psycogp2 Github issue github.com/psycopg/psycopg2/issues/1200#issuecomment-934861409 – Wlad Dec 05 '21 at 14:17
  • @Willow thanks for the hint, I've updated my comment. – Wlad Dec 05 '21 at 14:17
  • Had the same issue when installing Odoo 12 which doesn't seem to be compatible with python 3.9 => this solved it: pip3.8 install psycopg2-binary --force-reinstall --no-cache-dir No need to brew install python – ndemoreau Jun 23 '23 at 07:21
5

Miniconda (an alternative to virtualenv) has a Python 3.8 installer that works with psycopg2 on M1 Macs without the _PQbackendPID error: https://docs.conda.io/en/latest/miniconda.html

ridddlr
  • 51
  • 4
1

In my case, using the following worked:

  1. using miniconda
  2. using psycopg2-binary
William Baker Morrison
  • 1,642
  • 4
  • 21
  • 33
wnslwnsl2
  • 21
  • 1
1
brew install libpq
brew install openssl

Follow the instructions after brew completes both times.

Lauri Elias
  • 1,231
  • 1
  • 21
  • 25
1

Are you creating a virtual environment using pycharm?

Remove the virtual env (if already created)

Press ⌘ , to open the project Settings/Preferences and go to Project | Python Interpreter. Then click The Configure project interpreter icon and select Show All. Then click on '-' to remove the venv.

Create the virtual environment manually using the below command

python3 -m venv /path/to/venv

Activate the virtual venv

source venv/bin/activate

Install binary package of psycopg2

pip3 install psycopg2-binary 
eliarms
  • 551
  • 4
  • 6
1

As for people using poetry. After you installed postgres via homebrew, (Also Make sure the installation is successful)

brew install postgresql

Then I follow this steps to get it to work.

First I remove my current poetry env with

poetry env remove [your env]

Then, clear poetry cache

poetry cache clear --all [your cache]

e.g.

poetry cache clear --all .

Then spawn a new poetry env, and you can install psycopg2 with

poetry add psycopg2-binary
Teamata Leoneos
  • 85
  • 1
  • 11
1

It's fixed in psycopg2-binary==2.9.3 and higher, so just upgrade your package.

Nexus
  • 749
  • 6
  • 12
0

using exactly the 2.9.3 version fixed it for me. MBP M1.

2.9.6 version was also giving the same error. installed in venv with below command.

python -m pip install psycopg2-binary==2.9.3