-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_postgres_tables.py
69 lines (62 loc) · 1.69 KB
/
create_postgres_tables.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
61
62
63
64
65
66
67
68
69
import psycopg2
# define tables to be created
tables = []
tables.append('''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
phone_number VARCHAR(22),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
tables.append('''
CREATE TABLE IF NOT EXISTS vendors (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
state VARCHAR(14),
zip VARCHAR(5),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
tables.append('''
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
vendor_id INT,
FOREIGN KEY (vendor_id) REFERENCES vendors(id),
name VARCHAR(255),
description TEXT,
price DECIMAL(10, 2),
stock_quantity INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
tables.append('''
CREATE TABLE IF NOT EXISTS orders (
id SERIAL PRIMARY KEY,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(id),
product_id INT,
FOREIGN KEY (product_id) REFERENCES products(id),
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_product INT,
shipping_address VARCHAR(255),
payment_method VARCHAR(50),
status VARCHAR(50) DEFAULT 'Pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Connect to PostgreSQL database
conn = psycopg2.connect(
host="localhost",
user="benchmark_user",
password="password",
database="benchmark_db"
)
cursor = conn.cursor()
# Create tables
for table_def in tables:
cursor.execute(table_def)
# Commit the changes and close the connection
conn.commit()
conn.close()