depends_on alone only waits for the container to start, not for the process inside to be ready. Add a healthcheck and depend on that condition:
That said — make the app retry its initial connection anyway. In production there is no Compose orchestrating startup order, and databases restart independently of your app. The healthcheck fixes local development; retry logic fixes reality.