I cannot connect to Postgres on host.docker.internal

I can’t connect from the Docker to host.docker.internal.

The following (on the host) works:

psql postgres://test:123@localhost:5432/science_dao_smm

ping host.docker.internal does work inside the docker.

The docker command:

docker run -it --rm --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n --add-host=host.docker.internal:host-gateway docker.n8n.io/n8nio/n8n

What did I do wrong?

How’s your Docker backend setup - WSL? If so:

Try replacing localhost with host.docker.internal in your connection string.

However, some times it won’t resolve correctly in wsl environments. In that case, instead of host.docker.internal, use the actual IP address of your WSL host:

# Get the IP address of the WSL shell
ip addr show eth0 | grep "inet " | awk '{print $2}' | cut -d/ -f1

For example, if your WSL IP is 000.00.000.000:
postgres://test:[email protected]:5432/science_dao_smm

Alternatively, you can run n8n with --network host to share the host’s network stack:
docker run -it --rm --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n --network host ``docker.n8n.io/n8nio/n8n

Then use localhost in n8n:
postgres://test:123@localhost:5432/science_dao_smm


No, I use Linux.

The error was that my PostgreSQL allowed connections only from localhost and contrary to what I expected the address of Docker as visible from host was not 127.0.0.1.

After I fixed PostgreSQL config, it works.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.