8

I using docker postgres:9.4 image. I need to know how to backup and restore volume only container.

Created Volume container:

docker run -v /var/lib/postgresql/data --name dbdata postgres:9.4 /bin/true

Using the volume

docker run --name=postgres --volumes-from=dbdata -d -p 6432:5432 postgres:9.4

Backup Volume container

docker run --volumes-from dbdata -v $(pwd):/backup postgres:9.4 tar cvf /backup/backup.tar /var/lib/postgresql/data

Restore volume in new container

docker run --name=dbdata-new --volumes-from dbdata -v $(pwd):/backup ubuntu:14.04 /bin/sh -c 'cd /var/lib/postgresql/data && tar xvf /backup/backup.tar'

Use in the new volume in creating new postgres container:

docker run --name=postgres-new --volumes-from=dbdata-new -d -p 7532:5432 postgres:9.4

Issue: I get the below error in the logs when I run the new container.

initdb: directory "/var/lib/postgresql/data" exists but is not empty If you want to create a new database system, either remove or empty the directory "/var/lib/postgresql/data" or run initdb with an argument other than "/var/lib/postgresql/data"

Not sure what I am doing wrong. Can someone please point out where I am making mistake.

5
  • followed your steps and got no errors in the log. Commented Jul 30, 2015 at 10:26
  • Add database and tables and please try Commented Jul 30, 2015 at 10:44
  • worked fine with one table in one database, imported and available at postgres-new: Commented Jul 30, 2015 at 10:56
  • can you please check if the new postgres instance shows the persisted data Commented Jul 30, 2015 at 14:09
  • I'm experiencing the same problem, though I went about it differently. I run the PG container with a mounted volume for data, then later had to stop the container, rsync the data to another Docker host, and run a new container with the volume. Now it's freaking out. Pretty disturbing... Commented Nov 12, 2015 at 4:04

2 Answers 2

2

Could not easily reproduce the issue following the steps with very rudiment data (one record one table one new db):

psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -c "CREATE DATABASE ttt;"
psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -d ttt -c "CREATE table a(b int); insert into a(b) values(1);"
psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 6432 -d ttt -c "select * from a;"

when I start the postgres-new I get no exceptions in logs and the data seems to be there:

$ psql -U postgres -h $(boot2docker ip || echo 'localhost') -p 7532 -d ttt -c "select * from a;"
 b
---
 1
(1 row)
Sign up to request clarification or add additional context in comments.

Comments

2

data-new --volumes-from dbdata -v $(pwd):/backup ubuntu:14.04 /bin/sh -c 'cd /var/lib/postgresql/data && tar xvf /backup/backup.tar'

The bold text was the problem. By untaring the backup in the folder /var/lib/postgresql/data ,the tar command was creating /var/lib/postgresql/data inside the above folder.

Thank you for all the help MyKola.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.