N8N_ENCRYPTION_KEY: how n8n credentials encryption works, and how to never lose the key
By Clément Lacaille (Tech-Bharat) · Published 3 August 2026 · updated 24 November 2026 · 12 min read · how we build and test
FlowKit Self-Hosting Kit — €79
Tested Docker Compose, encrypted backups, updates, monitoring · guides and checklists in English
n8n encrypts every credential before storing it in its database, with a key it generates on first start and keeps in ~/.n8n/config, unless you set your own in the N8N_ENCRYPTION_KEY environment variable — which the official documentation recommends, and requires on every worker in queue mode. Lose that key and the stored credentials can no longer be decrypted. The official docs page fits in a few lines; this guide adds what it leaves out, read from n8n's own source code: what the config file contains, why n8n refuses to start with "Mismatching encryption keys", how to pass the key as a Docker secret, and how to really change it.
It's usually the same story that brings people here. You've just migrated your n8n instance to a new server, or reinstalled it after an incident. The database restored fine: the workflows are there, the execution history too, everything looks in order. Then you run a workflow, and every credential returns the same message: "Credentials could not be decrypted". Nothing is corrupted, nothing has vanished — the encryption key simply changed, and without it, the credentials stored in the database are unreadable. By design.
This guide explains how n8n protects your credentials, why the N8N_ENCRYPTION_KEY variable is the most critical — and most overlooked — piece of any self-hosted setup, and how to organize yourself so you never live through that scenario, or get out of it cleanly if it's already too late.

