How to connect to postgres container from a node container using docker-compose?
docker-compose contains an internal DNS. In the example the service names are also the DNS names. In this case, the DNS of the database is db. If one configure this as a hostname in the node app then one should be able to access the database.

Back in the day, the wordpress example of docker-compose helped me understanding this. When I changed WORDPRESS_DB_HOST: db:3306 to WORDPRESS_DB_HOST: db2:3306, the wordpress was not able to access the mysql db anymore as depicted in the log. When it was changed back and forth to db and to db2 it was not able to access it and with db it was. Then I realized that the service names are actually added to an internal DNS that can be resolved by other services that are defined in the docker-compose.yml.

Recently I realized that running docker-compose up and specifying multiple files using -f also ensures that all services get added to the internal DNS. Initially I thought that the resolving was restricted to the same docker-compose.yml, but probably the docker network is responsible. If multiple apps reside in the same network, in this case the default, then they can resolve each other.