Cannot access postgresql container from another container

docker run -p 8080:8080 -t --name my_api --net=host my_api could work

or if you replace localhost with your host ip / name.

Your API container has it’s own network. So its localhost is not your localhost.