What n8n encrypts, and why
An n8n instance quickly accumulates dozens of credentials: OpenAI API keys, Slack tokens, SMTP passwords, PostgreSQL accesses… If all of that were stored in plain text in the database, anyone able to dump it — a contractor, a backup sitting on a misconfigured bucket, one access too many — would walk away with every one of your accesses.
n8n avoids that risk by encrypting credentials before writing them to the database. A SELECT on the credentials table only reveals unreadable blobs. To decrypt them, n8n needs the encryption key, which it obtains in one of two ways:
- Supplied explicitly via the
N8N_ENCRYPTION_KEYenvironment variable — the recommended case, more on that below. - Generated automatically on first startup if the variable is absent, then stored in the configuration file inside n8n's data directory (
~/.n8n/config, which lives in a Docker volume on containerized installs).
Where n8n keeps the key when you don't set it
On first launch without N8N_ENCRYPTION_KEY, n8n logs No encryption key found - Auto-generating and saving to: …/.n8n/config, draws 24 random bytes, encodes them in base64 and writes them to that settings file (n8n source, instance-settings.ts, read on November 24, 2026). The file is a small JSON object:
{
"encryptionKey": "…"
}
Where that file lives depends on the install:
- npm or bare install:
~/.n8n/config, in the home directory of the user running n8n. - Official Docker image:
/home/node/.n8n/config, which only survives a container recreation if/home/node/.n8nis mounted on a persistent volume. - Custom location:
N8N_USER_FOLDERsets the folder in which n8n creates.n8n— the deployment variables reference describes it as the place that stores "database file and encryption key".
To read the key of a running Docker instance before a migration:
docker compose exec n8n cat /home/node/.n8n/config
Letting n8n generate the key is the default mode, and it's the one that traps everybody: the key exists, it works, but nobody knows it's there. The day the Docker volume is lost, the server is reinstalled, or a migration only copies the database, the new instance generates a new key — and every credential encrypted with the old one becomes undecryptable.
It's worth stating plainly, because it's counter-intuitive: a backup of the database alone does not protect your credentials. The database without the key contains perfectly restorable workflows and perfectly unusable credentials.
The golden rule: set the key explicitly from day one
The fix comes down to two moves, done once and for all at install time (or right now if your instance is already running — see below).
1. Generate a strong key. A long random value, produced by a real generator:
openssl rand -hex 24
2. Set it in the configuration, via the .env referenced by your docker-compose.yml:
# docker-compose.yml (excerpt)
services:
n8n:
image: docker.n8n.io/n8nio/n8n
environment:
- N8N_ENCRYPTION_KEY=${N8N_ENCRYPTION_KEY}
# ... rest of the configuration
# .env
N8N_ENCRYPTION_KEY=the_value_generated_by_openssl
Variant: pass the key as a file. n8n accepts a _FILE suffix on its configuration variables (deployment variables reference), and its configuration code names N8N_ENCRYPTION_KEY_FILE explicitly. With a Docker secret, the key no longer appears in the compose file or in docker inspect:
services:
n8n:
image: docker.n8n.io/n8nio/n8n
environment:
- N8N_ENCRYPTION_KEY_FILE=/run/secrets/n8n_encryption_key
secrets:
- n8n_encryption_key
secrets:
n8n_encryption_key:
file: ./secrets/n8n_encryption_key.txt
Write the file without a trailing newline (openssl rand -hex 24 | tr -d '\n' > secrets/n8n_encryption_key.txt) and keep it out of Git.
3. Store it in a password manager (Bitwarden, 1Password, Vaultwarden…), the same way you'd store the server's root password. Not in a text file on the server itself — if the server dies, the key dies with it.
One important caveat: if your instance is already running with an auto-generated key, do not set a new value in N8N_ENCRYPTION_KEY — you'd trigger the exact error this guide is trying to prevent. Recover the existing key from the ~/.n8n/config file inside the container (for instance docker exec -it n8n cat /home/node/.n8n/config), and that's the value you carry over into the .env. The key doesn't change; it simply becomes explicit, storable in your secrets manager, and impossible to lose to a volume accident.
This habit echoes an old lesson from security research: cryptography rarely fails on the mathematics, and almost always on the human handling of keys. The classic study by Whitten and Tygar, Why Johnny Can't Encrypt: A Usability Evaluation of PGP 5.0 (USENIX Security, 1999), already showed that most participants failed to correctly use an encryption tool designed for the general public — not because the algorithm was weak, but because key handling escaped them. Twenty-five years later, the "Credentials could not be decrypted" of an n8n instance migrated without its key is the exact same illustration: the algorithm did its job perfectly; it's the key that didn't come along.
A complete backup = database + key
A proper n8n backup therefore has two inseparable parts:
- The database, which holds workflows, encrypted credentials and history — the full procedure is in our PostgreSQL backup and restore guide.
- The encryption key, without which the credentials half of that database is noise.
If the key is set in the .env as recommended, including that file (or its contents, via the password manager) in the backup procedure is enough. If it isn't, then the Docker volume containing ~/.n8n must be backed up alongside the database — and that's precisely the volume most backup scripts forget, because the thinking is "the data lives in PostgreSQL" and nobody sees what could be irreplaceable about a config directory. It holds the only copy of the key.
The FlowKit Self-Hosting Kit automates that check: its restore script compares the N8N_ENCRYPTION_KEY stored in the backup archive with the one in the current .env, and stops before touching the database if they differ.
Test the restore at least once: mount the restored database and the key on a throwaway instance, open a credential, run a workflow. A backup that's never been tested is a hypothesis, not a backup.
Migration: restore the database AND reuse the same key
Every instance migration — a server change, a move from SQLite to PostgreSQL, or a migration from n8n Cloud to self-hosted — follows the same rule: the new instance must start with the old instance's key, supplied via N8N_ENCRYPTION_KEY, before its first launch. Order matters: if the new instance starts once without the key, it generates its own, and you'll have to reconfigure it properly before the restored credentials become readable again.
The credentials side of the migration checklist:
- Recover the source instance's key (
.env, or~/.n8n/configif it was auto-generated). - Put it in the target instance's
.env. - Restore the database.
- Start up, open an existing credential to check it decrypts, run a test workflow.
The same principle applies to a plain version update: the key doesn't change between versions, but an update done by recreating containers without preserving the volume or the .env can make it disappear — our guide on updating n8n without breaking anything covers the procedure that avoids that kind of collateral damage.

