Quoting @yosifkit from this issue
The volume needs to be empty or a valid already initialized postgres
database with the filePG_VERSIONin there so the init can be
skipped.… If there are any files or folders in there like
lost+foundit
will probably fail to initialize. If there are files that you want to
keep in the volume (or have no control over) you could adjust the
PGDATAenvironment variable to point to a sub-directory in there
like-e PGDATA=/var/lib/postgresql/data/db-files/.
So I added PGDATA to the environment section of the compose file to solve the issue (notice the some_name at the end):
services:
postgres:
image: postgres:12
environment:
PGDATA: /var/lib/postgresql/data/some_name/