Docker-Compose postgres升级initdb:错误:目录“ / var / lib / postgresql / data”存在但不为空

时间:2020-07-02 13:06:18

标签: postgresql docker docker-compose

我使用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?

预先感谢

3 个答案:

答案 0 :(得分:2)

@yosifkit引述this issue

该卷必须为空或有效的已经初始化的postgres 数据库中包含文件PG_VERSION,因此可以将init 跳过了。

...如果其中有任何文件或文件夹,例如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/

答案 1 :(得分:2)

我今天遇到了同样的问题,我通过在您的案例中删除卷 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

答案 2 :(得分:1)

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

docker-compose down -v