Skip to content

Instantly share code, notes, and snippets.

@bruno-uy
Last active March 19, 2024 10:10
Show Gist options
  • Save bruno-uy/f6c7618e7c3d917ba18fa0f7e2d05426 to your computer and use it in GitHub Desktop.
Save bruno-uy/f6c7618e7c3d917ba18fa0f7e2d05426 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:

which pg_config
which 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
@huxzy
Copy link

huxzy commented Oct 14, 2023

This is what worked for me in a M2 processor, because your instructions didn't work for me, since it was still complaining for not finding pg_config:

brew install postgresql@15

Note that you need to specify the version with @. Otherwise, it won't install.

After installing, you will see the message:

==> Caveats
This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /opt/homebrew/var/postgresql@15
For more details, read:
  https://www.postgresql.org/docs/15/app-initdb.html

postgresql@15 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have postgresql@15 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"' >> ~/.zshrc

For compilers to find postgresql@15 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/postgresql@15/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/postgresql@15/include"

To start postgresql@15 now and restart at login:
  brew services start postgresql@15
Or, if you don't want/need a background service you can just run:
  LC_ALL="C" /opt/homebrew/opt/postgresql@15/bin/postgres -D /opt/homebrew/var/postgresql@15
==> Summary
🍺  /opt/homebrew/Cellar/postgresql@15/15.4: 3,698 files, 61.5MB

Just copy the export commands and do one by one the export:

export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/postgresql@15/lib"
export CPPFLAGS="-I/opt/homebrew/opt/postgresql@15/include"

Now do the install: pip install psycopg2.

This worked for me on M2 processor. Thanks

@cyb3rko
Copy link

cyb3rko commented Mar 19, 2024

Thank you!! I've tried so many different approaches until I've found this here.
The Apple silicon really is a pain in terms of software support...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment