Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL docker container on Windows

I've been trying to run a PostgresSQL Docker container on my Windows machine and mounting the data volume, using the following command:

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

However I keep getting a list of permission denied errors when the container tries to spin up:

chown: changing ownership of ‘/var/lib/postgresql/data/pg_log’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical/mappings’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical/snapshots’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_logical’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/members/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/members’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/offsets/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact/offsets’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_multixact’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_notify/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_notify’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_replslot’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_serial’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_snapshots’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/db_0.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/db_16395.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp/global.stat’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_stat_tmp’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_subtrans/0000’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_subtrans’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_tblspc’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_twophase’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/PG_VERSION’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/00000001000000000000000A’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/00000001000000000000000B’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog/archive_status’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/pg_xlog’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.auto.conf’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postgresql.conf’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.opts’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data/postmaster.pid’: Permission denied
chown: changing ownership of ‘/var/lib/postgresql/data’: Permission denied

Can someone point out what I'm doing wrong?

Thanks in advance.

like image 711
cudiaco Avatar asked Apr 19 '17 19:04

cudiaco


People also ask

Can you run PostgreSQL on Windows?

PostgreSQL was developed for UNIX-like platforms, however, it was designed to be portable. It means that PostgreSQL can also run on other platforms such as macOS, Solaris, and Windows. Since version 8.0, PostgreSQL offers an installer for Windows systems that makes the installation process easier and faster.

Which Docker command will you use to execute commands to a PostgreSQL Docker container?

Run PostgreSQL on Docker Containers The first option uses Docker Compose, a tool for managing multi-container Docker applications. You can use Docker Compose to configure the Postgres as a service running inside of a container. In that case, you create a yaml file with all the specifications.

Where does Postgres store Docker?

User Defined Volume To circumvent this issue, we can use the information we gathered earlier that showed us that the volume is mounted at /var/lib/postgresql/data. Inside the container, this directory is where Postgres stores all the relevant tables and databases.


2 Answers

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

like image 191
friism Avatar answered Sep 18 '22 02:09

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.)

like image 43
mdisibio Avatar answered Sep 21 '22 02:09

mdisibio