postgresql - WSL2 和 Docker 桌面上的 Postgres 卷安装 : Permission Denied on PGDATA folder

标签 postgresql docker docker-volume ubuntu-20.04 wsl-2

有一些类似的帖子,但这与在 Docker 桌面上运行带有 WSL2 后端的 Postgres 特别相关。 WSL2 在 Windows 上带来完整的 Linux 体验。卷可以挂载到 Windows 和 Linux 文件系统。但出于性能原因,最佳做法是使用 Linux 文件系统,请参阅 docker documentation .

Performance is much higher when files are bind-mounted from the Linux filesystem, rather than remoted from the Windows host. Therefore avoid docker run -v /mnt/c/users:/users (where /mnt/c is mounted from Windows). Instead, from a Linux shell use a command like docker run -v ~/my-project:/sources where ~ is expanded by the Linux shell to $HOME.

我的 WSL 发行版是 Ubuntu 20.04 LTS。我将安装 Postgres 数据目录绑定(bind)到 Linux 文件系统上的目录,并且我还将 Postgres PGDATA 配置为使用子目录,因为官方 Docker 图像文档中对此进行了说明:

PGDATA This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data. If the data volume you're using is a filesystem mountpoint (like with GCE persistent disks) or remote folder that cannot be chowned to the postgres user (like some NFS mounts), Postgres initdb recommends a subdirectory be created to contain the data.

这就是我启动 Postgres 并将卷安装到 WSL2 Ubuntu 文件系统的方式:

docker run -d \
--name some-postgres -e POSTGRES_PASSWORD=root \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v ~/custom/mount:/var/lib/postgresql/data \
postgres

我可以执行到正在运行的容器并验证数据文件夹是否存在并且配置是否正确:

enter image description here

现在,如果我尝试从主机 (WSL2 Linux) 访问该文件夹,我将获得被拒绝的权限:

enter image description here

如果有人能提供解决方案,我将不胜感激。现有帖子均无法解决该问题。

最佳答案

这与 PostgreSQL 无关。 Docker 容器以 root 运行,因此 Docker 创建的任何目录也将属于 root

关于postgresql - WSL2 和 Docker 桌面上的 Postgres 卷安装 : Permission Denied on PGDATA folder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64357189/

相关文章:

docker - 在挂载 “hostpath PV ”到Pod时保留Pod目录文件

Docker-compose 命名挂载卷

Dockerfile,使用 VOLUME 持久化数据

docker - 使用 compose 重新创建命名卷

ruby - Chef 独奏 : Installing build-essential recipe before postgresql

postgresql - rds 在 postgres 9.6.6 上运行,而 yum update 将导致安装版本更新到 9.6.8

java - HTTP 状态 500 - java.lang.NoClassDefFoundError : java/time/temporal/TemporalField when run app on OpenShift

postgresql - 为什么 atttypmod 不同于 character_maximum_length?

docker - 如何在 Traefik v2.3 中使用粘性 session ?

docker - Vue UI 无法通过 docker-compose 工作