[英]Docker-Compose postgres upgrade initdb: error: directory "/var/lib/postgresql/data" exists but is not empty
I had postgres 11 installed using docker-compose.我使用 docker-compose 安装了 postgres 11。 I wanted to upgrade it to 12 but even though I have removed the container and its volume but the status of the container says "Restarting".
我想将它升级到 12,但即使我已经删除了容器及其卷,但容器的状态显示为“正在重新启动”。
Here is my docker-compose file这是我的 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:
However it is not working and the logs has the following issue但是它不起作用,并且日志有以下问题
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".
How could I remove or empty this /var/lib/postgresql/data when the container is constantly restarting?当容器不断重新启动时,如何删除或清空这个 /var/lib/postgresql/data ?
Thanks in advance提前致谢
Quoting @yosifkit from this issue从这个问题引用@yosifkit
The volume needs to be empty or a valid already initialized postgres database with the file
PG_VERSION
in there so the init can be skipped.该卷需要是空的或一个有效的已经初始化的 postgres 数据库,其中包含文件
PG_VERSION
以便可以跳过初始化。... If there are any files or folders in there like
lost+found
it will probably fail to initialize....如果那里有任何文件或文件夹,例如
lost+found
它可能无法初始化。 If there are files that you want to keep in the volume (or have no control over) you could adjust thePGDATA
environment variable to point to a sub-directory in there like-e PGDATA=/var/lib/postgresql/data/db-files/
.如果您想保留卷中的文件(或无法控制),您可以调整
PGDATA
环境变量以指向其中的子目录,例如-e PGDATA=/var/lib/postgresql/data/db-files/
。
So I added PGDATA
to the environment
section of the compose file to solve the issue (notice the some_name
at the end):所以我将
PGDATA
添加到 compose 文件的environment
部分来解决这个问题(注意最后的some_name
):
services:
postgres:
image: postgres:12
environment:
PGDATA: /var/lib/postgresql/data/some_name/
I had the same issue today, I fixed it by removing the content of the volume db_data in your case我今天遇到了同样的问题,我通过在您的情况下删除卷 db_data 的内容来修复它
docker volume ls
docker volume inspect db_data <-- will show you the mountpoint
I went to the directory (mountpoint) ex: /path/data我去了目录(挂载点)例如:/path/data
cp data data.backup
cd data
rm -R *
And start services:并启动服务:
docker-compose up -d
I got this issue because the /var/lib/postgresql/data/postgresql.conf
and the /var/lib/postgresql/data
overlap in the docker container at /var/lib/postgresql/
.我遇到了这个问题,因为
/var/lib/postgresql/data/postgresql.conf
和/var/lib/postgresql/data
在/var/lib/postgresql/
的 docker 容器中重叠。
An example of a broken config is:损坏配置的示例是:
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
To avoid this I tell PostgreSQL to find it's config in /etc/postgresql.conf
instead so the overlapping volumes don't occur like this:为了避免这种情况,我告诉 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
This is similar to what pmsoltani suggests, but I move the location of the postgresql.conf
file instead of the data directory.这类似于pmsoltani的建议,但我移动了
postgresql.conf
文件的位置,而不是数据目录。
Another solution is to simply remove the volume attached to the container:另一种解决方案是简单地删除附加到容器的卷:
docker-compose down -v
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.