PostgreSQL
is an object-relational database that uses and extends the SQL language to scale and store big, complicated data in the database.
In this shot, we will talk about how to connect PostgreSQL
with Python.
pip install pipenv
pipenv shell
pipenv install psycopg2
app.py
.import psycopg2# The try block lets you test a block of code for errors.try:con = psycopg2.connect(database="postgres", user="postgres", password="12345", host="127.0.0.1", port="5432")print("Database opened successfully")# The except block lets you handle the error.except Exception as error:print('database failed to connect')
python app.py
Database opened successfully
. Otherwise, it will print database failed to connect
.