"Mismatching encryption keys": three checks
The other error people run into is a startup refusal rather than a decryption failure:
Mismatching encryption keys. The encryption key in the settings file /home/node/.n8n/config does not match the N8N_ENCRYPTION_KEY env var. Please make sure both keys match.
n8n found both a .n8n/config file and an N8N_ENCRYPTION_KEY variable, with different values, and it refuses to guess. It typically appears after someone "regularizes" an existing instance by adding a fresh key to the .env. Check, in this order:
- The key in the file:
docker compose exec n8n cat /home/node/.n8n/config(if the container doesn't stay up, read the file from the volume). - The key the container actually receives:
docker compose exec n8n printenv N8N_ENCRYPTION_KEY— an unquoted special character, a stray space or a second.envcan change it. - The key of each worker, in queue mode: same command on every worker service.
The fix is almost always to put the file's value into the environment: it's the key your credentials were encrypted with. Don't delete config to make the error go away until you've confirmed which key the credentials use — n8n would start, and every credential would fail to decrypt.
Queue mode: one key for main and every worker
The official "Set a custom encryption key" page (read on October 21, 2026) says it in one line: in queue mode, you must specify the encryption key environment variable for all workers.
If your instance runs in queue mode with Redis and workers, the constraint extends: the main process and every worker read the same encrypted credentials from the same database, so all of them must start with exactly the same N8N_ENCRYPTION_KEY. A worker started with no key at all doesn't even get that far: n8n refuses to start it with Failed to start worker because of missing encryption key. Please set the N8N_ENCRYPTION_KEY env var when starting the worker. A worker given a different key starts, accepts jobs, and fails when it tries to decrypt the credentials needed to run them.
In practice, that's one more argument for the shared .env: a single file, referenced by the n8n, n8n-worker and related services in the docker-compose.yml, and inconsistency becomes impossible.
Lost key: what's recoverable, what isn't
If the key is definitively lost — deleted volume, dead server, no backup of the config file — the honest answer is: the credentials are unrecoverable. No support team, no tool, no database access can decrypt them without the key; that's exactly the guarantee encryption is meant to provide, and it cuts against you too.
What remains intact:
- The workflows, in full: logic, nodes, connections, settings.
- The execution history and the rest of the database.
The way forward: generate a new key, set it in N8N_ENCRYPTION_KEY, back it up (this time), then re-enter every credential by hand. It's tedious, but it's also an unexpected opportunity to clean house: regenerate the API keys on the service side rather than pasting the old ones back in, delete orphaned credentials no workflow uses anymore, and note who has access to what — the full approach is in our guide to securing your API credentials in n8n. Plenty of instances carry accesses granted "just to test" two years earlier; the forced inventory has at least that virtue.
Changing the key on purpose: rotation, without illusions
Changing the value of N8N_ENCRYPTION_KEY re-encrypts nothing. With the old config file still present, n8n stops at startup with "Mismatching encryption keys"; without it, n8n starts and every stored credential becomes unreadable.
What n8n does offer is rotation of a second layer. Its encryption key rotation page describes a two-layer model: N8N_ENCRYPTION_KEY is the master key, which "never changes" and only protects a data encryption key stored encrypted in the database; that data key encrypts the credentials and can be rotated from Settings > Data Encryption Keys. The feature exists on every self-hosted edition, not on n8n Cloud; it is on by default from n8n 3.0, and on n8n 2.x you enable it with N8N_ENV_FEAT_ENCRYPTION_KEY_ROTATION=true on the main instance and every worker. Take a full database backup first: data written with a data encryption key can't be read by older n8n versions.
If the master key itself must change (a leaked .env, for instance), the CLI is the clean route:
- With the old key active, export every credential in plain text:
n8n export:credentials --all --decrypted --output=backups/decrypted.json(CLI commands). - Start a fresh instance with the new key, and import:
n8n import:credentials --input=backups/decrypted.json. The import command encrypts plain-text credentials with the instance's current key before saving them. - Delete the export file securely. It holds every secret in clear: keep it local, short-lived and out of every backup.
Run it on a test instance first. And if the key leaked, the credentials it protected must be considered exposed too: regenerate them on the provider side rather than just re-encrypting them.
What N8N_ENCRYPTION_KEY is not
Two recurring confusions:
- It's not a configuration variable like the others. n8n's environment variables get changed, tested and tuned over time — the webhook URL, the timezone, execution limits.
N8N_ENCRYPTION_KEYis the only one whose modification functionally destroys data. You set it once, you back it up, you never touch it again. - It's not the interface's authentication. The encryption key protects the credentials stored in the database against a direct read of the database. It does not protect access to the n8n interface itself (user accounts, SSO, reverse proxy): those are two distinct layers, and you need both.
Common pitfalls
- Letting n8n generate the key without ever writing it down: the default mode works perfectly… until the first migration or volume loss. Set
N8N_ENCRYPTION_KEYexplicitly, from install day. - Backing up the database but not the key: the backup script covers PostgreSQL and ignores the
~/.n8nvolume — the credentials half of the backup is unusable when the day comes. - Setting a new value on an existing instance thinking you're "regularizing" things: the credentials encrypted with the old auto-generated key become unreadable. Recover the existing key from
~/.n8n/configfirst. - Starting the new instance before the key is in place during a migration: an auto-generated key settles in, and "Credentials could not be decrypted" appears despite a correctly restored database.
- Queue-mode workers with different keys: jobs get dispatched, decryption fails at execution time. One
.envshared by all services. - Storing the key only on the server it protects: keep it in the server's
.envand in an external password manager, or the key disappears with the machine. - Committing the
.envto a Git repository: the key is a secret on par with a database password — out of version control, always.
In summary
N8N_ENCRYPTION_KEY is the most critical variable of a self-hosted n8n install: it encrypts every credential in the database, and without it, a backup or a migration only restores workflows with no accesses. The discipline comes down to three moves — generate a strong key (openssl rand -hex 24), set it explicitly in the .env from install day, keep it in a password manager — and one rule: the database and the encryption key always travel together, in backups as in migrations. If you inherit a lost key, the workflows survive, the credentials get re-entered, and the forced access inventory that follows is exactly the kind of project the Compliance & Audit Pack (€149) formalizes: who has access to what, where the secrets live, what gets logged — managing the encryption key is only the first line of it. And if you are installing from scratch, the FlowKit Self-Hosting Kit (€79) generates N8N_ENCRYPTION_KEY with openssl in its .env, refuses to start the stack without it, and shares it between the main instance and the queue-mode workers.
FAQ
Frequently asked questions
Where does n8n store its encryption key by default?
If N8N_ENCRYPTION_KEY is not set, n8n generates a random key on first launch and writes it to the settings file config inside its .n8n folder: ~/.n8n/config on a bare install, /home/node/.n8n/config in the official Docker image (N8N_USER_FOLDER moves that folder). The file is a small JSON object whose encryptionKey field holds the key. Read it with cat before any migration, and copy the value into N8N_ENCRYPTION_KEY.
What does "Mismatching encryption keys" mean when n8n starts?
n8n found both a settings file (.n8n/config) and an N8N_ENCRYPTION_KEY variable, and the two values differ, so it refuses to start rather than pick one. Compare the encryptionKey in the config file with the value the container actually receives (docker compose exec n8n printenv N8N_ENCRYPTION_KEY). In nearly every case the config file holds the key your credentials were encrypted with: put that value in the environment instead of deleting the file.
What happens if I change N8N_ENCRYPTION_KEY on an existing instance?
Nothing gets re-encrypted. If the .n8n/config file is still there, n8n stops at startup with "Mismatching encryption keys"; if it isn't, n8n starts with the new key and every stored credential fails with "Credentials could not be decrypted". To really move to a new key, export the credentials decrypted with the old key (n8n export:credentials --all --decrypted), start a fresh instance with the new key, import them with n8n import:credentials, then delete the plain-text file.
Why do my credentials show "Credentials could not be decrypted" after migrating or reinstalling n8n?
Because the new instance isn't using the same encryption key as the old one. Credentials are stored encrypted in the database, and n8n needs the exact key that encrypted them to read them back. If you never set N8N_ENCRYPTION_KEY, the key lived in the config file inside the old instance's .n8n directory; the new instance generated a fresh one on first startup, and decryption fails. The fix: recover the original key (the ~/.n8n/config file from the old server or its backup) and supply it via N8N_ENCRYPTION_KEY before restarting.
I've permanently lost my n8n encryption key: is everything gone?
No, but the credentials are. Without the key, no tool can decrypt the credentials stored in the database — that's precisely what the encryption is for. The database itself remains usable: workflows, their execution history and their settings are intact. Set a new N8N_ENCRYPTION_KEY, back it up this time, then re-enter every credential by hand (and regenerate the API keys on the service side while you're at it — it's a good opportunity to inventory and clean up your accesses).
Do all workers in queue mode need the same N8N_ENCRYPTION_KEY?
Yes, without exception. The main process and every worker read the same encrypted credentials from the same database, so they must share exactly the same key. A worker started with a different key will fail to decrypt credentials when it executes the workflows handed to it. Put the key in a shared .env file referenced by every service in the docker-compose.