This library contains several functions that allow you to migrate data from a CSV file or Pandas Dataframe into a PostgreSQL database using the libraries Psycopg2 and Pandas. Specifically, it includes functions for:
- Loading data from a CSV file to a Pandas dataframe
- Mapping Pandas Dataframe columns to their datatypes
- Mapping Pandas Dataframe columns to suitable PostgreSQL datatypes
- Connect to a PostgreSQL database
- Creating new tables on a PostgreSQL database
- Inserting data from a Pandas DataFrame into a table in a PostgreSQL database
Install The Required Dependencies
pip install pandas psycopg2
Install The pandaspg
Package
pip install pandaspg
I will walk you through a step-by-step example of how to migrate data from a CSV
file into a PostgreSQL
database.
For demonstration purposes, download the exoplanets file located in this repo.
wget https://raw.githubusercontent.com/adolfojmnz/pandaspg/main/exoplanets_07-04-2023.csv
import pandaspg
dataframe = pandaspg.csv_to_dataframe('exoplanets_04-07-2023.csv')
column_datatypes_dict = pandaspg.get_dataframe_column_dtypes_dict(dataframe)
pg_column_datatypes_dict = pandaspg.map_pandas_to_postgresql_datatypes(
column_datatypes_dict
)
connection = pandaspg.connect_to_postgresql(
database='analysis',
user='postgres',
password='postgres',
host='localhost',
port=5432,
)
pandaspg.create_postgresql_table(
connection, 'exoplanets_csv', pg_column_datatypes_dict
)
pandaspg.insert_dataframe_into_postgresql(
connection, 'exoplanets_csv', dataframe
)
connection.close()
import pandaspg
dataframe = pandaspg.csv_to_dataframe('exoplanets_04-07-2023.csv')
column_datatypes_dict = pandaspg.get_dataframe_column_dtypes_dict(dataframe)
pg_column_datatypes_dict = pandaspg.map_pandas_to_postgresql_datatypes(
column_datatypes_dict
)
connection = pandaspg.connect_to_postgresql(
database='analysis',
user='postgres',
password='postgres',
host='localhost',
port=5432,
)
pandaspg.create_postgresql_table(
connection, 'exoplanets_csv', pg_column_datatypes_dict
)
pandaspg.insert_dataframe_into_postgresql(
connection, 'exoplanets_csv', dataframe
)
connection.close()
psql -U postgres -d analysis
\dt
SELECT * FROM exoplanets_csv;