-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_postgres.py
More file actions
76 lines (62 loc) · 2.63 KB
/
Copy pathpopulate_postgres.py
File metadata and controls
76 lines (62 loc) · 2.63 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
import csv
import psycopg2
# Database connection details
conn = psycopg2.connect(
host="localhost",
port=5433,
dbname="metabase",
user="metabase",
password="metabase"
)
print("Connecting to Database")
# Path to the CSV file
csv_file_path = "metrics.csv"
# Extract table name from the CSV file name
table_name = csv_file_path.replace(".csv", "")
# Open the CSV file
with open(csv_file_path, "r") as file:
reader = csv.reader(file)
header = next(reader) # Get the header row
# Build the CREATE TABLE query.
create_table_query = f"CREATE TABLE IF NOT EXISTS {table_name} ("
# Iterate over the header columns to create the table columns
for column in header:
if column in ['enrolled_students', 'course_id', 'weekly_active_users', 'daily_active_users']:
create_table_query += f"{column} INTEGER, "
elif column == 'date':
create_table_query += f"{column} DATE, "
else:
create_table_query += f"{column} VARCHAR(255), "
create_table_query = create_table_query.rstrip(", ") # Remove the trailing comma and space
create_table_query += ");"
# Execute the CREATE TABLE query
with conn.cursor() as cursor:
cursor.execute(create_table_query)
conn.commit()
# Load the CSV data into the table
with open(csv_file_path, "r") as file:
reader = csv.DictReader(file)
with conn.cursor() as cursor:
for row in reader:
# Build the INSERT query
# NOTE: had to remove single quotes from CSV file because it messed with the script.
insert_query = f"INSERT INTO {table_name} ({', '.join(header)}) VALUES ("
# Iterate over the header columns to construct the INSERT query
for column in header:
if column in ['enrolled_students', 'course_id', 'weekly_active_users', 'daily_active_users']:
if row[column] == '':
insert_query += f"{0}, "
else:
insert_query += f"{int(row[column])}, "
elif column == 'Date':
insert_query += f'TO_DATE("{row[column]}", "MM/DD/YY"), '
else:
insert_query += f"'{row[column]}', "
insert_query = insert_query.rstrip(", ") # Remove the trailing comma and space
insert_query += ");"
# Execute the INSERT query
cursor.execute(insert_query)
conn.commit()
# Close the database connection
conn.close()
print("Database connection closed.")