为什么pg_restore segfaulting在Docker中? [英] Why is pg_restore segfaulting in Docker?
问题描述
我正在为docker容器中的postgres数据库测试备份/还原过程.
I am testing a backup/restore procedure for my postgres DB inside a docker container.
我这样转储数据库:
$ 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)
为什么在尝试读取数据库转储时pg_restore出现段错误?
Why is pg_restore segfaulting when trying to read my DB dump?
推荐答案
分析:
问题是由转储数据库时损坏引起的. pg_dump
产生二进制输出.此输出首先通过Docker容器的 stdout
传递,然后重定向到主机上的文件中.在途中的某个地方,非ASCII字节已损坏.
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.
解决方案:
让 pg_dump
写入Docker容器内的文件,然后将其复制到主机:
Let pg_dump
write to a file inside the Docker container, then copy that out to the host:
正确的转储程序:
docker exec -ti my_postgres_container bash -c 'pg_dump -Fc -U postgres > /db.dump'
docker cp my_postgres_container:/db.dump db.dump
正确的还原过程:
docker cp db.dump my_postgres_container:/db.dump
docker exec -ti my_postgres_container pg_restore -U postgres -c -d postgres db.dump
这篇关于为什么pg_restore segfaulting在Docker中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!