load data from csv to postgres

mail@pastecode.io avatar
unknown
python
2 months ago
1.3 kB
5
Indexable
Never
import pandas as pd
import psycopg2 
from sqlalchemy import create_engine

file_path = '/lessons/2. Анализ вводных по задаче/7. Использование файлов и подключение к БД/Задание 1/stage/' 

conn_string = 'postgresql://jovyan:jovyan@localhost:5432/de'
  
db = create_engine(conn_string)
conn = db.connect() 
conn_bd = psycopg2.connect( 
    database="de", 
    user='jovyan',  
    password='jovyan',  
    host='localhost',  
    port= '5432'
) 
  
conn_bd.autocommit = True
cursor = conn_bd.cursor() 
  
user_order_log = pd.read_csv(f'{file_path}user_order_log.csv')
user_order_log.pop('id')
user_order_log.pop('uniq_id')

user_order_log.to_sql('user_order_log', db, if_exists='replace', index=False, schema='stage')

user_activity_log = pd.read_csv(f'{file_path}user_activity_log.csv')
user_activity_log.pop('id')
user_activity_log.pop('uniq_id')

user_activity_log.to_sql('user_activity_log', db, if_exists='replace', index=False, schema='stage')

customer_research = pd.read_csv(f'{file_path}customer_research.csv') 

customer_research.to_sql('customer_research', db, if_exists='replace', index=False, schema='stage')
  
conn_bd.commit() 
conn_bd.close() 
Leave a Comment