-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_data.py
42 lines (33 loc) · 1.33 KB
/
load_data.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
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
from datetime import datetime
def load_data():
# Database connection parameters
db_params = {
'host': 'local.hasura.dev',
'database': 'dev',
'user': 'user',
'password': 'password',
'port': 7861
}
try:
# Read the CSV file
print("Reading CSV file...")
df = pd.read_csv('datasets/normalized_financial_data.csv')
# Rename columns to match database schema
df.columns = ['date', 'stock_symbol', 'open_price', 'high_price',
'low_price', 'close_price', 'volume']
# Convert date string to datetime
df['date'] = pd.to_datetime(df['date']).dt.date
# Create SQLAlchemy engine
engine = create_engine(f'postgresql://{db_params["user"]}:{db_params["password"]}@{db_params["host"]}:{db_params["port"]}/{db_params["database"]}')
# Load data into PostgreSQL
print("Loading data into PostgreSQL...")
df.to_sql('financial_data', engine, if_exists='append', index=False,
method='multi', chunksize=1000)
print("Data loaded successfully!")
except Exception as e:
print(f"An error occurred: {str(e)}")
if __name__ == "__main__":
load_data()