Skip to content

Instantly share code, notes, and snippets.

@peter-gy
Forked from bruno-uy/install_psycopg2_mac_m1.md
Created December 21, 2021 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter-gy/0ebe072acb9065944ecb04d95a4c3096 to your computer and use it in GitHub Desktop.
Save peter-gy/0ebe072acb9065944ecb04d95a4c3096 to your computer and use it in GitHub Desktop.
Install psycopg2 in Mac M1

Install psycopg2 in Mac M1

Error while installing through pip install psycopg2 looks like this:

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option (...)

Reference to the solution here.

Steps followed in my local

Install PostgreSQL, start the deamon and install openssl:

brew install postgresql
pg_ctl -D /opt/homebrew/var/postgres start
brew install openssl

If you already have openssl installed, run:

brew reinstall openssl

Find the path of pg_config and openssl:

find / -name pg_config
find / -name openssl

In my case those were located in these folders:

/opt/homebrew/Cellar/postgresql/13.3/bin/pg_config
/opt/homebrew/Cellar/openssl@1.1/1.1.1k/bin/openssl

Add that to $PATH:

export PATH=$PATH:/opt/homebrew/Cellar/postgresql/13.3/bin/pg_config
export PATH=$PATH:/opt/homebrew/Cellar/openssl@1.1/1.1.1k/bin/openssl

Then run: pip install psycopg2.

Problems while executing django command

I encountered the following error while running a django command:

Traceback (most recent call last):
  File "/Users/user/opt/miniconda3/envs/dat/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
    import psycopg2 as Database
  File "/Users/user/opt/miniconda3/envs/dat/lib/python3.7/site-packages/psycopg2/__init__.py", line 51, in <module>
    from psycopg2._psycopg import (                     # noqa
ImportError: dlopen(/Users/user/opt/miniconda3/envs/dat/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so, 2): Symbol not found: _PQbackendPID
  Referenced from: /Users/user/opt/miniconda3/envs/dat/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so
  Expected in: flat namespace
 in /Users/user/opt/miniconda3/envs/dat/lib/python3.7/site-packages/psycopg2/_psycopg.cpython-37m-darwin.so

Following this thread, this finally worked:

brew install libpq --build-from-source
export LDFLAGS="-L/opt/homebrew/opt/libpq/lib"
pip install psycopg2-binary

Before using that, uninstall psycopg2 and psycopg2-binary:

pip uninstall psycopg2-binary
pip uninstall psycopg2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment