Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

psycopg2-binary fails to install on macOS Big Sur 11.0.1 and Python 3.9.0 (with possible workaround) #1200

Closed
akurani opened this issue Nov 21, 2020 · 69 comments

Comments

@akurani
Copy link

akurani commented Nov 21, 2020

Environment:

  • macOS 11.0.1 (Big Sur)
  • Python 3.9.0 (installed via pyenv)

When I attempt to pip-install psycopg2-binary with in a venv, the installer instead tries to compile psycopg2 and fails for me as I don't have PostgreSQL installed:

$ pip install psycopg2-binary
Collecting psycopg2-binary
  Using cached psycopg2-binary-2.8.6.tar.gz (384 kB)
    ERROR: Command errored out with exit status 1:
     command: /Users/amar/test3/.venv/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-install-jo9na1rs/psycopg2-binary/setup.py'"'"'; __file__='"'"'/private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-install-jo9na1rs/psycopg2-binary/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx
         cwd: /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-install-jo9na1rs/psycopg2-binary/
    Complete output (23 lines):
    running egg_info
    creating /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx/psycopg2_binary.egg-info
    writing /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/private/var/folders/73/97rprb4s6qz9qc7n8xy0vt800000gn/T/pip-pip-egg-info-mgvwjdyx/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>).

I can get around this using the SYSTEM_VERSION_COMPAT=1 env var. That sets the macOS version to 10.16 instead of 11.0:

$ SYSTEM_VERSION_COMPAT=1 pip install psycopg2-binary
Collecting psycopg2-binary
  Downloading psycopg2_binary-2.8.6-cp39-cp39-macosx_10_9_x86_64.macosx_10_9_intel.macosx_10_10_intel.macosx_10_10_x86_64.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 5.1 MB/s 
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.6

Note, I haven't tested using this psycopg2-binary on Big Sur yet. I'm running into install issues with Pillow that I'm sorting out too. (How I wish I didn't upgrade to Big Sur yet!)

@dvarrazzo
Copy link
Member

Probably the last and greatest Mac OS is not compatible with wheels packages.

You should hear from the Python Package Authority, or from Apple.

@AAraKKe
Copy link

AAraKKe commented Nov 27, 2020

Hi, I am using Catalina (10.15.7) and I also had the same issue Error: pg_config executable not found..

In our case the issue went away updating the version we were using of psycopg2. We were using python 3.7 with pyscopg2 2.7.5 and everything was working fine. We are now updating python to 3.9 and when trying to install requirements we found that psycopg could not find pg_config. This error also happened if trying to install 2.7.5 with python 3.8.

Allowing the version to be ~=2.8.0 fixed it and now it can install properly. This has nothing to do with the original issue raised but I just wanted to leave it here in case it triggers some thought and someone arrives here with the same issue I had before :)

@hsnprsd
Copy link

hsnprsd commented Nov 28, 2020

Hey, I just got the same message today. I'm using fedora 33 and python 3.9 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:

Collecting psycopg2-binary==2.7.7
  Downloading psycopg2-binary-2.7.7.tar.gz (428 kB)
     |████████████████████████████████| 428 kB 451 kB/s 
    ERROR: Command errored out with exit status 1:
     command: /home/ehsan/.virtualenvs/CQ/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-5_22rja8/psycopg2-binary/setup.py'"'"'; __file__='"'"'/tmp/pip-install-5_22rja8/psycopg2-binary/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-1utvtzmt
         cwd: /tmp/pip-install-5_22rja8/psycopg2-binary/
    Complete output (23 lines):
    running egg_info
    creating /tmp/pip-pip-egg-info-1utvtzmt/psycopg2_binary.egg-info
    writing /tmp/pip-pip-egg-info-1utvtzmt/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /tmp/pip-pip-egg-info-1utvtzmt/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /tmp/pip-pip-egg-info-1utvtzmt/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/tmp/pip-pip-egg-info-1utvtzmt/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
    <http://initd.org/psycopg/docs/install.html>).
    
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

@dvarrazzo
Copy link
Member

@hsnprsd you are trying to install 2.7.7, which was released way before Python 3.9. We don't have a time machine: MR for time travel features are well received.

Binary packages for Python 3.9 for linux and windows are available only from 2.8.6 on.

Mac OS has released a new version and as per usual they have broken everything they could. So this ticket is about MacOS, not windows and linux. I'd appreciate if it remained that way.

