forked from techiescamp/python-for-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgresql.py
More file actions
29 lines (24 loc) · 882 Bytes
/
postgresql.py
File metadata and controls
29 lines (24 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import psycopg2
try:
connection = psycopg2.connect(database="dvdrental",
host="192.168.5.5",
user="postgres",
password="myPassword",
port="5432")
cursor = connection.cursor()
sql_query = "select * from actor"
cursor.execute(sql_query)
print("Selecting rows from mobile table using cursor.fetchall")
actor_records = cursor.fetchall()
print("Print each row and it's columns values")
for row in actor_records:
print("Id = ", row[0], )
print("Model = ", row[1])
print("Price = ", row[2], "\n")
except (Exception, psycopg2.Error) as error:
print("Error while fetching data from PostgreSQL", error)
finally:
if connection:
cursor.close()
connection.close()
print("PostgreSQL connection is closed")