Network bridge: listen tcp 0.0.0.0:5432: bind: address already in use

I occasionally get the following error when starting a PostgreSQL container:

ERROR: Cannot start container 8a598e8de967c74e855822027446722031e574b201ade5566146ce38b1085a41: failed to create endpoint blockai_postgres_1 on network bridge: listen tcp 0.0.0.0:5432: bind: address already in use

This is despite both stoping postgresql and waiting for the 5432 port to be available!

test:
  # see https://discuss.circleci.com/t/docker-run-error-bind-address-already-in-use/294/10?u=olalonde
  pre:
    - sudo service postgresql stop
    # wait for postgresql to shutdown
    - while lsof -Pi :5432 -sTCP:LISTEN -t; do sleep 1; done
    - $(aws ecr get-login --region us-west-1)
  override:
    - make run-test

Has anyone figured a way to prevent that issue?

Seems to work with sudo:

   - sudo service postgresql stop
    # wait for postgresql to shutdown
    - while sudo lsof -Pi :5432 -sTCP:LISTEN -t; do sleep 1; done
```