@hsnprsd
Copy link

hsnprsd commented Nov 28, 2020

@dvarrazzo Sorry, you are right. I wasn't aware I am installing an old version of psycopg.

@nilnullzip
Copy link

I found this problem was resolved by upgrading pip:

python3 -m pip install --upgrade pip

Running pip prints warnings to upgrade.

@Nkarnaud
Copy link

Nkarnaud commented Jan 30, 2021

I am using Mac os big sure and this works for me.
install openssl using brew

brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2

pip3 install psycopg2

@shreyagupta30
Copy link

I am using Mac os big sure and this works for me.
install openssl using brew

brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2

pip3 install psycopg2

Hi @Nkarnaud , I am using Big Sur on Apple silicon. This doesn't work for me. Can you help? :(

@Nkarnaud
Copy link

Nkarnaud commented Feb 1, 2021

I am using Mac os big sure and this works for me.
install openssl using brew
brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2
pip3 install psycopg2

Hi @Nkarnaud , I am using Big Sur on Apple silicon. This doesn't work for me. Can you help? :(

Hi @shreyagupta30, can you share the error you are having?

@shreyagupta30
Copy link

shreyagupta30 commented Feb 1, 2021

I am using Mac os big sure and this works for me.
install openssl using brew
brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2
pip3 install psycopg2

Hi @Nkarnaud , I am using Big Sur on Apple silicon. This doesn't work for me. Can you help? :(

Hi @shreyagupta30, can you share the error you are having?

ERROR: Command errored out with exit status 1: /Library/Developer/CommandLineTools/usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-install-nyhue42a/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-install-nyhue42a/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-record-v6umrnqf/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.

Here @Nkarnaud

@Nkarnaud
Copy link

Nkarnaud commented Feb 1, 2021

I am using Mac os big sure and this works for me.
install openssl using brew
brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2
pip3 install psycopg2

Hi @Nkarnaud , I am using Big Sur on Apple silicon. This doesn't work for me. Can you help? :(

Hi @shreyagupta30, can you share the error you are having?

ERROR: Command errored out with exit status 1: /Library/Developer/CommandLineTools/usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-install-nyhue42a/psycopg2/setup.py'"'"'; __file__='"'"'/private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-install-nyhue42a/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/j8/2nzz29zn0439c37m5klxf9mh0000gn/T/pip-record-v6umrnqf/install-record.txt --single-version-externally-managed --compile Check the logs for full command output.

Here @Nkarnaud

check this it may be of help. https://stackoverflow.com/questions/62877588/psycopg2-gives-error-while-installing-in-macos-catalina-10-15-5

@cheslijones
Copy link

cheslijones commented Feb 9, 2021

Adding that I'm having similar issues with the following:

  • Apple M1
  • Big Sur 11.2
  • Python 3.8.2
  • Pip 21.0.1
$ pip install psycopg2-binary --no-cache-dir
Collecting psycopg2-binary
  Downloading psycopg2-binary-2.8.6.tar.gz (384 kB)
     |████████████████████████████████| 384 kB 25.7 MB/s 
    ERROR: Command errored out with exit status 1:
     command: /Users/m1/company-app/api/.venv/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-install-ccqum8r8/psycopg2-binary_08a234e704634109bb83b0b7c6b8ca28/setup.py'"'"'; __file__='"'"'/private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-install-ccqum8r8/psycopg2-binary_08a234e704634109bb83b0b7c6b8ca28/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise
         cwd: /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-install-ccqum8r8/psycopg2-binary_08a234e704634109bb83b0b7c6b8ca28/
    Complete output (23 lines):
    running egg_info
    creating /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise/psycopg2_binary.egg-info
    writing /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise/psycopg2_binary.egg-info/PKG-INFO
    writing dependency_links to /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise/psycopg2_binary.egg-info/dependency_links.txt
    writing top-level names to /private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise/psycopg2_binary.egg-info/top_level.txt
    writing manifest file '/private/var/folders/9y/z49zz0951k3gkbnp0mb4f13w0000gn/T/pip-pip-egg-info-ybwazise/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.

I ran into the same issue with SYSTEM_VERSION_COMPAT=1 pip install psycopg2-binary.

@dvarrazzo
Copy link
Member

@cheslijones

Pip 21.0.1

