-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_db.py
55 lines (42 loc) · 1023 Bytes
/
setup_db.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import psycopg2
import argparse
from configs import (
POSTGRES_USER,
POSTGRES_PASSWORD,
POSTGRES_PORT,
POSTGRES_DATABASE,
)
def handle_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'--drop',
default=False,
help='Drops the database',
)
return parser.parse_args()
args = handle_arguments()
drop_db = False
if args.drop:
drop_db = True
print('Getting connection......')
conn = psycopg2.connect(
user=POSTGRES_USER,
password=POSTGRES_PASSWORD,
host='localhost',
port=POSTGRES_PORT
)
print('Connected!')
conn.autocommit = True
cursor = conn.cursor()
if drop_db:
sql = f'''DROP database {POSTGRES_DATABASE};'''
print('Dropping database......')
cursor.execute(sql)
print('Database successfully dropped......')
else:
sql = f'''CREATE database {POSTGRES_DATABASE};'''
print('Creating database......')
cursor.execute(sql)
print('Database created successfully......')
conn.close()
print('Closed connection!')