Docker-Compose postgres upgrade initdb:错误:目录“/var/lib/postgresql/data”存在但不是空的

xe55xuns  于 2023-05-16  发布在  Docker
关注(0)|答案(5)|浏览(465)

我用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?
先谢谢你了

lo8azlld

lo8azlld1#

引用@yosifkit来自this issue
卷必须是空的,或者是一个有效的已经初始化的postgres数据库,其中包含文件PG_VERSION,这样就可以跳过init。
...如果有任何文件或文件夹在那里像lost+found它可能会无法初始化。如果您想在卷中保留一些文件(或者无法控制),可以调整PGDATA环境变量,使其指向其中的子目录,如-e PGDATA=/var/lib/postgresql/data/db-files/
因此,我将PGDATA添加到compose文件的environment部分来解决这个问题(请注意最后的some_name):

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

d7v8vwbk2#

我遇到这个问题是因为/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文件的位置,而不是数据目录。

gcxthw6b

gcxthw6b3#

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

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

我转到目录(mountpoint)ex:/path/data

cp data data.backup
cd data
rm -R *

并启动服务:

docker-compose up -d
sxissh06

sxissh064#

它弹出作为第一个搜索结果没有批准的答案,所以让我在这里添加我的解决方案。
我想在/var/lib/postgresql/data中提供自定义的postgresql.confpg_hba.conf,其中文件默认存储在图像postgres:latest中(截至撰写本文时为PostgreSQL 15)。
镜像提供了一个目录,您可以在其中存储.sql.sh文件以初始化数据库:/docker-entrypoint-initdb.d/
COPYpostgresql.confpg_hba.conf文件精心制作为/tmpCOPY,这是一个简短的Bash脚本,将配置文件移动到/var/lib/postgresql/data中。它确保调用的正确顺序:首先是initdb,然后替换配置文件。

h6my8fg2

h6my8fg25#

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

docker-compose down -v

相关问题