Not that I know whether psycopg works on Mac M1 or not. However the words "Make sure to use an up-date-date version of pip" written in the documentation are not there just to waste electrons. With such an old pip version you don't stand a chance.

@cheslijones
Copy link

cheslijones commented Feb 9, 2021

@cheslijones

Pip 21.0.1

Not that I know whether psycopg works on Mac M1 or not. However the words "Make sure to use an up-date-date version of pip" written in the documentation are not there just to waste electrons. With such an old pip version you don't stand a chance.

Pretty sure 21.0.1 is the latest pip version unless I'm reading this wrong:

https://pypi.org/project/pip/

Released January 30, 2021

@dvarrazzo
Copy link
Member

Pretty sure 21.0.1 is the latest pip version unless I'm reading this wrong:

mmm... you are right, apologies. I thought we were in the 40s by now.

Ok, keep on having fun with your macs.

@cheslijones
Copy link

Actually, figured it out. Haven't worked on a Mac in a while and forgot about the PostgreSQL dependencies that have to be installed before psycopg2-binary. Never have to do this with Linux so forgot about it...

$ brew install postgresql

$ brew link openssl
Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

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

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"

Follow the exports that it shows in the message. Then it worked.

@dvarrazzo
Copy link
Member

Is there anything standard in all these paths that mac os have to change every single time? is there a MR someone can propose against the setup.py to make your life easier?

@hkennyv
Copy link

hkennyv commented Feb 10, 2021

can confirm @cheslijones's method was successful for me on an M1 Macbook Air.

LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib" CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include" PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig" pip install psycopg2-binary

$ LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib" CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include" PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig" pip install psycopg2-binary
Collecting psycopg2-binary
  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-cp39-cp39-macosx_11_0_arm64.whl size=137210 sha256=8f65c425b29f41ff85ff2bff51bf787169c8196c7a70809308f89d7e64e4ed22
  Stored in directory: /Users/khuynh/Library/Caches/pip/wheels/21/fa/fd/4d20397b9959492f88bfa4ba6f993d48de49b93001f90bdda3
Successfully built psycopg2-binary
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.6

It seems the confusion is because brew recommends installing to /usr/local for rosetta and intel and to /opt/homebrew for ARM [1].

[1] https://docs.brew.sh/Installation

@aleks-dontsu
Copy link

  1. install iTerm
  2. run iTerm using Roseta 2
  3. install venv, after in venv:
  4. pip3 install psycopg2-binary
  5. ...
  6. run Django project in iTerm (with Roseta2)

@rsazima
Copy link

rsazima commented Mar 14, 2021

This one works: LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib" CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include" PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig" pip install psycopg2-binary

@junyeongchoi-29cm
Copy link

I recommend you to install postgres first.
brew install postgres
pip3 install psycopg2

@GuillaumeHuin
Copy link

GuillaumeHuin commented Mar 27, 2021

Hi there,

I found out a way for installing psycopg2 on a ARM architecture : use a conda virtual environment using miniforge3. This is recommended by apple for installing ML package tensorflow (apparently optimised for the new chip). Here is the miniforge repo for installation :
[1] https://github.com/conda-forge/miniforge#miniforge3

Eventually you can create a yml file to remove some ML package that is shipped along conda. I am not sure this solution is optimal but to me it looks much cleaner that exporting flags in a specific location on the file system.

@darylyu
Copy link

darylyu commented Apr 3, 2021

I wrote about how to get around this in the psycopg2 section of a guide for setting up a Django development environment on an M1 machine.

I used Python 3.8 from Homebrew and Postgres 11 from Homebrew, but long story short the solution that worked for me looks something like this:

export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib -L/opt/homebrew/opt/python@3.8/lib -L/opt/homebrew/opt/icu4c/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -I/opt/homebrew/opt/icu4c/include"
export PATH=/opt/homebrew/opt/postgresql@11/bin:$PATH

@feconroses
Copy link

feconroses commented Apr 4, 2021

Hey 👋 I struggled with installing psycopg2 on the new Apple Mac M1. The solution that finally worked for me for installing not only psycopg2, but also other packages (e.g. pandas and numpy) was using Conda (miniforge3). Created a virtual environment with conda, installed these packages that were having some problems with the M1 (such as psycopg2) with conda install psycopg2 and it worked!

@mahi-official
Copy link

