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.