Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 330
kampta Avatar asked Oct 01 '16 15:10

kampta


People also ask

Does Docker restart lose data?

Yes, once your container ceases to exist or you restart it, the data will be lost. But that can be easily fixed if you use persistent volumes to store your data. My preferred solution is to use a container orchestrator like Swarm, Kubernetes, etc. I'll expand on volumes in Kubernetes a little bit.

Where is Postgres data stored Docker?

To circumvent this issue, we can use the information we gathered earlier that showed us that the volume is mounted at /var/lib/postgresql/data. Inside the container, this directory is where Postgres stores all the relevant tables and databases.

How do I Dockerize an existing Postgres database?

Install PostgreSQL on DockerStart by creating a new Dockerfile : Note: This PostgreSQL setup is for development-only purposes. Refer to the PostgreSQL documentation to fine-tune these settings so that it is suitably secure. Build an image from the Dockerfile and assign it a name.


1 Answers

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 :)

like image 55
siddhartha kasaraneni Avatar answered Oct 28 '22 10:10

siddhartha kasaraneni