Hi, I'm able to install "psycopg2-binary" locally but getting error while installing the same in a virtual environment.
Look at the logs below:

In file included from psycopg/win32_support.c:28:
In file included from ./psycopg/psycopg.h:38:
./psycopg/config.h:82:13: warning: unused function 'Dprintf' [-Wunused-function]
static void Dprintf(const char *fmt, ...) {}
            ^
1 warning generated.
gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -DPSYCOPG_VERSION=2.9.dev0 (dt dec pq3 ext lo64) -DPG_VERSION_NUM=130002
gcc -bundle -undefined dynamic_lookup -arch arm64 -arch x86_64 -g build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_asis.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_binary.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_datetime.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_list.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_pboolean.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_pdecimal.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_pfloat.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_pint.o build/temp.macosx-10.9-universal2-3.9/psycopg/adapter_qstring.o build/temp.macosx-10.9-universal2-3.9/psycopg/aix_support.o build/temp.macosx-10.9-universal2-3.9/psycopg/bytes_format.o build/temp.macosx-10.9-universal2-3.9/psycopg/column_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/connection_int.o build/temp.macosx-10.9-universal2-3.9/psycopg/connection_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/conninfo_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/cursor_int.o build/temp.macosx-10.9-universal2-3.9/psycopg/cursor_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/diagnostics_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/error_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/green.o build/temp.macosx-10.9-universal2-3.9/psycopg/libpq_support.o build/temp.macosx-10.9-universal2-3.9/psycopg/lobject_int.o build/temp.macosx-10.9-universal2-3.9/psycopg/lobject_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/microprotocols.o build/temp.macosx-10.9-universal2-3.9/psycopg/microprotocols_proto.o build/temp.macosx-10.9-universal2-3.9/psycopg/notify_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/pqpath.o build/temp.macosx-10.9-universal2-3.9/psycopg/psycopgmodule.o build/temp.macosx-10.9-universal2-3.9/psycopg/replication_connection_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/replication_cursor_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/replication_message_type.o build/temp.macosx-10.9-universal2-3.9/psycopg/solaris_support.o build/temp.macosx-10.9-universal2-3.9/psycopg/typecast.o build/temp.macosx-10.9-universal2-3.9/psycopg/utils.o build/temp.macosx-10.9-universal2-3.9/psycopg/win32_support.o build/temp.macosx-10.9-universal2-3.9/psycopg/xid_type.o -L/opt/homebrew/lib -lpq -lssl -lcrypto -o build/lib.macosx-10.9-universal2-3.9/psycopg2/_psycopg.cpython-39-darwin.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/gcc' failed with exit code 1

Using M1 with python3.9
Latest version of pip
exported Postgresql path, LDFlags and CPPFlags.
(BTW only after above steps i was able to install it locally)
But i need to use it within my django project which is in a virtualenv running on same configs.
PS: i'll try to copy psycopg2-binaries from local to virtualenv, maybe that can work out.

@Olney1
Copy link

Olney1 commented Jun 10, 2021

@meistiyak I will release 2.9 with the fix in a few days.

Any luck with that update? Tried all of the above and not working for me. I have lots of nice new packages though.

@dvarrazzo
Copy link
Member

Psycopg 2.9 has been released, which should solve the build problem with Mac OS.

The binary packages are not compatible with the Apple M1 in ARM mode, but building should be fixed. See #1286 for M1 support.

@meistiyak
Copy link

Thanks @dvarrazzo

@thewolfcommander
Copy link

Actually, figured it out. Haven't worked on a Mac in a while and forgot about the PostgreSQL dependencies that have to be installed before psycopg2-binary. Never have to do this with Linux so forgot about it...

$ brew install postgresql

$ brew link openssl
Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

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

For pkg-config to find openssl@1.1 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"

Follow the exports that it shows in the message. Then it worked.

You are great bro... was getting frustrated over this. Thanks a lot

@CsabaHalmagyi
Copy link

What worked for me on Mac:

$ brew install postgresql

$ brew link openssl
Warning: Refusing to link macOS provided/shadowed software: openssl@1.1
If you need to have openssl@1.1 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"' >> ~/.zshrc

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

For pkg-config to find openssl@1.1 you may need to set:
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"

Then I downloaded and installed PG Admin from https://www.pgadmin.org/download/
Then my system will be able to resolve the psycopg2 requirements and "pip install psycopg2-binary" worked just fine.

