Backup n8n with PostgreSQL
This guide explains how to backup a self-hosted n8n instance running in Docker with PostgreSQL database.
What Gets Backed Up
Section titled “What Gets Backed Up”| Component | Contents |
|---|---|
| PostgreSQL database | Workflows, credentials (encrypted), execution history, users |
| n8n_data volume | Config file, binary uploads, custom nodes, SSH keys |
| compose.yaml | Environment variables, encryption key, database credentials |
Prerequisites
Section titled “Prerequisites”- n8n running in Docker with PostgreSQL
- Access to the server
postgres-maincontainer running (or your PostgreSQL container name)
Step 1 - Backup the PostgreSQL Database
Section titled “Step 1 - Backup the PostgreSQL Database”The database contains all workflows and credentials:
docker exec <postgres_container> pg_dump -U <db_user> <db_name> > n8n-db-<date>.sqlExample
Section titled “Example”docker exec postgres-main pg_dump -U n8n_user n8n > n8n-db-18-mar-2026.sqlStep 2 - Backup the n8n Data Volume
Section titled “Step 2 - Backup the n8n Data Volume”The volume contains the encryption key file and binary data:
docker run --rm \ -v n8n_data:/data \ -v /home/<user>:/backup \ alpine tar -czvf /backup/n8n-data-<date>.tar.gz -C /data .Example
Section titled “Example”docker run --rm \ -v n8n_data:/data \ -v /home/samob:/backup \ alpine tar -czvf /backup/n8n-data-18-mar-2026.tar.gz -C /data .Step 3 - Backup the Compose File
Section titled “Step 3 - Backup the Compose File”Copy the compose file which contains the encryption key:
cp /opt/docker/n8n/compose.yaml n8n-compose-<date>.yamlExample
Section titled “Example”cp /opt/docker/n8n/compose.yaml n8n-compose-18-mar-2026.yamlStep 4 - Create Combined Archive
Section titled “Step 4 - Create Combined Archive”Bundle all components into a single backup file:
tar -czvf n8n-<date>.tar.gz \ n8n-db-<date>.sql \ n8n-data-<date>.tar.gz \ n8n-compose-<date>.yamlExample
Section titled “Example”tar -czvf n8n-18-mar-2026.tar.gz \ n8n-db-18-mar-2026.sql \ n8n-data-18-mar-2026.tar.gz \ n8n-compose-18-mar-2026.yamlStep 5 - Verify Backup
Section titled “Step 5 - Verify Backup”Check the backup contents:
tar -tzvf n8n-<date>.tar.gzExpected output:
n8n-db-18-mar-2026.sqln8n-data-18-mar-2026.tar.gzn8n-compose-18-mar-2026.yamlCritical: Encryption Key
Section titled “Critical: Encryption Key”The N8N_ENCRYPTION_KEY is essential for restoring credentials. It’s stored in:
compose.yamlas environment variablen8n_datavolume in/configfile
If lost, encrypted credentials cannot be recovered. Store the encryption key in a password manager separately from backups.
Restore on New Server
Section titled “Restore on New Server”Step 1 - Prepare the Server
Section titled “Step 1 - Prepare the Server”docker network create proxymkdir -p /opt/docker/n8nStep 2 - Extract Backup
Section titled “Step 2 - Extract Backup”tar -xzvf n8n-<date>.tar.gzStep 3 - Restore Compose File
Section titled “Step 3 - Restore Compose File”cp n8n-compose-<date>.yaml /opt/docker/n8n/compose.yamlImportant: Ensure N8N_ENCRYPTION_KEY matches the original.
Step 4 - Create and Restore Volume
Section titled “Step 4 - Create and Restore Volume”docker volume create n8n_data
docker run --rm \ -v n8n_data:/data \ -v $(pwd):/backup \ alpine tar -xzvf /backup/n8n-data-<date>.tar.gz -C /dataStep 5 - Set Up PostgreSQL
Section titled “Step 5 - Set Up PostgreSQL”docker exec -it postgres-main psql -U postgres -c "CREATE USER n8n_user WITH PASSWORD '<your_password>';"docker exec -it postgres-main psql -U postgres -c "CREATE DATABASE n8n OWNER n8n_user;"docker exec -it postgres-main psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE n8n TO n8n_user;"Step 6 - Restore Database
Section titled “Step 6 - Restore Database”cat n8n-db-<date>.sql | docker exec -i postgres-main psql -U n8n_user -d n8nStep 7 - Start n8n
Section titled “Step 7 - Start n8n”cd /opt/docker/n8ndocker compose up -dDownload Example Backup
Section titled “Download Example Backup”A sample backup file is available:
- n8n-18-mar-2026.tar.gz - 51KB
Next Steps
Section titled “Next Steps”- Store backups in multiple locations
- Schedule automated backups with cron
- Test restore procedure before you need it
- Keep encryption key in password manager