[SOLVED] psycopg2 installed on M1 running MacOS Big Sur, but unable to run local server


This Content is from Stack Overflow. Question asked by TechSelfLearner

I have a M1 Max Mac(released in October 2021), and installed Django 4.1.1. I was trying to have my Django app connected to a PostgreSql database I created under DATABASES in settings.py. Successfully installed psycopg2-binary using pip3 install psycopg2-binary. In ENGINE I have django.db.backends.postgresql_psycopg2. When I run python3 manage.py runserver, I kept getting this error:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/hahaha/codingProjects/django/env/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    import psycopg2 as Database
  File "/Users/hahaha/codingProjects/django/env/lib/python3.9/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/hahaha/codingProjects/django/env/lib/python3.9/site-packages/psycopg2/_psycopg.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace (_PQbackendPID)

I was following the step from here to tackle the issue and then got

You are using macOS 12.
We do not provide support for this pre-release version.
You will encounter build failures with some formulae.

and since then no success. Some guidance needed


Installing libpq from source got things working again. Of course, do this in the Rosetta terminal.

$ brew install libpq --build-from-source

$ export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"

$ pip install psycopg2

Courtesy: https://github.com/psycopg/psycopg2/issues/1216#issuecomment-767892042

If it still doesn’t work, try

brew install openssl

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

then install psycopg2

pip3 install psycopg2

This Question was asked in StackOverflow by Monitis_support and Answered by Shyam It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?