Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Python to PostgreSQL

On googling, I found documents about PyGreSQL library that would help me connect Python to Postgres.

However, I cannot find the link to download the package anywhere. Even this document: http://www.pygresql.org/install.html

talks about downloading the Windows Installer etc, but doesn't tell from where.

I want the connection to work for Python 2.7

like image 265
user3422637 Avatar asked Feb 03 '26 23:02

user3422637


1 Answers

Step 1: pip install psycopg2

Step 2: User below code:-

import psycopg2

connection = psycopg2.connect(database="dbname", user="username", password="pass", host="hostname", port=5432)

cursor = connection.cursor()

cursor.execute("SELECT * from portal.portal_users;")

# Fetch all rows from database
record = cursor.fetchall()

print("Data from Database:- ", record)
like image 120
sandeep shewale Avatar answered Feb 06 '26 12:02

sandeep shewale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!