Connection to localhost:5333 refused. Why it's refusing?

Basically I have a database feedback created in postgres and for creating the table I used flyway. The table script is located in resources/db.migration. Postgres is configured using spring properties. But I want to run my application using docker compose and when I run it I get this error that says

postgres-db | Message : Connection to localhost:5333 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

Docker compose configuration

version: "3.8"

networks:
  app:
    driver: bridge

services:
  postgres:
    image: postgres:16.0-alpine
    container_name: postgres-db
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      - ./initdb:/docker-entrypoint-initdb.d
    ports:
      - 5333:5432
    environment:
      - POSTGRES_DB=feedback_db
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=1
    healthcheck:
      test: \["CMD", "pg_isready", "-q", "-U", "postgres", "-d", "feedback_db"\]
      interval: 5s
      timeout: 1s
      retries: 5
    networks:
      - app

  flyway:
    image: flyway/flyway
    command: -url=jdbc:postgresql://postgres:5432/feedback_db -schemas=public -user=postgres -password=1 migrate
    volumes:
      - ./src/main/resources/db/migration:/flyway/sql
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - app

  feedback-service:
    build: .
    container_name: rugid-feedback-service
    ports:
      - 8082:8080
    depends_on:
      postgres:
        condition: service_healthy
    environment:
      - SPRING_FLYWAY_URL=jdbc:postgresql://postgres:5432/feedback_db
      - SPRING_FLYWAY_USER=postgres
      - SPRING_FLYWAY_PASSWORD=1
      - SPRING_R2DBC_URL=r2dbc:postgresql://postgres:5432/feedback_db
      - SPRING_R2DBC_USERNAME=postgres
      - SPRING_R2DBC_PASSWORD=1
    networks:
      - app

I’m not sure how you connect to the port, but the error message is from the database container, so it looks like something executes a command inside the database container trying to connect to port 5333 inside, which is available only from the outside.

So port 5333 should be used only from outside containers and port 5432 can be used from inside a container. If it is not the same container in which the database is running, use the service name as you did according to the compose file, but something still uses “localhost” from the wrong context.

update

On second thought, excuting something in the container would not send logs to the container’s standard streams only if the main process is executing something or its children. I haven’t seen these kind of postgres logs recently, so I’m not sure I interpreted it correctly. It is still true that port 5333 would only be available on localhost from outside containers.

We don’t know what the folder contains. Afaik, the initdb mechanism executes whatever .sql or .sh file is inside that folder (when the database was not initialized yet).

This type of error could make sense if at least one .shfile uses psql and uses invalid connection details. The containerized process itself would never neither know that a host port is published for a container port, nor what the host port would be.