6

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?

6
  • well, you can point a volume containing the backup at first run and then call pg_restore, but that doesn't have to go through docker way so you can do it remotely Commented Mar 24, 2017 at 17:24
  • cool, but as far as I understand, my problem is that I cannot get the unpacking of the compressed file right. Any ideas? Commented Mar 24, 2017 at 17:29
  • would gunzip < backup.tar.gz | pg_restore -F t .. help for your case? Commented Mar 24, 2017 at 17:43
  • I think we're very close but something is missing. the above command just prints out the (uncompressed) content of the file. Commented Mar 24, 2017 at 17:43
  • just to be more explicit gunzip < backup.tar.gz | pg_restore -U USER -F t DB. Other than the output, have you connect and check if any data has written? Commented Mar 24, 2017 at 17:47

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Lovely docker expertise!
@hurturk Oddly, I can get the backup the OP posted to work just fine: docker exec postgres-local-15 pg_dump -U postgres -F t dvdrental | gzip > /Users/me/Downloads/dvdrental.tar.gz But I get "zsh: command not found: pg_restore" when trying either of your methods to restore, like so: docker exec postgres-local-15 gunzip < /Users/me/Downloads/dvdrental.tar | pg_restore -U postgres -F t -d dvdrental -or- gunzip < /Users/me/Downloads/dvdrental.tar | pg_restore -U postgres -F t -d dvdrental -h 127.0.0.1 -p 5432 Could the "zsh" mean that it's looking on my local for pg_restore?
0

How take backup of the data which is existing in the running PostgreSQL container


  1. Create some folder in your root

    mkdir -p '/myfolder/bdbackup'
    
  2. download the postgres image which you are using and execute the following command

    docker run --name demo1 -e POSTGRES_PASSWORD=passowrd -v /myfolder/bdbackup:/var/lib/postgresql/data -d postgres 
    
    docker exec -it demo1 psql -U postgres
    
  3. Back up will be stored in the following folder /myfolder/bdbackup

  4. you can kill the container and stop the container any time but data will be stored in the host.
  5. and once again re-run the postgres the container with same command

    docker run --name demo2 -e POSTGRES_PASSWORD=passowrd -v /myfolder/bdbackup:/var/lib/postgresql/data -d postgres 
    
    docker exec -it demo1 psql -U postgres
    

    and execute following query select * from emp;

  6. you can see the data has restored...

1 Comment

How to do it with volume instead of mount?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.