Automation is becoming increasingly important in today’s world, and n8n provides a robust, extendable solution to build workflows for your needs. Self-hosting n8n, combined with PostgreSQL as the database backend and Adminer for easy database management, is a cost-effective and flexible option.
In this blog, we’ll guide you through setting up n8n, PostgreSQL, and Adminer using Docker Compose. This setup ensures persistent storage and an isolated environment for your automation workflows.
Prerequisites
Before starting, ensure you have the following:
Docker and Docker Compose installed on your system.
Basic understanding of Docker and Compose commands.
Why Self-Host n8n?
n8n is a popular workflow automation tool that lets you connect APIs, applications, and services. By self-hosting, you gain:
Full control over data and configurations.
No reliance on third-party servers.
Flexibility to integrate with your existing infrastructure.
Using PostgreSQL as the database backend improves performance and scalability, while Adminer makes managing the database easy.
Setting Up the Docker Compose File
Here’s the complete docker-compose.yml
file for the setup:
default
version: '3.9'
services:
postgres:
image: postgres:latest
container_name: postgres
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: n8npassword
POSTGRES_DB: n8n
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- n8n_network
adminer:
image: adminer:latest
container_name: adminer
ports:
- "8080:8080"
networks:
- n8n_network
n8n:
image: n8nio/n8n:latest
container_name: n8n
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=n8n
- DB_POSTGRESDB_USER=n8n
- DB_POSTGRESDB_PASSWORD=n8npassword
- N8N_BASIC_AUTH_ACTIVE=true
- N8N_BASIC_AUTH_USER=admin
- N8N_BASIC_AUTH_PASSWORD=adminpassword
- N8N_HOST=n8n.local
- N8N_PORT=5678
- WEBHOOK_URL=http://localhost:5678
ports:
- "5678:5678"
depends_on:
- postgres
networks:
- n8n_network
volumes:
- n8n_data:/home/node/.n8n
volumes:
postgres_data:
n8n_data:
networks:
n8n_network:
Breakdown of the File
PostgreSQL Service
Image: Uses the official
postgres
image.Environment Variables: Defines the database name, username, and password.
Volumes: Ensures data persists across container restarts.
Ports: Maps PostgreSQL to the host's
5432
port.
Adminer Service
Image: Uses the lightweight Adminer image for database management.
Ports: Exposes Adminer on the host’s
8080
port for easy web access.
n8n Service
Image: Pulls the latest n8n image.
Environment Variables: Configures n8n to connect to the PostgreSQL database and enables basic authentication.
Ports: Maps n8n’s web interface to
5678
on the host.Volumes: Ensures workflows and settings persist.
Dependencies: Waits for PostgreSQL to initialize before starting.
Volumes and Networks
Volumes: Persistent storage for PostgreSQL and n8n data.
Networks: Connects all services securely within a Docker bridge network.
Steps to Deploy
Save the
docker-compose.yml
file to a directory of your choice.Run the following command to start the services:
docker-compose up -d
Verify that the containers are running:
docker ps
Access the services:
n8n: Open
http://localhost:5678
in your browser.Adminer: Open
http://localhost:8080
in your browser and log in with:System: PostgreSQL
Server:
postgres
Username:
n8n
Password:
n8npassword
Customization Options
Change Ports: Update the ports in the
docker-compose.yml
file to avoid conflicts.Authentication: Update
N8N_BASIC_AUTH_USER
andN8N_BASIC_AUTH_PASSWORD
for better security.Database Settings: Adjust PostgreSQL credentials to fit your existing setup.
Conclusion
Self-hosting n8n with PostgreSQL and Adminer using Docker Compose is a straightforward process. This setup gives you complete control over your workflow automation and data, ensuring scalability and security. With Adminer, managing the database becomes effortless.
Start building your automation workflows today and take control of your automation infrastructure!
Happy automating! 🚀