Repository navigation
Expand file tree
/
Copy pathinsert_data.py
More file actions
90 lines (80 loc) · 2.71 KB
/
Copy pathinsert_data.py
File metadata and controls
90 lines (80 loc) · 2.71 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import psycopg2
from faker import Faker
# Load environment variables from .env
from dotenv import load_dotenv
load_dotenv()
DB_HOST = os.getenv("DB_HOST", "localhost")
DB_PORT = os.getenv("POSTGRES_PORT", "5432")
DB_NAME = os.getenv("POSTGRES_DB")
DB_USER = os.getenv("POSTGRES_USER")
DB_PASSWORD = os.getenv("POSTGRES_PASSWORD")
TABLES = ["customers", "products", "orders"]
fake = Faker()
def create_tables(conn):
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS customers (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
price DECIMAL(10,2)
);
""")
cur.execute("""
CREATE TABLE IF NOT EXISTS orders (
id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(id),
product_id INT REFERENCES products(id),
quantity INT
);
""")
conn.commit()
print("Tables created.")
def insert_random_data(conn, num_customers=5, num_products=5, num_orders=10):
with conn.cursor() as cur:
# Insert customers
for _ in range(num_customers):
cur.execute(
"INSERT INTO customers (name, email) VALUES (%s, %s) ON CONFLICT DO NOTHING;",
(fake.name(), fake.unique.email())
)
# Insert products
for _ in range(num_products):
cur.execute(
"INSERT INTO products (name, price) VALUES (%s, %s);",
(fake.word(), round(fake.pydecimal(left_digits=3, right_digits=2, positive=True), 2))
)
conn.commit()
# Insert orders
cur.execute("SELECT id FROM customers;")
customer_ids = [row[0] for row in cur.fetchall()]
cur.execute("SELECT id FROM products;")
product_ids = [row[0] for row in cur.fetchall()]
for _ in range(num_orders):
cur.execute(
"INSERT INTO orders (customer_id, product_id, quantity) VALUES (%s, %s, %s);",
(fake.random_element(customer_ids), fake.random_element(product_ids), fake.random_int(1, 5))
)
conn.commit()
print("Random data inserted.")
def main():
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
create_tables(conn)
insert_random_data(conn)
conn.close()
print("Database connection closed.")
if __name__ == "__main__":
main()