1

I'm trying to spin up a postgresl database using docker compose on my mac running macOS 12.4.

My docker-compose.yml file is

version: '3.8'
services:
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: gymbro
    ports:
      - 5438:5432
    volumes:
      - ./pgdata:/var/lib/postgresql/data

Running docker-compose up results in the container exiting with status 1. Here is the relevant output

Attaching to docker-compose-gymbro-db-1
docker-compose-gymbro-db-1  | The files belonging to this database system will be owned by user "postgres".
docker-compose-gymbro-db-1  | This user must also own the server process.
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | The database cluster will be initialized with locale "en_US.utf8".
docker-compose-gymbro-db-1  | The default database encoding has accordingly been set to "UTF8".
docker-compose-gymbro-db-1  | The default text search configuration will be set to "english".
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | Data page checksums are disabled.
docker-compose-gymbro-db-1  | 
docker-compose-gymbro-db-1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
docker-compose-gymbro-db-1  | creating subdirectories ... ok
docker-compose-gymbro-db-1  | selecting dynamic shared memory implementation ... posix
docker-compose-gymbro-db-1  | selecting default max_connections ... 100
docker-compose-gymbro-db-1  | selecting default shared_buffers ... 128MB
docker-compose-gymbro-db-1  | selecting default time zone ... Etc/UTC
docker-compose-gymbro-db-1  | creating configuration files ... ok
docker-compose-gymbro-db-1  | running bootstrap script ... ok
docker-compose-gymbro-db-1  | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL:  data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1  | 2022-12-24 17:42:20.791 UTC [40] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).
docker-compose-gymbro-db-1  | child process exited with exit code 1
docker-compose-gymbro-db-1  | initdb: removing contents of data directory "/var/lib/postgresql/data"
docker-compose-gymbro-db-1 exited with code 1

Seems like there is a permissions issue,

docker-compose-gymbro-db-1  | performing post-bootstrap initialization ... 2022-12-24 17:42:20.791 UTC [40] FATAL:  data directory "/var/lib/postgresql/data" has invalid permissions
docker-compose-gymbro-db-1  | 2022-12-24 17:42:20.791 UTC [40] DETAIL:  Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).

I can spin up the db using a docker volume, but I'm just curious as to why this is happening and if there is a possible solution.

2
  • Especially on MacOS, I might use a named volume here and not worry about this: it will be significantly faster than the bind-mounted directory, and you can't usefully do anything with the opaque on-disk database files. The error message suggests some very specific permission requirements, though; does using chmod(1) from a host terminal window help? Commented Dec 24, 2022 at 18:06
  • @DavidMaze it does not help. If the recommended approach is just to use the named volume, then that's what I'll do. The reason I want to use this approach is because I'm currently developing an app locally that I will eventually send to a Raspberry pi. Commented Dec 24, 2022 at 18:16

1 Answer 1

1

On Linux here, so this might not work on Mac. However, why not avoid using the local volum?

Like:

version: '3'

services:

  db:
    image: postgres:13.2
    env_file:
      - .env
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data: {}

This is a solution I always use and solved my issues once and for all when I used to have it.

However, there is a more interesting discussion on this here that you can go through: https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/22

I have seen some items I honestly won't recommend like changing the PG_DATA, but if you're comfortable with it, have a go.

Let me know what you think :)

Sign up to request clarification or add additional context in comments.

1 Comment

Specifying PGDATA: /tmp in the environment variables fixes the issue. I don't think this is great either, but at least the container doesn't exit with status 1 now. I'lll go through that thread, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.