Why is pg_restore segfaulting in Docker?

时光毁灭记忆、已成空白 提交于 2021-01-28 07:59:35

问题


I am testing a backup/restore procedure for my postgres DB inside a docker container.

I dump my db like this:

$ docker exec -ti my_postgres_container pg_dump -Fc -U postgres > db.dump

Afterwards, I try to restore it like this:

$ docker cp db.dump my_postgres_container:/db.dump
$ docker exec -ti my_postgres_container pg_restore -U postgres -c -d postgres db.dump

The command returns without output or errors, but nothing happens.

So instead, I tried to restore it manually like this:

$ docker cp db.dump my_postgres_container:/db.dump
$ docker exec -ti my_postgres_container bash
root@fdaad610bee3:/# pg_restore -U postgres -c -d postgres db.dump
Segmentation fault (core dumped)

Why is pg_restore segfaulting when trying to read my DB dump?


回答1:


Analysis:

The problem is caused by a corruption when dumping the DB. pg_dump produces binary output. This output is first passed through the Docker container's stdout and then redirected into a file on the host. Somewhere on the way, non-ASCII bytes are corrupted.

Solution:

Let pg_dump write to a file inside the Docker container, then copy that out to the host:

correct dumping procedure:

docker exec -ti my_postgres_container bash -c 'pg_dump -Fc -U postgres > /db.dump'
docker cp my_postgres_container:/db.dump db.dump

correct restoring procedure:

docker cp db.dump my_postgres_container:/db.dump
docker exec -ti my_postgres_container pg_restore -U postgres -c -d postgres db.dump


来源:https://stackoverflow.com/questions/63934856/why-is-pg-restore-segfaulting-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!