Python PostgreSQL - Introduction


PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development phase and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.

To communicate with PostgreSQL using Python you need to install psycopg, an adapter provided for python programming, the current version of this is psycog2.

psycopg2 was written with the aim of being very small and fast, and stable as a rock. It is available under PIP (package manager of python)

Installing Psycog2 using PIP

First of all, make sure python and PIP is installed in your system properly and, PIP is up-to-date.

To upgrade PIP, open command prompt and execute the following command −

C:\Users\Tutorialspoint>python -m pip install --upgrade pip
Collecting pip
   Using cached
Installing collected packages: pip
   Found existing installation: pip 19.0.3
      Uninstalling pip-19.0.3:
         Successfully uninstalled pip-19.0.3
Successfully installed pip-19.2.2

Then, open command prompt in admin mode and execute the pip install psycopg2-binary command as shown below −

C:\WINDOWS\system32>pip install psycopg2-binary
Collecting psycopg2-binary
   Using cached
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.8.3


To verify the installation, create a sample python script with the following line in it.

import mysql.connector

If the installation is successful, when you execute it, you should not get any errors −

D:\Python_PostgreSQL>import psycopg2