kampta
kampta

Reputation: 4898

data lost while restarting postgres with docker

I was running a postgres instance with following docker-compose.yml-

postgres:
  restart: always
  image: postgres:latest
  volumes:
    - /data:/var/lib/postgresql
  ports:
    - "5432:5432"
  environment:
    - POSTGRES_USER=admin
    - POSTGRES_PASSWORD=123456
    - POSTGRES_DB=mydb
    - PGDATA=/var/lib/postgresql/data

I had added some tables with few rows. I made a small change in the file, adding container_name: postgres, and restarted with docker-compose up -d

and now when i login to the database, I don't see any tables/data.

psql -h ###### -p 5432 -d mydb -U admin --password

The data directory is added as volumes and is intact.

Is it something to do with initializing postgres?

UPDATE 2016/10/02

Please note that /data is an external hard drive mounted on host docker-machine.

UPDATE 2016/10/02

Also I noticed that when I change the rename the location of docker-compose.yml (from /home/ubuntu/dev/docker-storage/docker-compose.yml to /home/ubuntu/dev/storage/docker-compose.yml), and do a docker-compose up -d, I get an error saying

ERROR: for postgres  Conflict. The name "/postgres" is already in use by container 6de8378a8156ec368748194f8912836a7b5e3212fbb69627093d0f6114b82f0d. 
You have to remove (or rename) that container to be able to reuse that name.

Is that where problem is coming from? I might have removed the original container.

UPDATE 2016/10/06

This seems to be working now. For some weird reason, I had to mount both /var/lib/postgresql and /var/lib/postgresql/data

postgres:
    restart: always
    image: postgres:latest
    container_name: postgres
    volumes:
      - /data/postgresql:/var/lib/postgresql
      - /data/postgresql/data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=123456
      - POSTGRES_DB=mydb

Upvotes: 9

Views: 19440

Answers (4)

siddhartha kasaraneni
siddhartha kasaraneni

Reputation: 688

What you did is a hack! The official way to do it, is to have a environment variable named PGDATA, same as the container volume path

here you go:

postgres:
    restart: always
    image: postgres:latest
    container_name: postgres
    volumes:
      - ${PATH_TO_STORAGE}:/var/lib/postgresql/data/:rw <--- check this out
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      PGDATA: /var/lib/postgresql/data/ <--- Check this out

Hope this helps :)

Upvotes: 17

deinspanjer
deinspanjer

Reputation: 515

The error about an existing /postgres container means you didn't successfully shut everything down before trying to start the new one with the same name.

It is likely what might have happened is that you started two instances of postgres both pointed at the same data directory. That could possibly cause data corruption.

Specifying a container name is a good way to prevent that from happening because as you saw, Docker won't let you start a second instance with the same name.

Upvotes: 1

deinspanjer
deinspanjer

Reputation: 515

I noticed your host volume directory name is data, but you are mounting it to postgresql rather than postgresql/data.

I am not sure if that could be causing this problem, but it seems like a good thing to clean up.

Upvotes: 1

RichArt
RichArt

Reputation: 1672

up -d doesn't restart. I think you just started a new instance. You may use restart.

docker-compose restart ...

Upvotes: 3

Related Questions