How to specify different port for a Docker postgres instance?

Inside the container, so in DATABASE_URL the port needs to stay the same 5432. In the docker-compose you only map the existing port 5432 to the outside world as 5433 using the given

ports:
    - "5433:5432"

In your docker-compose you can add this: (Long-Syntax)

ports:  
  - target: 80
    published: 8080
    protocol: tcp
    mode: host

Where,

  • target: the port inside the container
  • published: the publicly exposed port
  • protocol: the port protocol (tcp or udp)
  • mode: host for publishing a host port on each node, or ingress for a swarm mode port to be load balanced.

Alternatively a one-liner: (Short-Syntax)

ports:
  - "4040:5432" # HOST:CONTAINER

Where,

  • 4040 is port to be exposed on the HOST
  • 5432 is port exposed on container

NOTE:

You may experience erroneous results when using a container port lower than 60, because YAML parses numbers in the format xx:yy as a base-60 value. For this reason, we recommend always explicitly specifying your port mappings as strings.