-
Notifications
You must be signed in to change notification settings - Fork 7
/
dbcheck.py
36 lines (28 loc) · 988 Bytes
/
dbcheck.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
import psycopg2
from config import dbconfig
def connectcheck():
""" 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 a statement
cur.execute("SELECT version();")
# display the PostgreSQL database server version
version = cur.fetchone()
print("You are connected to - ", version,"\n")
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.")
connectcheck()