-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImporting_Data.py
More file actions
23 lines (17 loc) · 1007 Bytes
/
Importing_Data.py
File metadata and controls
23 lines (17 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Importing Libraries
import pandas as pd
from sqlalchemy import create_engine
# PostgreSQL Connection String
conn_string = 'postgresql://{YOUR_USERNAME}:{YOUR_PASSWORD}@{YOUR_HOST}:{YOUR_PORT_NUMBER}/{YOUR_DATABASE_NAME}'
db = create_engine(conn_string)
conn = db.connect()
# List of CSV Files
files = ['album', 'artist', 'customer', 'employee', 'genre', 'invoice', 'invoice_line', 'media_type', 'playlist', 'playlist_track', 'track']
# Loop through each csv file and import to PostgreSQL
for file in files:
# Read the CSV files from the specified directory
df = pd.read_csv(f'{YOUR_DIRECTORY_OF_CSV_FILES}/{file}.csv')
# Write the DataFrame to PostgreSQL database
df.to_sql(file, con=conn, if_exists='replace', index=False)
# REMEMBER TO INSTALL NECESSARY LIBRARIES FOR YOUR DATABASE SYSTEM - IN THIS CASE IT'S pandas, sqlalchemy AND psycopg2-binary *OR* psycopg2.
# REMEMBER TO REPLACE {YOUR_INFORMATION} WITH YOUR ACTUAL CREDENTIALS