@pedromassango
Copy link

This issue still persists

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 25, in <module>
    import psycopg2 as Database
ModuleNotFoundError: No module named 'psycopg2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/code/manage.py", line 22, in <module>
    main()
  File "/code/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/usr/local/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.9/site-packages/django/apps/config.py", line 301, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 855, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/local/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/usr/local/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/options.py", line 207, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/usr/local/lib/python3.9/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 29, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
ERROR: 1

@hoanghungbk93
Copy link

pip install --upgrade wheel
pip install --upgrade setuptools
pip install psycopg2

@sikorskydenis
Copy link

@jatinduracube
Copy link

I am using Mac os big sure and this works for me.
install openssl using brew
brew install openssl
after that export these variables in the terminal.

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

then finally install psycopg2
pip3 install psycopg2

Hi @Nkarnaud , I am using Big Sur on Apple silicon. This doesn't work for me. Can you help? :(

Thanks, it worked for me

@thebogdan
Copy link

I am using macOS Big Sur 11.5.2 and this worked for me

brew install openssl

(which installed openssl@1.1 1.1.1l)

export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
python3.9 -m pip install -U pip setuptools wheel
python3.9 -m pip install psycopg2-binary

Successfully installed psycopg2-binary-2.9.1

@dvarrazzo
Copy link
Member

@thebogdan out of curiosity:

  • does the above work if you install psycopg instead of psycopg-binary?
  • does the above work if you don't specify the env vars?

thank you

@wlad
Copy link

wlad commented Sep 30, 2021

@thebogdan out of curiosity:

  • does the above work if you install psycopg instead of psycopg-binary?
  • does the above work if you don't specify the env vars?

thank you

nope. unfortunately not.

@wlad
Copy link

wlad commented Sep 30, 2021

Here's what worked for me:

  • update Python to 3.9.7 (⚠️ make sure to download the python-3.9.7-macos11.pkg from python.org - the other one is for Intel based Macs)
  • brew install postgresql
  • optional: create a venv (python3 -m venv myvenv)
  • pip install psycopg2-binary --force-reinstall --no-cache-dir

check

❯ pip list
Package         Version
--------------- -------
pip             21.2.4
psycopg2-binary 2.9.1
setuptools      57.4.0

@moonvent
Copy link

moonvent commented Oct 3, 2021

This problem may be from not installed postgre (I use postgre from docker), cause install postgre
brew install postgresql
And, after this (postgre is off default), try install psycopg2. It's help to me (Mac mini m1)

@DarkSuniuM
Copy link

DarkSuniuM commented Oct 5, 2021

A lot of answers, suggest installing postgresql, It's the whole PostgreSQL Package, what's the point of installing psycopg2-binary if we are going to install postgresql on the system?

Anyway, My solution was to install libpq instead of postgresql, it contains only the C Library API which is required to build the psycopg2-binary.

$ brew install libpq

add the libpq binaries path to your $PATH, I have a MacBook Pro M1 and I use ZSH, may be a bit different for other Macbooks and Shells.

$ echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

Restart your Terminal (or reload shell) and install the package

$ pip3 install psycopg2-binary --force-reinstall --no-cache-dir

@wlad
Copy link

wlad commented Oct 6, 2021

@DarkSuniuM I haven't try yet but I guess it's safe to uninstall postgresql after psycopg2-binary was successfully installed(?)

@DarkSuniuM
Copy link

@DarkSuniuM I haven't try yet but I guess it's safe to uninstall postgresql after psycopg2-binary was successfully installed(?)

Yes, it is, but if you have multiple projects inside of multiple virtual envs, then you wanna keep it installed for the sake of next projects,

Also, as you read in my response, you can replace it with libpq

@shoaib30
Copy link

A lot of answers, suggest installing postgresql, It's the whole PostgreSQL Package, what's the point of installing psycopg2-binary if we are going to install postgresql on the system?

Anyway, My solution was to install libpq instead of postgresql, it contains only the C Library API which is required to build the psycopg2-binary.

$ brew install libpq

add the libpq binaries path to your $PATH, I have a MacBook Pro M1 and I use ZSH, may be a bit different for other Macbooks and Shells.

$ echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc

Restart your Terminal (or reload shell) and install the package

$ pip3 install psycopg2-binary --force-reinstall --no-cache-dir

