【问题标题】:Docker-Compose postgres upgrade initdb: error: directory "/var/lib/postgresql/data" exists but is not emptyDocker-Compose postgres 升级initdb:错误:目录“/var/lib/postgresql/data”存在但不为空
【发布时间】:2020-10-23 02:17:50
【问题描述】:

我使用 docker-compose 安装了 postgres 11。我想将它升级到 12,但即使我已经删除了容器及其卷,但容器的状态显示为“正在重新启动”。

这是我的 docker-compose 文件

version: '3.5'
services:
  postgres:
    image: postgres:12
    environment:
      POSTGRES_HOST_AUTH_METHOD: "trust"
    ports:
    - "5432"
    restart: always
    volumes:
    - /etc/postgresql/12/postgresql.conf:/var/lib/postgresql/data/postgresql.conf
    - db_data:/var/lib/postgresql/data
volumes:
  db_data:

但是它不工作并且日志有以下问题

2020-07-02T12:54:47.012973448Z The files belonging to this database system will be owned by user "postgres".
2020-07-02T12:54:47.013030445Z This user must also own the server process.
2020-07-02T12:54:47.013068962Z 
2020-07-02T12:54:47.013222608Z The database cluster will be initialized with locale "en_US.utf8".
2020-07-02T12:54:47.013261425Z The default database encoding has accordingly been set to "UTF8".
2020-07-02T12:54:47.013281815Z The default text search configuration will be set to "english".
2020-07-02T12:54:47.013293326Z 
2020-07-02T12:54:47.013303793Z Data page checksums are disabled.
2020-07-02T12:54:47.013313919Z 
2020-07-02T12:54:47.013450079Z initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
2020-07-02T12:54:47.013487706Z If you want to create a new database system, either remove or empty
2020-07-02T12:54:47.013501126Z the directory "/var/lib/postgresql/data" or run initdb
2020-07-02T12:54:47.013512379Z with an argument other than "/var/lib/postgresql/data".

当容器不断重启时,如何删除或清空这个 /var/lib/postgresql/data?

提前致谢

【问题讨论】:

  • 从 Postgres 11 升级 --> 12 是一项重大升级,您不能只用一个替换另一个。如果 11 实例有与之关联的数据,您需要将 12 实例安装在另一个位置,然后执行转储/恢复或使用 pg_upgrade。
  • 未来用户:请检查配置错误。对我来说,这是 yml 文件中的错字。

标签: postgresql docker docker-compose


【解决方案1】:

this issue引用@yosifkit

卷需要为空或有效的已初始化 postgres 带有文件PG_VERSION 的数据库,因此可以进行初始化 跳过。

...如果那里有像lost+found这样的任何文件或文件夹 可能无法初始化。如果有你想要的文件 保持音量(或无法控制),您可以调整 PGDATA 环境变量指向那里的子目录 喜欢-e PGDATA=/var/lib/postgresql/data/db-files/

所以我将PGDATA 添加到撰写文件的environment 部分以解决问题(注意末尾的some_name):

services:
  postgres:
    image: postgres:12
    environment:
      PGDATA: /var/lib/postgresql/data/some_name/

【讨论】:

  • 在我的情况下,添加PG_DATA(擦除旧数据,也许你有一些备份要恢复),然后下次重新启动时,它会重新启动。
  • 谢谢!我为此苦苦挣扎了很长时间,但添加 PG_data 也对我有用
【解决方案2】:

另一种解决方案是简单地删除附加到容器的卷:

docker-compose down -v

【讨论】:

  • 并且丢失了所有数据。
  • 当然这条评论是针对开发环境的。如果您在生产中的容器中运行 postgres,并且不希望机器因任何原因关闭,我担心您有更大的问题要解决:)
【解决方案3】:

我今天遇到了同样的问题,我通过删除卷 db_data 的内容来解决它

docker volume ls
docker volume inspect db_data <-- will show you the mountpoint

我去了目录(挂载点)例如:/path/data

cp data data.backup
cd data
rm -R *

并启动服务:

docker-compose up -d

【讨论】:

    【解决方案4】:

    我遇到这个问题是因为/var/lib/postgresql/data/postgresql.conf/var/lib/postgresql/data/var/lib/postgresql/ 的docker 容器中重叠。

    一个损坏的配置示例是:

    version: "3.8"
    services:
      db:
        image: "postgres:10"
        ports:
          - "5432:5432"
        volumes:
          - ./postgresql.conf:/var/lib/postgresql/data/postgresql.conf
          - ./pg-data:/var/lib/postgresql/data
    

    为了避免这种情况,我告诉 PostgreSQL 在 /etc/postgresql.conf 中找到它的配置,这样就不会像这样发生重叠卷:

    version: "3.8"
    services:
      db:
        image: "postgres:10"
        command: ["postgres", "-c", "config_file=/etc/postgresql.conf"]
        ports:
          - "5432:5432"
        volumes:
          - ./postgresql.conf:/etc/postgresql.conf
          - ./pg-data:/var/lib/postgresql/data
    

    这类似于pmsoltani 的建议,但我移动了postgresql.conf 文件的位置而不是数据目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 2021-11-24
      • 2019-07-22
      • 1970-01-01
      相关资源
      最近更新 更多