Connect PostgreSQL to n8n?
This guide shows how to connect PostgreSQL to n8n using Docker and the n8n PostgreSQL node. You will get a working Docker Compose example, commands to create the database and user, and a simple query workflow. Follow the steps to automate data tasks with Postgres and n8n automation.
What You Need
- Docker and Docker Compose installed.
- Basic command line knowledge.
- PostgreSQL, n8n, and a terminal.
How to Connect PostgreSQL to n8n
Below are clear numbered steps to run Postgres, create a database, start n8n, and connect using the Postgres node. Each step includes commands you can copy and run.
Step 1: Run PostgreSQL with Docker
Create a simple docker-compose.yml with Postgres and n8n. This runs both services on a local network.
version: '3.8'
services:
postgres:
image: postgres:15
environment:
POSTGRES_USER: n8n_user
POSTGRES_PASSWORD: n8n_pass
POSTGRES_DB: n8n_db
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
n8n:
image: n8nio/n8n:latest
environment:
- N8N_PORT=5678
ports:
- "5678:5678"
depends_on:
- postgres
volumes:
pgdata:
Start the services:
docker-compose up -d
Step 2: Create database and user (optional)
If you used the compose above, the database and user already exist. To create another database or user, use psql from the running container.
docker exec -it $(docker ps -qf "ancestor=postgres:15") psql -U n8n_user -d n8n_db
-- Inside psql
CREATE USER my_user WITH PASSWORD 'secret';
CREATE DATABASE my_app_db OWNER my_user;
GRANT ALL PRIVILEGES ON DATABASE my_app_db TO my_user;
\q
Step 3: Start n8n and configure credentials
Open n8n in your browser at http://localhost:5678. Add Postgres credentials in n8n using the database host, port, user, and password.
- Host: postgres or localhost (use network host when running separately).
- Port: 5432.
- Database: n8n_db or your database name.
- User and Password: from environment or created user.
Step 4: Build a simple query workflow
Create a new workflow. Add the PostgreSQL node. Select the credentials you added. Use a simple SELECT to test the connection.
-- Example SQL you can run in the PostgreSQL node
SELECT id, name FROM users LIMIT 5;
Run the node. The output will show rows from your table. Use this to trigger other nodes or write results to other systems.
Step-by-step logic
- 1. Start Postgres and n8n via Docker Compose.
- 2. Ensure the database and user exist.
- 3. Add Postgres credentials in n8n settings.
- 4. Use the PostgreSQL node to run queries and receive results in workflows.
Update
Keep images updated. Pull newer Docker images and rebuild periodically. To update, stop containers and pull images, then restart.
docker-compose pull
docker-compose up -d --force-recreate
Security
Do not use default passwords in production. Use strong passwords and network limits. Run Postgres behind a firewall. Use TLS if the database is remote.
- Store credentials in n8n credentials, not in plain workflow code.
- Limit Postgres user privileges to only needed tables and actions.
- Use environment variables and Docker secrets for passwords.
Done
You now know how to connect PostgreSQL to n8n and run a sample query. Use this setup to automate reports, sync data, or build integrations. Expand workflows by adding triggers and other nodes as needed.