forked from PDXPythonPirates/introtopostgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createtable.py
35 lines (27 loc) · 908 Bytes
/
createtable.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
import psycopg2
from config import dbconfig
def createtable():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = dbconfig()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
print("The database is live.")
# execute and comit the sql statement
with open("create.sql", 'r') as f:
cur.execute(f.read())
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
# close the cursor with the PostgreSQL
cur.close()
# close the connection with the PostgreSQL
conn.close()
print("The database is closed.")
createtable()