Mattermost is unable to connect to Postgres database when using Docker

For feature requests, please see:

For troubleshooting questions, please post in the following format:

Mattermost is unable to connect to the Postgres database when deployed using Docker and Docker compose

Steps to reproduce
According to the deployment instructions, I cloned the repository, filled in the relevant environment variables (I had to hardcode them because the .env file wouldn’t recognise the environment variables that were declared within itself), then ran docker-compose -f docker-compose.yml -f docker-compose.without-nginx.yml up -d

Expected behaviour
Docker would start, and I would be able to use Mattermost.

Observed behaviour
I was unable to connect to Mattermost. On checking the container logs with docker logs <uuid> -f, the predominant error was

{"timestamp":"2022-04-24 15:39:26.471 Z","level":"fatal","msg":"Failed to ping DB, server will exit.","caller":"sqlstore/store.go:270","error":"pq: password authentication failed for user \"mattermost\""}

Which seems to me that the instance is unable to connect to the Postgres database. Does anyone have any suggestions?

I eventually managed to fix this by deleting the ./volumes/db directory and regenerating it, so it seems to have been corrupted.