-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
60 lines (49 loc) · 1.83 KB
/
init_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
56
57
58
59
60
import os
import psycopg2
from dotenv import load_dotenv
load_dotenv()
USER = os.getenv('USER')
PASSWORD = os.getenv('PASSWORD')
def get_db_connection():
conn = psycopg2.connect(
dbname = "logs_db",
user = "postgres",
password = PASSWORD
)
return conn
conn = get_db_connection()
cur = conn.cursor()
cur.execute('DROP TABLE IF EXISTS logs;')
cur.execute('CREATE TABLE logs (id serial PRIMARY KEY,'
'ip_address varchar (150) NOT NULL,'
'request_url varchar (50) NOT NULL,'
'request_port integer NOT NULL,'
'request_path varchar (50) NOT NULL,'
'request_method varchar (50) NOT NULL,'
'browser_type varchar (150) NOT NULL,'
'request_time timestamp (50) NOT NULL,'
'service_name varchar (150) NOT NULL,'
'date_added date DEFAULT CURRENT_TIMESTAMP);'
)
cur.execute('INSERT INTO logs (ip_address,'
'request_url,'
'request_port,'
'request_path,'
'request_method,'
'browser_type,'
'request_time,'
'service_name)'
'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)',
('127.0.0.1',
'http://localhost:8000',
8000,
"/",
"GET",
"Chrome",
"2023-06-25T16:03:24.722256",
"Test_data_service"
)
)
conn.commit()
cur.close()
conn.close()