The idea to use libqp is a lot better than using postgres, but you will need to install openssl too

brew install openssl

and set the path for the same as mentioned in above comments, which in my case had a different path compared to what is mentioned above.

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"

After this I am able to use both psycopg2 as well as psycopg2-binary

@Radilx

This comment was marked as off-topic.

mergify bot pushed a commit to andrewbolster/bolster that referenced this issue Nov 15, 2021
Bumps [psycopg2-binary](https://github.com/psycopg/psycopg2) from 2.9.1 to 2.9.2.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/psycopg/psycopg2/blob/master/NEWS">psycopg2-binary's changelog</a>.</em></p>
<blockquote>
<h2>Current release</h2>
<p>What's new in psycopg 2.9.2
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Raise <code>ValueError</code> for dates &gt;= Y10k (:ticket:<code>[#1307](https://github.com/psycopg/psycopg2/issues/1307)</code>).</li>
<li><code>~psycopg2.errorcodes</code> map and <code>~psycopg2.errors</code> classes updated to
PostgreSQL 14.</li>
<li>Add preliminary support for Python 3.11 (:tickets:<code>[#1376](psycopg/psycopg2#1376), [#1386](https://github.com/psycopg/psycopg2/issues/1386)</code>).</li>
<li>Wheel package compiled against OpenSSL 1.1.1l and PostgreSQL 14.1
(:ticket:<code>[#1388](https://github.com/psycopg/psycopg2/issues/1388)</code>).</li>
</ul>
<p>What's new in psycopg 2.9.1
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<p>Fix regression with named <code>sql.Placeholder</code> (:ticket:<code>[#1291](https://github.com/psycopg/psycopg2/issues/1291)</code>).</p>
<h2>What's new in psycopg 2.9</h2>
<ul>
<li><code>with connection</code> starts a transaction on autocommit transactions too
(:ticket:<code>[#941](https://github.com/psycopg/psycopg2/issues/941)</code>).</li>
<li>Timezones with fractional minutes are supported on Python 3.7 and following
(:ticket:<code>[#1272](https://github.com/psycopg/psycopg2/issues/1272)</code>).</li>
<li>Escape table and column names in <code>~cursor.copy_from()</code> and
<code>~cursor.copy_to()</code>.</li>
<li>Connection exceptions with sqlstate <code>08XXX</code> reclassified as
<code>~psycopg2.OperationalError</code> (a subclass of the previously used
<code>~psycopg2.DatabaseError</code>) (:ticket:<code>[#1148](https://github.com/psycopg/psycopg2/issues/1148)</code>).</li>
<li>Include library dirs required from libpq to work around MacOS build problems
(:ticket:<code>[#1200](https://github.com/psycopg/psycopg2/issues/1200)</code>).</li>
</ul>
<p>Other changes:</p>
<ul>
<li>Dropped support for Python 2.7, 3.4, 3.5 (:tickets:<code>[#1198](psycopg/psycopg2#1198), [#1000](psycopg/psycopg2#1000), [#1197](https://github.com/psycopg/psycopg2/issues/1197)</code>).</li>
<li>Dropped support for mx.DateTime.</li>
<li>Use <code>datetime.timezone</code> objects by default in datetime objects instead of
<code>~psycopg2.tz.FixedOffsetTimezone</code>.</li>
<li>The <code>psycopg2.tz</code> module is deprecated and scheduled to be dropped in the
next major release.</li>
<li>Provide :pep:<code>599</code> wheels packages (manylinux2014 tag) for i686 and x86_64
platforms.</li>
<li>Provide :pep:<code>600</code> wheels packages (manylinux_2_24 tag) for aarch64 and
ppc64le platforms.</li>
<li>Wheel package compiled against OpenSSL 1.1.1k and PostgreSQL 13.3.</li>
<li>Build system for Linux/MacOS binary packages moved to GitHub Actions.</li>
</ul>

</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li>See full diff in <a href="https://github.com/psycopg/psycopg2/commits">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=psycopg2-binary&package-manager=pip&previous-version=2.9.1&new-version=2.9.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>
@korivernon
Copy link

I recommend you to install postgres first. brew install postgres pip3 install psycopg2

This worked for me
Apple M1 Pro Macbook
Monterey 12.0.1

@psycopg psycopg locked as resolved and limited conversation to collaborators Dec 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests