This project provides a simple and reusable Python script that uploads multiple CSV files from a local folder into a PostgreSQL database.
It uses Pandas, SQLAlchemy, and python-dotenv to handle data loading and credential management securely.
PYTHON_TO_POSTGRES/ β βββ venv/ # Virtual environment (ignored by Git) βββ .env # Environment variables ignored by Git (database credentials) βββ .gitignore # Files and folders ignored by Git βββ main.py # Main Python script to upload CSVs βββ requirements.txt # Project dependencies βββ readme.md # Project documentation
Before running this project, ensure that you have:
- Python 3.8+
- PostgreSQL installed and running locally or remotely
- The ability to connect to your PostgreSQL database (user, password, host, port, database)
git clone https://github.com/Ojosticka/Python_to_Postgres.git
cd PYTHON_TO_POSTGRES
2οΈβ£ Create and activate a virtual environment
python -m venv venv
# Activate it
# On Windows
venv\Scripts\activate
# On macOS/Linux
source venv/bin/activate
3οΈβ£ Install dependencies
pip install -r requirements.txt
4οΈβ£ Create a .env file Create a file named .env in the root directory and add your PostgreSQL credentials:
DB_NAME=posey_db
DB_USER=postgres
DB_PASSWORD=your_password_here
DB_HOST=localhost
DB_PORT=5432
Make sure .env is included in .gitignore to keep credentials private.
- The script loads database credentials from .env using python-dotenv.
- It loops through all CSV files in the specified folder.
- Each CSV is read into a Pandas DataFrame.
- Using SQLAlchemy, each DataFrame is written into a PostgreSQL table with the same name as the CSV file (without .csv).
- Run the script:
python main.py
- Youβll be prompted to enter the full path to the folder containing your CSV files, e.g.:
Enter full path to folder containing CSV files: C:\Users\Tobi\Documents\csv_folder
- The script will:
- Connect to PostgreSQL
- Create (or replace) tables matching each CSV filename
- Upload all CSV data into those tables
Importing accounts.csv into table: accounts
Successfully imported accounts.csv into 'accounts'
Importing transactions.csv into table: transactions
Successfully imported transactions.csv into 'transactions'
π― All CSVs imported successfully!
C:\Users\Tobi\Documents\csv_folder β βββ accounts.csv βββ customers.csv βββ transactions.csv
This will create three tables in PostgreSQL:
- accounts
- customers
- transactions
- psycopg2 or sqlalchemy not found:
Run pip install -r requirements.txt again.
-
Connection errors: Double-check credentials in .env and ensure PostgreSQL is running and accessible.
-
Permission denied: If on Windows, ensure your user has permission to access the CSV folder.
Your requirements.txt should include (approximate versions):
- pandas
- sqlalchemy
- python-dotenv
- psycopg2-binary
Feel free to fork the repository and enhance the script β for example:
- Add error handling per file (skip failed uploads)
- Log upload activity to a text file
- Add support for incremental uploads instead of replacing tables
Author: Oluwatobi Ojo
Purpose: Demonstration of Python automation to load structured data into PostgreSQL.