Can't connect to the PostgreSQL from a Docker container running in WSL

Describe the problem/error/question

I’m new to n8n and I have been using WSL for coding under my Windows system for quite a while. My problem started when I decided to use the containerized version of n8n running on WSL together with a PostgreSQL database that runs in my host system (Windows).

It seems like the container is not able to connect to the database and fails to initialize, even though I can successfully test the connection directly from the WSL terminal.

What is the error message (if any)?

This is the error message with the -e DEBUG=* option in Docker:

(node:8) Warning: TypeError
module: @oclif/[email protected]
task: findCommand (user-management:reset)
plugin: n8n
root: /usr/local/lib/node_modules/n8n
message: Cannot read properties of undefined (reading ‘database’)
See more details with DEBUG=*
2024-10-30T11:22:34.381Z oclif:config reading user plugins pjson /home/node/.local/share/n8n/package.json
2024-10-30T11:22:34.382Z oclif:config config done
2024-10-30T11:22:34.382Z oclif:config start init hook
2024-10-30T11:22:34.383Z oclif:config init hook done
2024-10-30T11:22:34.383Z oclif:config runCommand start
2024-10-30T11:22:34.383Z oclif:config start command_not_found hook
2024-10-30T11:22:34.383Z oclif:config command_not_found hook done
› Error: command start not found
2024-10-30T11:22:34.387Z oclif:error Error: command start not found
at Config.runCommand (/usr/local/lib/node_modules/n8n/node_modules/@oclif/core/lib/config/config.js:394:19)
at run (/usr/local/lib/node_modules/n8n/node_modules/@oclif/core/lib/main.js:94:16)
at /usr/local/lib/node_modules/n8n/bin/n8n:72:2

This is only the final part of the full error, however the following lines are consistent through all of the error message (they appear many times):

(node:8) Warning: TypeError
module: @oclif/[email protected]

and

message: Cannot read properties of undefined (reading ‘database’)

Please share your workflow

I’m using the following environment variables:

N8N_ENCRYPTION_KEY=
DB_TYPE=‘postgresdb’
DB_POSTGRESDB_HOST=‘localhost’
DB_POSTGRESDB_PORT=5432
DB_POSTGRESDB_DATABASE=‘n8n’
DB_POSTGRESDB_USER=‘postgres’
DB_POSTGRESDB_PASSWORD=‘postgres’
N8N_BASIC_AUTH_ACTIVE=‘true’
N8N_BASIC_AUTH_USER=
N8N_BASIC_AUTH_PASSWORD=
WEBHOOK_URL=

And I’m running the following run script:

sudo docker run --rm --name n8n -p 5678:5678 --env-file n8n.env -v n8n_data:/home/node/.n8n n8nio/n8n:latest

I’ve already changed the configuration of PostgreSQL to listen to any IP and tried using the host.docker.internal host mapped to the WSL IP address, both with no success. The network in WSL is also configured in mirrored mode.

The container runs normally with any of the above mentioned variables except the DB_TYPE, once I define it’s a PostgreSQL database it breaks.

Information on your n8n setup

  • **n8n version:**latest
  • **Database (default: SQLite):**PostgreSQL 16
  • **Running n8n via (Docker, npm, n8n cloud, desktop app):**Docker
  • **Operating system:**PostgreSQL server running on Windows 11 23H2 and Docker running on WSL2 with Ubuntu 22.04

Welcome to the community @gutrivelatto !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


I think by running the n8n instance in the container referring to the DB location as localhost would suggest running PostgreSQL in the container too. However, you seem to be running PostgeSQL on the host machine, not the container.

You can connect to PostgreSQL from WSL because it runs on host machine, not the Docker container.

Normally to access the host machine from the Docker container you would use host.docker.internal.