What exactly is Psycopg2?

Posted on

Question :

This question is for a report I’m writing for school. Technically speaking, what is Psycopg2? In this tutorial they refer to it both as an “adapter” and “driver”

Psycopg2 is a DB API 2.0 compliant PostgreSQL driver that is actively
developed. It is designed for multi-threaded applications and manages
its own connection pool. Other interesting features of the adapter are
that if you are using the PostgreSQL array data type, Psycopg will
automatically convert a result using that data type to a Python list.

Since it’s imported into Python, I would’ve guest it was a library?

What’s the distinction between library, driver and adapter?

Answer :

psycopg2 is a wrapper around libpq, written in C, to expose a Python DB-API compatible API to Python programs. It implements Python objects in C that call libpq functions. It has a thin Python module wrapper around it to load it and provide some of the interface functionality that’s easier to write in pure Python.

Anything that implements the DB-API to connect to an external database can be reasonably called a database driver or database adapter. The two are pretty much interchangeable. Any DB-API implementation must be a Python module. Python modules are programming language libraries, in that they’re collections of re-usable code. Some are also C shared libraries, in that they’re compiled C code that gets linked into the cPython executable.

Leave a Reply

Your email address will not be published. Required fields are marked *