Establishing a connection with the PostgreSQL database
As the first steps establish a connection with your existing database, using the connect() function of Psycopg2.
Python3
from psycopg2 import connect # declare the connection string specifying # the host name database name # use name and password conn_string = "host = 'host_name' \ dbname = 'database_name' user = 'user_name' \ password = 'your_password' " # use connect function to establish the connection conn = connect(conn_string) |
Explanation:
host – name of the host in which the database is hosted
Username – Name of the admin
Password – Password of the admin
dbname– database name
Perform PostgreSQL CRUD operations from Python
The DDL is comprised of the Create, Read, Update, Delete (CRUD) operations which form the backbone of any SQL database system. Let us discuss how to perform CRUD operations on a PostgreSQL database using python. Pyscopg2 is the most preferred mode and is widely used to connect the PostgreSQL database using python. Pyscopg2 is a Database API which is a PostgreSQL compatible driver that manages connection pools.
In this article, we will learn how to connect to the PostgreSQL database using SQLAlchemy in python and also understood the procedure on how to perform CRUD operations against the PostgreSQL database.
Contact Us