-
Notifications
You must be signed in to change notification settings - Fork 20
/
postgresql.py
37 lines (33 loc) · 1.09 KB
/
postgresql.py
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
30
31
32
33
34
35
36
37
#brew install postgresql, pip install psycopg2
import psycopg2
try:
connection = psycopg2.connect(user = "<username>",
password = "<password>",
host = "<host for example, 127.0.0.1>",
port = "5432",
database = "<dbName>")
cursor = connection.cursor()
except (Exception, psycopg2.Error) as error :
print ("Error connecting to PostgreSQL database", error)
# Insert an item into the table
try:
cursor.execute("INSERT INTO dbName VALUES "
"(NULL, 'USA', 327000000),"
"(NULL, 'Germany', 82000000);"
)
connection.commit()
print("Item Inserted successfully.")
except connection.Error as err:
print(err)
# Print Items
try:
print("\nPrinting items...")
cursor.execute("SELECT * FROM dbName;")
connection.commit()
for item in cursor.fetchall():
print(item)
except connection.Error as err:
print(err)
cursor.close()
connection.close()
print("PostgreSQL connection is closed")