Docker NodeJS app cant connect to postgres database

When using networks with docker-compose you should use the name of the service as you hostname.

so in your case the hostname should be postgres and not localhost

You can read more about at here: https://docs.docker.com/compose/networking/