Software⏱️ 3 min read📅 2026-06-11

How to Fix: Postgres docker error "data directory "/var/lib/postgresql/data" has wrong ownership"

Postgres Docker error data directory has wrong ownership

Quick Answer: Use the --user option to set the ownership of the data directory, for example: docker run -d -e POSTGRES_PASSWORD=secret -p 5432:5432 --user postgres postgres:15.2-alpine

The error 'data directory '/var/lib/postgresql/data' has wrong ownership' occurs when the PostgreSQL container is unable to start due to incorrect file permissions. This issue affects users who run PostgreSQL containers using Docker and have not taken steps to correct the ownership of the data directory.

This error can be frustrating as it prevents the container from starting, and the database cannot be accessed. However, by following the steps outlined in this guide, users can resolve the issue and get their PostgreSQL container up and running.

🛑 Root Causes of the Error

  • The primary reason for this error is that the PostgreSQL data directory has incorrect ownership. The PostgreSQL server requires the data directory to be owned by the user who starts the server process. In a Docker container, the default user is usually 'postgres', but the data directory may have been created with a different owner.
  • An alternative reason could be that the Docker container's file system permissions do not allow the PostgreSQL server process to access the data directory.

🔧 Proven Troubleshooting Steps

Correcting Ownership using Docker Volumes

  1. Step 1: To correct the ownership, create a Docker volume with the correct ownership. This can be done by adding the following command when running the Docker container: `--volumes=postgres-data:/var/lib/postgresql/data -e POSTGRES_USER=postgres`.
  2. Step 2: Alternatively, you can also set the ownership of the data directory manually using the `chown` command before starting the PostgreSQL server. This can be done by running the following command in a terminal window: `sudo chown postgres:postgres /var/lib/postgresql/data`.
  3. Step 3: After correcting the ownership, restart the PostgreSQL container and verify that it starts successfully.

Correcting Ownership using Environment Variables

  1. Step 1: Another way to correct the ownership is by setting the `POSTGRES_USER` environment variable to 'postgres' when running the Docker container. This can be done by adding the following command when running the Docker container: `-e POSTGRES_USER=postgres`.
  2. Step 2: This method assumes that you want to use the default user 'postgres' for your PostgreSQL database.

🎯 Final Words

To resolve the error 'data directory '/var/lib/postgresql/data' has wrong ownership', follow one of the two primary fix methods outlined above. Correcting the ownership using Docker volumes or environment variables will allow you to start your PostgreSQL container successfully and access your database.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions