This project integrates n8n with a PostgreSQL database using Docker Compose, and includes:
- A Python backend for managing database models and migrations (via Alembic)
- Dockerized orchestration with PostgreSQL
- Automated seeding of test data
- An n8n workflow for importing
.zipfiles with CSVs (clients, contracts, readings) - Anomaly detection using Z-score
n8n_postgres_integration/
├── docker-compose.yml
├── Dockerfile
├── pyproject.toml (Poetry)
├── src/
│ ├── config.py
│ ├── models/
│ ├── seed/
│ └── ...
├── migrations/
├── data/
│ ├── clientes.csv
│ ├── contratos.csv
│ ├── leituras.csv
git clone https://github.com/your-user/n8n-postgres-integration.git
cd n8n-postgres-integrationdocker compose up --buildThis will:
- Start Postgres
- Run Alembic migrations
- Seed the database with initial data (if prompted)
- Launch n8n on http://localhost:5678
| Service | Port | Description |
|---|---|---|
| n8n | 5678 | Workflow automation UI |
| postgres | 5432 | Main database |
| migrate | — | Runs Alembic migrations on startup |
| seed | — | Populates database using ORM models |
poetry installMake sure to install with:
poetry config virtualenvs.create falseThe data/ folder contains:
clientes.csv: 10 clients (7 active)contratos.csv: client–contract mappingleituras.csv: energy readings, with 1 outliers
Use these files in the n8n .zip workflow to populate the database.
An n8n workflow is included that:
- Accepts a
.zipfile via HTTP: POST http://localhost:5678/webhook-test/upload-seed - Unzips
clientes.csv,contratos.csv, andleituras.csv - Parses and inserts the rows into Postgres
Use [Move Binary Data], [Split In Batches], and [Postgres Insert] nodes inside the workflow.
Another workflow:
- Fetches mean readings per active client (last 3 months)
- Calculates Z-score
- Detects outliers and returns JSON
- Feeds data to an AI agent for insight generation
- Triggered by: GET http://localhost:5678/webhook-test/get-results
To create migrations:
alembic revision --autogenerate -m "create clients table"To apply migrations:
alembic upgrade headTo downgrade:
alembic downgrade -1These are not necessary to be executed, if the project is run by docker compose
- Rodrigo Rangel 🧙♂️
- With assistance from ChatGPT 😄
MIT License