Tuesday, November 20, 2018

Postgres Python Integration

 How to fetch data from POSTGRESQL from Python script.
 
 Here you will learn how to connect your python script to postgresql database. You can see below example only trying to fetch data from database.
 
 
 
 
import psycopg2 # database connection


#Data base connection
try:
    conn = psycopg2.connect("dbname='YourDatabase' user='DBUserName' host='IP/locatalhost' password='DBPassword'")
except:
    print "I am unable to connect to the database"

cur = conn.cursor()
query = "SELECT name from users limit 5";
cur.execute(query)
rows = cur.fetchall()
for row in rows:
    print "   ", row[0]
conn.close()

No comments:

Post a Comment

If you have any query please comment here, Will get back to you :)