n8n · troubleshooting · reproduced on n8n 2.41.4, 5 December 2026
n8n: There was an error initializing DB
There was an error initializing DB means n8n could not connect to its database at startup and gave up. The message itself carries no cause. The cause is in the lines just above it, which start with Initial database connection attempt … failed:. The three causes we reproduced with Postgres are the host (localhost inside a container points at n8n itself), the password, and the database name. Fix the setting that line names, then start n8n again.
Find your line: what each cause looks like
| The attempt line contains | Cause | What to do |
|---|---|---|
connect ECONNREFUSED ::1:5432 | n8n is looking for Postgres on localhost, which inside a container is the n8n container itself | Set DB_POSTGRESDB_HOST to the Postgres service name |
password authentication failed for user "n8n" | Wrong password, a user that does not exist, or a password changed after the data volume was first created | Match DB_POSTGRESDB_USER and DB_POSTGRESDB_PASSWORD to what the database was created with |
database "n8n_prod" does not exist | DB_POSTGRESDB_DATABASE names a database the server does not have | Use the name in POSTGRES_DB, or create the database |
Could not establish database connection within the configured timeout of 20,000 ms. | In our run: a host name that does not exist on the Docker network | Check the host name first; raising the timeout does not help a wrong name |
We produced all four on 5 December 2026 with the official n8nio/n8n:2.41.4 image and postgres:16-alpine on one Docker network, by setting one DB_POSTGRESDB_* variable wrong at a time. The messages are copied from docker logs. We did not reproduce TLS errors or failed migrations such as “Table already exists”; this page does not cover them.
What the log looks like
This is the startup log of a container started with DB_TYPE=postgresdb and DB_POSTGRESDB_HOST=localhost, with two unrelated lines, a repeated line and the stack trace removed. n8n logs five failed attempts, doubling the announced wait each time, then gives up. The container’s exit code was 1:
Initializing n8n process
Initial database connection attempt 1 failed: connect ECONNREFUSED ::1:5432. Retrying in 1000ms
Initial database connection attempt 2 failed: connect ECONNREFUSED ::1:5432. Retrying in 2000ms
Initial database connection attempt 3 failed: connect ECONNREFUSED ::1:5432. Retrying in 4000ms
Initial database connection attempt 4 failed: connect ECONNREFUSED ::1:5432. Retrying in 8000ms
Initial database connection attempt 5 failed: connect ECONNREFUSED ::1:5432. Retrying in 16000ms
There was an error initializing DB
Error: There was an error initializing DB
…
connect ECONNREFUSED ::1:5432 The other causes showed the same attempt lines with a different reason. We let only the ECONNREFUSED run go to the end; the other runs were captured while n8n was still retrying. A wrong password, and also a user that does not exist, both read:
Initial database connection attempt 1 failed: password authentication failed for user "n8n". Retrying in 1000ms A host name that does not exist behaved differently. We set the host to a service name that was not on the Docker network and did not get a DNS error. Each attempt ended with n8n’s own timeout message instead (other setups may fail faster, with a DNS error):
Initial database connection attempt 1 failed: Could not establish database connection within the configured timeout of 20,000 ms. Please ensure the database is configured correctly and the server is reachable. You can increase the timeout by setting the 'DB_POSTGRESDB_CONNECTION_TIMEOUT' environment variable.. Retrying in 1000ms The message suggests raising DB_POSTGRESDB_CONNECTION_TIMEOUT. That helps a slow database. It does not help a misspelled host name: check the name first.
The fixes
ECONNREFUSED: point n8n at the Postgres service, not localhost
n8n’s docs give these defaults: DB_POSTGRESDB_HOST is localhost, the port is 5432, the database is n8n, the user is postgres. So a container that only sets DB_TYPE=postgresdb looks for Postgres inside itself. We ran exactly that and got the same ECONNREFUSED ::1:5432. In Compose, use the service name of the database as the host. These are the database lines from n8n’s own Postgres Compose file, with the variable references left as they are (the comment is ours):
environment:
- DB_TYPE=postgresdb
- DB_POSTGRESDB_HOST=postgres # the Compose service name
- DB_POSTGRESDB_PORT=5432
- DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
- DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
- DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD} If Postgres runs on the Docker host or another machine instead, use an address the container can reach. Unless the container uses host networking, localhost is not that address.
password authentication failed: three different mistakes, one message
Postgres does not say which part was wrong. We got the identical line for a wrong password and for a user that did not exist: our database was created with POSTGRES_USER=n8n, so there was no postgres role, and connecting as postgres printed password authentication failed for user "postgres". That matters because postgres is n8n’s default user when DB_POSTGRESDB_USER is not set.
The third mistake is changing the password in your .env after the database already exists. In our test on postgres:16-alpine, a new POSTGRES_PASSWORD had no effect on a volume that already held a database. Postgres logged PostgreSQL Database directory appears to contain a database; Skipping initialization, n8n was refused with the new password and connected with the original one. So change the password inside Postgres, then update n8n to match. We did not run this last step ourselves; replace the service, user and database names with yours:
docker compose exec postgres psql -U n8n -d n8n \
-c "ALTER USER n8n WITH PASSWORD 'the-new-password';" database does not exist: use the name the server was created with
DB_POSTGRESDB_DATABASE has to name a database that already exists on the server. In n8n’s Compose file both sides read the same POSTGRES_DB variable, so they cannot drift apart. Our Postgres container was started with POSTGRES_DB=n8n; we asked n8n for n8n_prod and got database "n8n_prod" does not exist on every attempt. n8n creates its tables itself, but not the database.
Starts on the second try: Postgres was not ready yet
If the same settings fail on a cold start and work after a restart, n8n ran out of attempts before Postgres accepted connections. On 2.41.4 the log announced retry waits of 1, 2, 4, 8 and 16 seconds, about half a minute in all. n8n’s Compose file handles this with a healthcheck on Postgres and a condition on n8n:
postgres:
healthcheck:
test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
interval: 5s
timeout: 5s
retries: 10
n8n:
depends_on:
postgres:
condition: service_healthy What a good start looks like
With the host, user, password and database all correct, the same container printed this (two unrelated lines removed) and went on to run its migrations on the empty database:
Initializing n8n process
Postgres 16 is outside the supported range and receives compatibility support only. Upgrade to Postgres 17 or newer.
n8n ready on ::, port 5678
Acquiring database migration lock...
Migrations in progress, please do NOT stop the process. The Postgres 16 line is a warning. n8n’s docs list 17 and 18 as supported and 16 as kept for compatibility until PostgreSQL 19.1 is released.
Related
Moving from SQLite to Postgres does not move your encryption key: it stays in the .n8n folder. If credentials fail after the switch, see n8n encryption key errors. If n8n cannot write that folder at all, see EACCES: permission denied. The self-hosted n8n guide covers the volume and the Compose file around it.
Sources and last verified
Reproduced on n8n 2.41.4 with postgres:16-alpine on 5 December 2026; the docs and the Compose file were read the same day. On that day n8n’s docs gave 3.4.4 as the current stable version. We did not re-run the reproduction on 3.x, so wording may differ there. If n8n’s docs and this page disagree, n8n wins. Write to support@flowtemplates.app and we will re-verify.
- n8n Docs — Database environment variables (defaults for DB_TYPE and DB_POSTGRESDB_*, DB_POSTGRESDB_CONNECTION_TIMEOUT) — checked 2026-12-05
- n8n Docs — Choose n8n’s database (SQLite and Postgres; supported Postgres versions) — checked 2026-12-05
- n8n-hosting — docker-compose/withPostgres/docker-compose.yml (DB_POSTGRESDB_HOST=postgres, depends_on with service_healthy, pg_isready healthcheck) — checked 2026-12-05
- n8n Docs — Release notes (current stable version) — checked 2026-12-05
- n8n GitHub releases — checked 2026-12-05
Frequently asked questions
What does “There was an error initializing DB” mean in n8n?
n8n could not open its database at startup, so it stopped. On n8n 2.41.4 the log shows five lines beginning “Initial database connection attempt … failed:” first, each naming the real reason, for example “connect ECONNREFUSED ::1:5432” or “password authentication failed for user "n8n"”. In the run we let finish, n8n then printed “There was an error initializing DB” and the container exited with code 1. Read the attempt lines. The last line only says that startup gave up.
Why does n8n say ECONNREFUSED ::1:5432 when Postgres is running?
Because ::1 is localhost, and inside a container localhost is that container, not the machine and not the Postgres container. n8n’s docs give localhost as the default for DB_POSTGRESDB_HOST, so you get this when the variable is missing as well as when it is set to localhost. In Docker Compose, set DB_POSTGRESDB_HOST to the name of the Postgres service. n8n’s own Postgres Compose file uses DB_POSTGRESDB_HOST=postgres.
I changed POSTGRES_PASSWORD and n8n still says password authentication failed. Why?
In our test the Postgres container ignored a changed POSTGRES_PASSWORD on a volume that already held a database. We started postgres:16-alpine on a volume with one password, then started it again on the same volume with a different POSTGRES_PASSWORD. Postgres logged “PostgreSQL Database directory appears to contain a database; Skipping initialization”, n8n was refused with the new password and connected with the old one. Either keep using the original password, or change it inside Postgres (ALTER USER is the SQL command for that; we did not run that step).
Does n8n wait for Postgres to be ready?
For a while. n8n 2.41.4 logged five failed connection attempts, with retry waits of 1, 2, 4, 8 and 16 seconds, and then gave up. A database that is not accepting connections by then loses the race. n8n’s Postgres Compose file avoids it with a pg_isready healthcheck on the Postgres service and depends_on with condition: service_healthy on n8n, so n8n is not started until Postgres accepts connections.
Which Postgres versions does n8n support?
n8n’s docs, read on 5 December 2026, list the latest two actively maintained major versions, 17 and 18, plus 16 for compatibility until PostgreSQL 19.1 is released. When we ran n8n 2.41.4 against Postgres 16 it started normally and logged “Postgres 16 is outside the supported range and receives compatibility support only. Upgrade to Postgres 17 or newer.” That line is a warning, not the reason for a failed start.