skip to Main Content

I set up a project on my old laptop with Postgresql installed on my local, and use Poetry as packages manager. Now I move into another laptop. On this one, I want to use Postgres from Docker so I didn’t install it. But when I run poetry install to set up project from poetry.lock file, It cannot install Psycopg2 with the error:

Error: pg_config executable not found.

As I searched, It requires Postgres to be installed locally. So, how can I install Psycopg2 from the lock file with Postgres from Docker?

2

Answers


  1. PostgreSQL(development or any stable version) should be installed before installing psycopg2. You are planning to use the database server from docker which is perfectly fine. But, to install psycopg2, you need to have Postgres installed in your host machine. However, it is not required in host machine when you run the application in docker as well. You can check this answer to get help on installing psycopg2 in your OS.

    Login or Signup to reply.
  2. You don’t need to install Postgres itself, but you need libpq.

    libpq is the C application programmer’s interface to PostgreSQL. libpq is a set of library functions that allow client programs to pass queries to the PostgreSQL backend server and to receive the results of these queries.

    libpq is also the underlying engine for several other PostgreSQL application interfaces, including those written for C++, Perl, Python, Tcl and ECPG. So some aspects of libpq’s behavior will be important to you if you use one of those packages. In particular, Section 34.15, Section 34.16 and Section 34.19 describe behavior that is visible to the user of any application that uses libpq.

    Therefore, the pyscopg2 documentation recommends installing the packages python3-dev and libpq-dev.

    sudo apt install python3-dev libpq-dev 
    

    The below is written in their docs:

    Psycopg is a C wrapper around the libpq PostgreSQL client library. To install it from sources you will need:

    A C compiler.

    The Python header files. They are usually installed in a package such as python-dev or python3-dev. A message such as error: Python.h: No such file or directory is an indication that the Python headers are missing.

    The libpq header files. They are usually installed in a package such as libpq-dev. If you get an error: libpq-fe.h: No such file or directory you are missing them.

    The pg_config program: it is usually installed by the libpq-dev package but sometimes it is not in a PATH directory. Having it in the PATH greatly streamlines the installation, so try running pg_config –version: if it returns an error or an unexpected version number then locate the directory containing the pg_config shipped with the right libpq version (usually /usr/lib/postgresql/X.Y/bin/) and add it to the PATH:

    $ export PATH=/usr/lib/postgresql/X.Y/bin/:$PATH
    

    If you don’t want that, you can use the pre compiled binary via the python package psycopg2-binary, although it’s not really recommended.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search