Search code examples
postgresqldockerrestore

How to restore postgres within a docker?


I create backups like this: docker exec DOCKER pg_dump -U USER -F t DB | gzip > ./FILE.tar.gz

What's the best way to restore the database given that the database runs within a container?


Solution

  • For your case:

    docker exec -it <CONTAINER> gunzip < backup.tar.gz | pg_restore -U <USER> -F t -d <DB>
    

    Remote restore is also available if your container is public facing and remote connections are allowed in pg_hba.conf for postresql:

    gunzip < backup.tar.gz | pg_restore -U <USER> -F t -d <DB> -h <HOST_IP> -p 5432
    

    As a rule of thumb, it is good idea to document your backup and restore commands specific to the project.