Fix App Can't Reach Its Database on Coolify
Error: ECONNREFUSED / ENOTFOUND / connection to server at "postgres" port 5432 failed
Your app can't resolve or connect to its database because they aren't on the same Docker network, or it's using the wrong hostname (localhost instead of the service name).
Coolify containers talk to each other over Docker's internal network using the service name as the hostname, never via localhost. Two failure modes. (1) The connection string uses localhost or 127.0.0.1, which inside a container points at the container itself, not the DB, giving ECONNREFUSED. (2) The app and DB live in different Coolify resources/stacks. By default Coolify isolates every resource on its own network (named after the resource UUID), so containers in separate stacks can't resolve each other's names, giving ENOTFOUND/getaddrinfo failures. Same-stack services resolve each other by plain service name automatically; cross-stack ones do not.
The fix
In your app's connection string, use the database's service name as the host, not localhost or 127.0.0.1. For a DB and app in the same Docker Compose stack, the plain service name works, e.g. DB_HOST=postgres and the internal port DB_PORT=5432.
List running containers to confirm the exact service/container name to target.
docker ps --format 'table {{.Names}}'If the app and database are in DIFFERENT Coolify resources/stacks, open the app (and the DB if it's a standalone resource) and enable 'Connect to Predefined Network'. This attaches them to Coolify's shared 'coolify' network so they can reach each other across stacks.
IMPORTANT: once 'Connect to Predefined Network' is on, the plain service name may no longer resolve via internal DNS. Use the UUID-suffixed hostname Coolify assigns instead, e.g. DB_HOST=postgres-<uuid> (the full resource name shown in the Coolify UI). Copy it exactly from the database resource page.
Where possible, keep related services in the SAME Coolify project/environment (or the same Compose stack) so Coolify wires the network automatically and plain service names just work.
Redeploy the app so the new hostname and network setting take effect.
Verify it worked
From inside the app container, confirm it can resolve and reach the DB host by name. Run getent (present on most Linux images) to check DNS resolves to an IP; a returned address means the network is correct.
docker exec -it <your-app-name> getent hosts <db-service-name>Related Coolify errors
- Timeout / no response / container not starting
- no available server (Traefik 503) behind your Coolify domain after a successful deploy
- 502 Bad Gateway (Traefik) — build succeeds and container runs, but your-domain.com returns 502 Bad Gateway
- Healthcheck failed / Container is unhealthy — new container never receives traffic, deploy rolls back to the old container (or shows 404 "No available server")