import psycopg2
import csv
# Connection Parameters
conn_params = {
'dbname': 'your_database_name',
'user': 'your_username',
'password': 'your_password',
'host': 'your_host',
'port': 'your_port'
}
# Path to the data file
csv_file_path = '/Users/asr373/Downloads/layoffs.csv'
# Connecting to PostgreSQL DB
conn = psycopg2.connect(**conn_params)
cursor = conn.cursor()
#Creating a Table
create_table_query = '''
create table if not exists layoffs (
company varchar(100),
location varchar(100),
industry varchar(100),
total_laid_off integer,
percentage_laid_off float,
date timestamp,
stage varchar(100),
country varchar(100),
funds_raised_millions integer
);
'''
cursor.execute(create_table_query)
conn.commit()
# Loading the data
with open(csv_file_path, 'r') as f:
next(f) # Skip the header
cursor.copy_from(f, 'layoffs', sep=',', columns=(
'company', 'location', 'industry', 'total_laid_off',
'percentage_laid_off', 'date', 'stage', 'country',
'funds_raised_millions'))
conn.commit()
cursor.close()
conn.close()
print("Data loaded successfully.")