Need help connecting N8N to PostgreSQL

I have:

  • Ubuntu VPS on DigitalOcean
  • n8n running in Docker
  • PostgreSQL running directly on Ubuntu host (not Docker)
    PostgreSQL status:
  • listening on 0.0.0.0:5432
  • can connect locally with:
    psql -h localhost -U postgres -d football
    Docker container can ping:
  • 172.17.0.1 successfully
    n8n PostgreSQL credential settings:
  • Host: 172.17.0.1
  • Port: 5432
  • Database: football
  • User: postgres
  • SSL: disabled
    Still cannot connect from n8n.
    postgresql.conf:
    listen_addresses=‘*’
    pg_hba.conf includes:
    host all all 172.17.0.0/16 md5
    host all all 0.0.0.0/0 md5
    Need help identifying why n8n Docker container still cannot authenticate/connect to PostgreSQL host.

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @Sopheak_Sokchai Welcome !

Your config looks correct on paper. To help identify the issue, could you share the actual error message? Run docker logs <container_name> and paste what you see when the connection fails. “Cannot connect” could mean several different things and the exact error determines the fix.

@Sopheak_Sokchai whats the exact error the n8n credential test throws — “connection timeout”, “connection refused”, or “authentication failed”? changes which layer is wrong. timeout or refused = network (ufw blocking 5432 is the default on DO Ubuntu droplets), authentication failed = wrong password or a pg_hba match issue.

Try this, @Sopheak_Sokchai

# Check UFW status
sudo ufw status

# Allow Docker bridge subnet to reach PostgreSQL
sudo ufw allow from 172.17.0.0/16 to any port 5432

# Or temporarily disable UFW to test
sudo ufw disable

Confirm PostgreSQL is actually listening on the Docker bridge IP, not just 127.0.0.1

sudo ss -tlnp | grep 5432
# Should show: 0.0.0.0:5432 — NOT just 127.0.0.1:5432

If it shows only 127.0.0.1, restart PostgreSQL:

sudo systemctl restart postgresql

Does that help?

Hi there,
thanks for responding, this is the log.

root@Football-Test:~# docker logs n8n
Initializing n8n process
n8n ready on ::, port 5678
n8n Task Broker ready on 127.0.0.1, port 5679
Failed to start Python task runner in internal mode. because Python 3 is missing from this system. Launching a Python runner in internal mode is intended only for debugging and is not recommended for production. Users are encouraged to deploy in external mode. See: https://docs.n8n.io/hosting/configuration/task-runners/#setting-up-external-mode
[license SDK] Skipping renewal on init: license cert is not initialized
Registered runner “JS Task Runner” (166YvdQBOvt-jY3fyeeWG)
Instance registered
Discovered 4 cluster checks
Version: 2.21.7
Building workflow dependency index…
Finished building workflow dependency index. Processed 0 draft workflows, 0 published workflows.

Editor is now accessible via:
http://157.245.86.0:5678
(node:7) [DEP0060] DeprecationWarning: The util._extend API is deprecated. Please use Object.assign() instead.
(Use node --trace-deprecation ... to show where the warning was created)
Credential with ID “aMTYp5VkKSOWykDv” does not exist for type “postgres”.
Credential with ID “aMTYp5VkKSOWykDv” does not exist for type “postgres”.
Credential with ID “aMTYp5VkKSOWykDv” does not exist for type “postgres”.
Credential with ID “aMTYp5VkKSOWykDv” does not exist for type “postgres”.
Credential with ID “V7nTlUjfmg9rUtfc” does not exist for type “postgres”.
Credential with ID “V7nTlUjfmg9rUtfc” does not exist for type “postgres”.
Problem with execution 25: The workflow has issues and cannot be executed for that reason. Please fix them first.. Aborting.
The workflow has issues and cannot be executed for that reason. Please fix them first. (execution 25)
The workflow has issues and cannot be executed for that reason. Please fix them first.
Problem with execution 28: The workflow has issues and cannot be executed for that reason. Please fix them first.. Aborting.
The workflow has issues and cannot be executed for that reason. Please fix them first. (execution 28)
The workflow has issues and cannot be executed for that reason. Please fix them first.
root@Football-Test:~#

@Sopheak_Sokchai

Your logs show that the PostgreSQL credentials the workflow references don’t exist in n8n.

To fix: open your workflow, click the PostgreSQL node, and under Credential click Create New. Enter your connection details (host: 172.17.0.1, port: 5432, database: football, user: postgres, password) and save. Then re-run the workflow.

If you imported this workflow from another n8n instance, credentials don’t transfer since they’re tied to the instance’s encryption key. You always need to recreate them.

Let me know if it helps !

Thanks everyone for the response, i might have messed up in some weird places, still can’t get it to work.

Nonetheless, thank you!

The most common reason this still fails even with listen_addresses = '*' is pg_hba.conf - PostgreSQL has two separate controls. Check your /etc/postgresql/*/main/pg_hba.conf and add a line like:

host all all 172.17.0.0/16 md5

This allows connections from the Docker subnet (172.17.x.x). After editing, run sudo systemctl reload postgresql to apply without restarting. Also confirm the n8n credential is using 172.17.0.1 as the host (not localhost - localhost resolves to the container itself, not the host).

This is mostly a DNS issue. By design, a Docker container is not able to connect to the hosted services; you should enable it:

docker run -it -d --name n8n \
-p ... \
-v ... \
--add-host=host.docker.internal:host-gateway \
docker.n8n.io/n8nio/n8n:2.21.7

The most important part here is the --add-host attribute. You should be able to connect to the PostgreSQL via the address host.docker.internal:5432 from within the n8n