-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
71 lines (64 loc) · 1.99 KB
/
init_db.py
File metadata and controls
71 lines (64 loc) · 1.99 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
import psycopg2
from config import config
def connect():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = config()
# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# create a cursor
cur = conn.cursor()
#drop existing tables ane make new ones.
cur.execute("""DROP TABLE IF EXISTS leads""")
cur.execute("""DROP TABLE IF EXISTS high_priority""")
cur.execute("""
CREATE TABLE IF NOT EXISTS leads (
registration_dttm timestamp NOT NULL,
id INTEGER PRIMARY KEY NOT NULL,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
gender VARCHAR(255),
ip_address VARCHAR(255),
cc BIGINT,
country VARCHAR(255),
birthdate DATE,
salary NUMERIC,
title VARCHAR(255),
comments VARCHAR(255)
)
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS high_priority (
registration_dttm timestamp NOT NULL,
id INTEGER PRIMARY KEY NOT NULL,
first_name VARCHAR(255),
last_name VARCHAR(255),
email VARCHAR(255),
gender VARCHAR(255),
ip_address VARCHAR(255),
cc BIGINT,
country VARCHAR(255),
birthdate DATE,
salary NUMERIC,
title VARCHAR(255),
comments VARCHAR(255)
)
""")
# close the communication with the PostgreSQL
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')
def main():
connect()
if __name__ == '__main__':
connect()