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 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"

    container_name: appsmith
      - "8002:80"
      # - "443:443"
      # - "9001:9001"
      - ./stacks:/appsmith-stacks
      com.centurylinklabs.watchtower.enable: "true"
    restart: unless-stopped

    image: containrrr/watchtower:latest-dev
      - /var/run/docker.sock:/var/run/docker.sock
    # Update check interval in seconds.
    command: --schedule "0 0 * ? * *" --label-enable --cleanup
    restart: unless-stopped
    image: postgres
      - POSTGRES_USER=fakeuser
      - POSTGRES_PASSWORD=fakepassword
      - POSTGRES_DB=fakeDB
      - "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