Repository: psycopg/psycopg2

Issue #1286: psycopg2-binary support for M1 chipset
Created: 2021-06-08T18:51:45Z
Hello all,
With a big of guidance I'm happy to contribute a PR.
For now, let me document the current situation for any other M1 users. Please do correct me if I got the details wrong!
Python 3.8.10 is the first 3.8 version with support for M1 chipset (see bug). Older versions of pyenv do not have access to Python 3.8.10 (some details in here).
pyenv install 3.8.10
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip wheel

If you try to install psycopg2_binary and expect a wheel, it is not currently available.
There's the SYSTEM_VERSION_COMPAT=1 trick to pretend to be 10.6 instead of 11.x, however, the architecture still returns arm64.
❯ python -c "import platform; print(platform.mac_ver())"
('11.4', ('', '', ''), 'arm64')

~/code/M1 via 🐍 v3.8.2 (venv_3_system) on ☁️  armenzg@sentry.io
❯ SYSTEM_VERSION_COMPAT=1 python -c "import platform; print(platform.mac_ver())"
('10.16', ('', '', ''), 'arm64')
If you don't have a wheel then you need to build from source. See the log [1] at the bottom of this comment.
In order to have the libraries to build it, I took these steps which are based on this post:
brew install postgresql
# If you open a new terminal tab you will see that pg_config is available
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L${HOME}/.pyenv/versions/3.8.10/lib"
After that, it is possible to build the wheel
pip install psycopg2-binary==2.8.6
Collecting psycopg2-binary==2.8.6
  Using cached psycopg2-binary-2.8.6.tar.gz (384 kB)
Building wheels for collected packages: psycopg2-binary
  Building wheel for psycopg2-binary (setup.py) ... done
  Created wheel for psycopg2-binary: filename=psycopg2_binary-2.8.6-cp38-cp38-macosx_11_0_arm64.whl size=137096 sha256=af1103eb3c4d9fee1636d27e9dbab6c8bbe7fcd8502bea2a4a194f016a8afb85
  Stored in directory: /Users/armenzg/Library/Caches/pip/wheels/15/d7/13/ef37b82cf1de521c82c129a16ad8007a81b4eab0f23b22c7b3
Successfully built psycopg2-binary
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.6
Did I do this right? How can I help to have a release that includes an arm64 wheel?
[1]
SYSTEM_VERSION_COMPAT=1 pip install psycopg2-binary==2.8.6
Collecting psycopg2-binary==2.8.6
  Using cached psycopg2-binary-2.8.6.tar.gz (384 kB)
    ERROR: Command errored out with exit status 1:
     command: /Users/armenzg/code/M1/venv_3_system/bin/python3 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-install-9zonoxgp/psycopg2-binary_43fe9c32f13f470ebebbbdf290cabfe4/setup.py'"'"'; __file__='"'"'/private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-install-9zonoxgp/psycopg2-binary_43fe9c32f13f470ebebbbdf290cabfe4/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg
         cwd: /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-install-9zonoxgp/psycopg2-binary_43fe9c32f13f470ebebbbdf290cabfe4/
    Complete output (23 lines):
    running egg_info
    creating /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg/psycopg2_binary.egg-info
    writing /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/private/var/folders/j1/d109sd4n55qbk5ty8gzq5jgm0000gn/T/pip-pip-egg-info-cacpi1hg/psycopg2_binary.egg-info/SOURCES.txt'

    Error: pg_config executable not found.

    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:

        python setup.py build_ext --pg-config /path/to/pg_config build ...

    or with the pg_config option in 'setup.cfg'.

    If you prefer to avoid building psycopg2 from source, please install the PyPI
    'psycopg2-binary' package instead.

    For further information please check the 'doc/src/install.rst' file (also at
    <https://www.psycopg.org/docs/install.html>).

    ----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/fc/51/0f2c6aec5c59e5640f507b59567f63b9d73a9317898810b4db311da32dfc/psycopg2-binary-2.8.6.tar.gz#sha256=11b9c0ebce097180129e422379b824ae21c8f2a6596b159c7659e2e5a00e1aa0 (from https://pypi.org/simple/psycopg2-binary/) (requires-python:>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement psycopg2-binary==2.8.6 (from versions: 2.7.4, 2.7.5, 2.7.6, 2.7.6.1, 2.7.7, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6)
ERROR: No matching distribution found for psycopg2-binary==2.8.6

Duplicate issue #1425: problem with psycopg2 in mac M1
Created: 2022-02-16T08:22:53Z
Hey, I just got the same message today. I'm using mac os in M1 and python 3.10 and when I wanted to install psycopg2-binary using pip install psycopg2-binary it complained about not having pg_config executable binary.
Here is the full log:
macbook@Macbooks-MBP ~ % pip install psycopg2-binary 
Collecting psycopg2-binary
  Using cached psycopg2-binary-2.9.3.tar.gz (380 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      running egg_info
      creating /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-_0rsul8n/psycopg2_binary.egg-info
      writing /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-_0rsul8n/psycopg2_binary.egg-info/PKG-INFO
      writing dependency_links to /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-_0rsul8n/psycopg2_binary.egg-info/dependency_links.txt
      writing top-level names to /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-_0rsul8n/psycopg2_binary.egg-info/top_level.txt
      writing manifest file '/private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-_0rsul8n/psycopg2_binary.egg-info/SOURCES.txt'
      
      Error: pg_config executable not found.
      
      pg_config is required to build psycopg2 from source.  Please add the directory
      containing pg_config to the $PATH or specify the full executable path with the
      option:
      
          python setup.py build_ext --pg-config /path/to/pg_config build ...
      
      or with the pg_config option in 'setup.cfg'.
      
      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.
      
      For further information please check the 'doc/src/install.rst' file (also at
      <https://www.psycopg.org/docs/install.html>).
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
macbook@Macbooks-MBP ~ % pip3 install psycopg2-binary
Collecting psycopg2-binary
  Using cached psycopg2-binary-2.9.3.tar.gz (380 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [23 lines of output]
      running egg_info
      creating /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-nuq1g02d/psycopg2_binary.egg-info
      writing /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-nuq1g02d/psycopg2_binary.egg-info/PKG-INFO
      writing dependency_links to /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-nuq1g02d/psycopg2_binary.egg-info/dependency_links.txt
      writing top-level names to /private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-nuq1g02d/psycopg2_binary.egg-info/top_level.txt
      writing manifest file '/private/var/folders/dh/nh40h5j94_xd4hcdj1shjfvm0000gn/T/pip-pip-egg-info-nuq1g02d/psycopg2_binary.egg-info/SOURCES.txt'
      
      Error: pg_config executable not found.
      
      pg_config is required to build psycopg2 from source.  Please add the directory
      containing pg_config to the $PATH or specify the full executable path with the
      option:
      
          python setup.py build_ext --pg-config /path/to/pg_config build ...
      
      or with the pg_config option in 'setup.cfg'.
      
      If you prefer to avoid building psycopg2 from source, please install the PyPI
      'psycopg2-binary' package instead.
      
      For further information please check the 'doc/src/install.rst' file (also at
      <https://www.psycopg.org/docs/install.html>).
      
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details

Closing comment (2022-02-16T10:59:30Z): That the lack of support for the binary package has been already reported in #1286