Issue
I am trying to add Postgres as a data source in Appsmith, which both run on the same Docker host. However, I keep getting refused to connect errors when using the server's public IP, Docker internal IP, and localhost in the host address field. I need to know what to use as the host address for my PG data source.
Resolution
When running both Appsmith and PostgreSQL on the same Docker host—especially using `docker-compose`—you might encounter connection errors such as: `Connection refused`. This often happens due to incorrect hostnames used when configuring the Postgres data source in Appsmith.
Recommended Host Configuration:
There are two common scenarios based on your setup:
1. Using Docker Compose (Same Docker Network)
If both Appsmith and Postgres are defined as services in the same `docker-compose.yml` file, Docker automatically places them in the same network.
In this case, you should use the Postgres service name as the host in Appsmith’s data source configuration.
Example `docker-compose.yml`:
version: "3"
services:
appsmith:
image: appsmith/appsmith-ee
ports:
- "80:80"
depends_on:
- db
db:
image: postgres
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
Use this in Appsmith's host field: `db`
2. Using Standalone Docker Containers (Not in Compose Network)
If Postgres is running in a separate container not defined in the same Docker Compose file, use: `host.docker.internal`
This hostname resolves to the host machine from inside the Appsmith container. It works on:
- Docker for Mac
- Docker for Windows
- Linux (with some configuration)
Note: `host.docker.internal` is not recommended for production. Use explicit networking or shared Docker networks in production environments.
Common Mistakes:
- localhost / 127.0.0.1: Refers to the Appsmith container itself, not the host or Postgres container.
- Public IP / Internal IP: May fail due to Docker network isolation or firewall settings.
Related Resources:
- [Working with Local APIs on Appsmith](https://docs.appsmith.com/connect-data/how-to-guides/how-to-work-with-local-apis-on-appsmith)
By correctly identifying how your containers are networked and using the appropriate hostname (`db` or `host.docker.internal`), you can successfully connect Appsmith to your local Postgres instance.