Search code examples
postgresqldockerbackupdatabase-backups

Why is pg_restore segfaulting in Docker?


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?


Solution

  • TLDR: -t should not be used unnecessarily.

    I think for your pg_dump, the -t is corrupting the data written to db.dump. For that matter, the -i is also redundant since pg_dump does not need to read from stdin.

    For your pg_restore, you need neither option. If you redirect stdin from outside the container, then you need -i.

    I had the same problem and solved it by using neither -i nor -t for the pg_dump.

    I think the following fixed pg_dump should work for your case:

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

    Incidentally for the pg_restore, you don't need to copy the dump file to the container. You can just redirect stdin from the dump file on the host:

    $ docker exec -i my_postgres_container pg_restore -Fc -c -U postgres -d postgres < db.dump