-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
69 lines (55 loc) · 1.69 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
61
62
63
64
65
66
67
68
69
import psycopg2
from psycopg2 import sql
# Database connection parameters -- !!! make sure to change these to environments variable in productions !!!
db_params = {
'dbname': 'airflow',
'user': 'airflow',
'password': 'airflow',
'host': 'localhost',
'port': 5432
}
def execute_sql(sql):
try:
# Connect to the PostgreSQL database
conn = psycopg2.connect(**db_params)
# Create a cursor object
cur = conn.cursor()
# Execute the CREATE TABLE statement
cur.execute(sql)
# Commit the changes
conn.commit()
# Close the cursor and connection
cur.close()
conn.close()
return True
except Exception as e:
print(f"Error: {e}")
return False
def create_sample_table_1 ():
# Define the CREATE TABLE statement
table_name = 'sample_table_1'
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {table_name} (
id SERIAL PRIMARY KEY,
value VARCHAR(200)
);
'''
ran_query = execute_sql(create_table_query)
print(f"Table: {table_name} created!") if ran_query else print('Error!!')
def create_sample_table_2():
# Define the CREATE TABLE statement
table_name = 'sample_table_coffees'
create_table_query = f'''
CREATE TABLE IF NOT EXISTS {table_name} (
id SERIAL PRIMARY KEY,
title VARCHAR(200),
description TEXT,
images TEXT,
ingredients TEXT []
);
'''
ran_query = execute_sql(create_table_query)
print(f"Table: {table_name} created!") if ran_query else print('Error!!')
if __name__ == "__main__":
create_sample_table_1()
create_sample_table_2()