How to connect to postgres instance on same docker host?

I am running both postgres and appsmith on the same docker host but get refused to connect when trying to add postgres as a data source. It gets refused when using the servers public IP, docker internal IP, and localhost. What should I be using in the host address field for my PG data source?

An example error from running docker logs -f appsmith
backend stdout | org.postgresql.util.PSQLException: Connection to 172.17.0.1:21003 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Here is my compose file

version: "3"

services:
  appsmith:
    image: index.docker.io/appsmith/appsmith-ce
    container_name: appsmith
    ports:
      - "8002:80"
      # - "443:443"
      # - "9001:9001"
    volumes:
      - ./stacks:/appsmith-stacks
    labels:
      com.centurylinklabs.watchtower.enable: "true"
    restart: unless-stopped

  auto_update:
    image: containrrr/watchtower:latest-dev
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    # Update check interval in seconds.
    command: --schedule "0 0 * ? * *" --label-enable --cleanup
    restart: unless-stopped
  db:
    image: postgres
    environment:
      - POSTGRES_USER=fakeuser
      - POSTGRES_PASSWORD=fakepassword
      - POSTGRES_DB=fakeDB
    ports:
      - "21003:5432"

Please try db as the host in your Datasource configuration.

This is documented at Networking in Compose | Docker Documentation, the first few paragraphs should get you what you need.

1 Like

Hey Joey,

This might not apply to your scenario, but I found that for just developing and testing you can use host.docker.internal for the host address instead of localhost or an IP.

I do not recommend this for production environments, but for developing and testing this is a quick workaround.

Documentation reference: Explore networking features | Docker Documentation