PostgreSQL docker container on Windows PostgreSQL docker container on Windows postgresql postgresql

PostgreSQL docker container on Windows


This is known limitation in Docker for Windows. The Linux <-> Windows filesystem mapping semantics from your Windows dir to the Linux dir are imperfect because the mount is done with CIFS/SMB. One of the things that won't work is chown (changing the owner) because that cannot be mapped to your Windows filesystem.

You should probably use a named volume instead. This forum post has details: https://forums.docker.com/t/data-directory-var-lib-postgresql-data-pgdata-has-wrong-ownership/17963/24?u=friism


I posted a partial solution to one of the related git issue links.A partial workaround can be done by using Postgres tablespaces to store the database object files on the windows host system. It is not a perfect solution because the core data directory (represented by PGDATA) still resides on the LinuxVM. But at least you can manage subsequent database creation and file storage on the Windows host and outside the Linux VM.

docker run --rm --name mypostgres           -e POSTGRES_PASSWORD=pwd           -d -p 5432:5432            -v /var/lib/docker/basedata:/var/lib/postgresql/data           -v d:/data/largedb:/mnt/largedb           postgres

This sets the default data storage to a (persistent) directory on the Linux VM (/var/lib/docker/basedata).It also tells docker to mount your windows D:\data\largedb as a volume that is visible to the Postgres container as /mnt/largedb. (The windows directory need not exist yet.)Log into postgres and using psql or whatever tool, execute the following DDL:

CREATE TABLESPACE winhoststorage LOCATION '/mnt/largedb';CREATE DATABASE "my_large_db" WITH TABLESPACE = "winhoststorage";

Go ahead and create tables and data in my_large_db. Stop the containter. Navigate to D:\data\largedb in Windows Explorer and you will see data files. Restart the container and you will see the data from the previous session.

(Note: Docker Desktop for Windows (2.2) on Win 10 which uses the DockerDesktopVM and not MobyLinux or docker-machine.)


Are you using docker with hyper-v or docker-toolbox? Because in my experience, I used only docker-toolbox (it uses docker-machine), and to map a volume successfully using --volume or -v (is the same) you should put this nomenclature for the Win path:

docker run -p 5432:5432 -it -v /c/Users/me/Desktop/pg/data:/var/lib/postgresql/data postgres:latest -e POSTGRES_USER=user POSTGRES_PASSWORD=password

Not sure if it can help. Maybe your problem is because of the path for the mapped volume. Good luck!