7

Installing psycopg2 using pip fails on macOS with the error pg_config executable not found It seems to be looking for Postgres installation. I don't want to install Postgres on host os and trying connect Django app with dockerized Postgres.

The app works fine when Django and Postgres are dockerized in separate containers, but keeping the Postgres container up and connecting with host OS Django app does not work. I'm running Django inside a python virtualenv.

Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
Behan Remoshan
  • 136
  • 2
  • 6

2 Answers2

5

You dont need to have postgres server installed.
Psycopg2 needs to have pg_config and libpq installed, there is a binary package psycopg2-binary, that has all dependencies included:

Archive:  psycopg2_binary-2.8.4-cp27-cp27m-manylinux1_i686.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
     6281  10-20-2019 00:46   psycopg2/pool.py
    14082  10-20-2019 00:46   psycopg2/errorcodes.py
    14865  10-20-2019 00:46   psycopg2/sql.py
     4261  10-20-2019 00:46   psycopg2/_lru_cache.py
    43756  10-20-2019 00:46   psycopg2/extras.py
     2929  10-20-2019 00:46   psycopg2/_ipaddress.py
    17667  10-20-2019 00:46   psycopg2/_range.py
     4892  10-20-2019 00:46   psycopg2/__init__.py
     7084  10-20-2019 00:46   psycopg2/extensions.py
   864740  10-19-2019 20:56   psycopg2/_psycopg.so
     4408  10-20-2019 00:46   psycopg2/tz.py
     7258  10-20-2019 00:46   psycopg2/_json.py
     1382  10-20-2019 00:46   psycopg2/errors.py
      367  10-20-2019 00:46   psycopg2/compat.py
   738104  10-19-2019 20:56   psycopg2/.libs/libkrb5-083f5b6f.so.3.3
   106904  10-19-2019 20:56   psycopg2/.libs/libselinux-89f957db.so.1
   637892  10-19-2019 20:56   psycopg2/.libs/libssl-edc39324.so.1.1.1d
     9004  10-19-2019 20:56   psycopg2/.libs/libkeyutils-1-418112b0.2.so
   329164  10-19-2019 20:56   psycopg2/.libs/libpq-2be1e2df.so.5.11
   428784  10-19-2019 20:56   psycopg2/.libs/libldap_r-2-2e3da1cd.4.so.2.10.11
   251840  10-19-2019 20:56   psycopg2/.libs/libsepol-5bd02592.so.1
  3050920  10-19-2019 20:56   psycopg2/.libs/libcrypto-7d5df85f.so.1.1.1d
   171928  10-19-2019 20:56   psycopg2/.libs/libk5crypto-92a64cbe.so.3.1
   240952  10-19-2019 20:56   psycopg2/.libs/libgssapi_krb5-e4e762a2.so.2.2
    75576  10-19-2019 20:56   psycopg2/.libs/libz-83853723.so.1.2.3
     8852  10-19-2019 20:56   psycopg2/.libs/libcom_err-caa8c98e.so.2.1
    55692  10-19-2019 20:56   psycopg2/.libs/liblber-2-9ed85788.4.so.2.10.11
   138640  10-19-2019 20:56   psycopg2/.libs/libsasl2-fa5e3f6a.so.3.0.0
    42920  10-19-2019 20:56   psycopg2/.libs/libkrb5support-4db1495c.so.0.1
     2238  10-20-2019 00:56   psycopg2_binary-2.8.4.dist-info/LICENSE.txt
      107  10-19-2019 20:56   psycopg2_binary-2.8.4.dist-info/WHEEL
        9  10-20-2019 00:56   psycopg2_binary-2.8.4.dist-info/top_level.txt
     4297  10-20-2019 00:56   psycopg2_binary-2.8.4.dist-info/METADATA
     3029  10-19-2019 20:56   psycopg2_binary-2.8.4.dist-info/RECORD
---------                     -------
  7290824                     34 files
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
  • 1
    I'm trying it on linux Alpine but binary installation is not working .. one of the error is Error: pg_config executable not found. – Ameer Ul Islam Aug 29 '21 at 11:07
2

psycopg2 uses libpq (and other libraries) to connect to the PostgreSQL database, so at the very least you will need some form of the libraries and headers that are associated with PostgreSQL install. Without these libraries and header files, psycopg2 cannot be compiled. pg_config simply tells the compiler where to find these dependencies.

I'm not sure what your reasons against installing Postgres are, but another possibility is to download and install Postgres.app, which should have all the info you need to compile psycopg2, nicely packaged within your /Applications folder.

A similar question and answer for this situation can be found here

richyen
  • 8,114
  • 4
  • 13
  • 28
  • To clarify: of course it is not necessary to install the PostgreSQL *server*. – Laurenz Albe Oct 21 '19 at 05:03
  • Installing Postgres.app is like again installing the server? How does the python:3.6 docker image installs the pycopg2, does it come with Postgres files? – Behan Remoshan Oct 21 '19 at 06:30
  • The `python:3.6` image comes with `libpq` installed, as `libpq-dev` provides `/usr/bin/pg_config` (see `dpkg -S /usr/bin/pg_config`) – richyen Oct 21 '19 at 20:55