What’s it all about?

PostgreSQL is one of the most popular relational database management systems because it is open source and also a lot of its functionality. Using Python, we can easily establish a connection to PostgreSQL, locally or remotely. In Python, we have several modules to connect to PostgreSQL such as SQLAlchemypg8000py-postgresql, etc. However, psycopg2 becomes the most popular one. So that in this tutorial, we will discuss how to connect to PostgreSQL using psycopg2.

The psycopg module to connect a PostgreSQL

According to psycopg official site, the psycopg2 is the most popular PostgreSQL adapter for the Python programming language. Its core is a complete implementation of the Python DB API 2.0 specifications. It’s the core module for this tutorial, so make sure we have installed it in our machine. Use the following command on the terminal to install the psycopg2 module.

$pip3 install psycopg2

How to connect with PostgreSQL

To connect to the PostgreSQL, we must create a file that represents our database on PostgreSQL. We will save that database configuration file to .ini . It keeps our database configuration safe if we share our main Python script to the public. In this case, we will name it database.ini. Please input your database configuration, like hostnamedatabase nameuser namepasswordport number, etc to database.inias follows.

[postgresql]
host = localhost
database = customer
user = postgres
password = admindb
port = 5432

#hands-on-tutorials #data-science #programming #postgresql #data-analytics

Connect to PostgreSQL Database Server Using psycopg2 with an Elegant Configuration File
3.15 GEEK