Docker + postgres

I am using M2 chip.
Docker version 24.0.5

version: "3.5"
volumes:
  postgres:

services:

  pgsql2:
    ports:
      - "5432:5432"
    container_name: "${LOCALSTACK_DOCKER_NAME-pgsql2}"
    image: postgres:15.3
    restart: always
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_HOST_AUTH_METHOD: trust
      PGDATA: "/data/postgres"
    volumes:
      - "./postgres:/data/postgres"
      - "./dump.sql:/docker-entrypoint-initdb.d/dump.sql"
    user: ${UID}:${GID}

This is my docker compose file. I want to initialize database while running the container.
I am getting this error while doing docker-compose up pgsql2

initdb: error: could not change permissions of directory ā€œ/data/postgresā€: Operation not permitted
pgsql2 | fixing permissions on existing directory /data/postgres ā€¦

Tried most of solution like changing image or using named volumes.
Any help is appreciated!

Your post is in the Community category, which is used to inform us about community events or ask about them. Please let us know how your topic is related to the category.

I moved the topic to the compose category before reading @meyayā€™s post, but Iā€™m pretty sure it was nothing to do with the community. I mean the whole website is for questions answared by the community, but the question is not about the community,

I think you should just read the image description more carefully: postgres
You set the PGDATA to the moint point while the descriptions recommends mounting the parent folder not data

PGDATA

Important Note: when mounting a volume to /var/lib/postgresql, the /var/lib/postgresql/data path is a local volume from the container runtime, thus data is not persisted on the mounted volume.

You probably canā€™t change the ownership of the moint point.

1 Like