ECONNREFUSED Postgres Error in n8n Docker Compose

\n ```html

ECONNREFUSED Postgres Error in n8n Docker Compose: A DevOps Engineer's Guide to Resolution

Encountering the "ECONNREFUSED" error when your n8n Docker Compose setup attempts to connect to the PostgreSQL database is a common but frustrating problem. This typically manifests as n8n failing to start or workflow executions failing due to an inability to reach the database. This guide breaks down the usual suspects and provides step-by-step solutions to get your n8n instance up and running smoothly.

Quick Summary: The "ECONNREFUSED" error in n8n indicates that n8n's container cannot establish a connection to your PostgreSQL database. This usually boils down to issues with the database container not being ready, incorrect connection parameters, or firewall restrictions.

1. Database Container Not Ready or Unreachable

The most frequent cause is the PostgreSQL database container not being fully initialized or accessible when n8n tries to connect. Docker Compose orchestration involves container startup order, and if n8n attempts to connect *before* the database is ready, it will fail.

Resolution Steps:

  1. Check Container Logs: Use Docker Compose to view the logs for both the `n8n` and `postgres` containers. Look for any errors or messages related to database startup.
  2. Ensure Correct Dependency Order: Verify your `docker-compose.yml` file's configuration. Ensure that n8n depends on the PostgreSQL service. You might need to use `depends_on` or utilize health checks. Here's an example:
  3. version: "3.9"
    services:
    postgres:
    image: postgres:15
    # ... other configurations
    n8n:
    image: n8n/n8n:latest
    depends_on:
    - postgres
    # ... other configurations
  4. Implement Health Checks (Recommended): Introduce health checks for the PostgreSQL container to ensure it's fully operational before n8n attempts to connect.
    • Add a `healthcheck` section to your `postgres` service in `docker-compose.yml`:
    postgres:
    image: postgres:15
    healthcheck:
    test: ["CMD-SHELL", "pg_isready -U postgres"]
    interval: 10s
    timeout: 5s
    retries: 5
    • Adjust the `depends_on` in your `n8n` service to reflect the health check:
    n8n:
    image: n8n/n8n:latest
    depends_on:
    - postgres: # Changed to the service, not just a service name
    condition: service_healthy
  5. Restart and Test: After making these changes, rebuild and restart your containers: `docker-compose up --build -d`. Monitor the logs again.

2. Incorrect Database Connection Parameters

Another common cause is incorrect configuration of the connection parameters that n8n uses to connect to the PostgreSQL database. This includes incorrect hostnames, port numbers, database names, usernames, or passwords.

Resolution Steps:

  1. Verify Environment Variables: Examine the environment variables you've configured for your n8n container, especially these critical ones:
    • `DB_TYPE`: Should be set to `postgresDB`.
    • `POSTGRES_HOST`: The hostname or IP address of your PostgreSQL container (usually "postgres" if they're in the same Docker network).
    • `POSTGRES_PORT`: The port your PostgreSQL instance is listening on (default is 5432).
    • `POSTGRES_DB`: The name of your database.
    • `POSTGRES_USER`: The PostgreSQL username.
    • `POSTGRES_PASSWORD`: The PostgreSQL password.
    n8n:
    image: n8n/n8n:latest
    environment:
    - DB_TYPE=postgresDB
    - POSTGRES_HOST=postgres # Assumes your Postgres service is named "postgres"
    - POSTGRES_PORT=5432
    - POSTGRES_DB=n8n
    - POSTGRES_USER=n8nuser
    - POSTGRES_PASSWORD=your_secure_password
  2. Confirm Network Connectivity: Ensure that the n8n container can *resolve* the hostname of the PostgreSQL container. This is usually managed by Docker Compose itself. If you're using a custom network, make sure both services are on it.
  3. Database Credentials: Double-check the username and password in your `docker-compose.yml` file against the ones you created for your PostgreSQL database.
  4. Database Name: Verify that the database name specified in `POSTGRES_DB` exists in your PostgreSQL instance. You can create it manually if necessary: `docker exec -it psql -U postgres -c "CREATE DATABASE n8n;"`.

3. Firewall or Network Restrictions

Firewall rules or network configurations on the host machine or within the Docker network can prevent the n8n container from connecting to the PostgreSQL database.

Resolution Steps:

  1. Check Host Firewall: Verify that the host machine's firewall allows connections on the PostgreSQL port (default 5432) from the Docker network. Use commands like `ufw status` (Ubuntu) or `firewall-cmd --list-all` (CentOS/RHEL) to inspect firewall rules.
  2. Docker Network Configuration: In rare cases of custom Docker network configurations, ensure that there aren't any network isolation policies preventing communication between the n8n and PostgreSQL containers.
  3. Test Port Accessibility: Use tools like `telnet` or `nc` (netcat) from inside the n8n container to test connectivity to the PostgreSQL container on the correct port. Example: `docker exec -it sh -c "nc -zv postgres 5432"`. A successful connection will show "succeeded" or a similar message.
  4. Debugging with a Shell: Enter the n8n container's shell (e.g., `docker exec -it sh`) and try to connect using `psql`. This helps isolate whether the issue is n8n-specific or a general connectivity problem. Use the connection string from your `n8n` configuration. Example: `psql -h postgres -U n8nuser -d n8n`.

Comparison Table: Troubleshooting Steps

Issue Possible Cause Resolution
Database Container Not Ready Startup order, health check failure Verify `depends_on`, Implement Health Checks, check logs.
Incorrect Connection Parameters Mismatched environment variables. Double-check `POSTGRES_*` variables, verify database exists.
Firewall or Network Restrictions Blocked traffic on port 5432 (default). Inspect host firewall, test port accessibility with `nc` or `telnet`.

Leverage AI for Flawless Workflow Creation

Tired of debugging connection errors? Generate perfect, error-free n8n workflows with the power of Scriflow AI. Automate your automation!

``` \n
\n
Stuck with nodes? Generate workflows with AI in 10 seconds.
Try Scriflow Free ⚡