Search code examples
dockerdocker-composevolumes

Docker-compose postgres volume not persisting


Not sure why, my docker compose file is:

services:
  db:
    image: "postgres:latest"
    ports:
      - "5432:5432"
    volumes:
      - ods:/var/postgresql/data
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=ods

volumes:
  ods:

when I do docker volume ls I can see the line

local postgres_ods

has been created (not sure why it has added postgres_ to the front)

I will then create a table, save it, commit it, add some data to it.

do a docker compose down then a docker compose up -d

I find that the data and the table has gone missing.

inspecting postgres_ods gives me:

[
    {
        "CreatedAt": "2021-11-17T00:26:15Z",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "postgres",
            "com.docker.compose.version": "2.0.0",
            "com.docker.compose.volume": "ods"
        },
        "Mountpoint": "/var/lib/docker/volumes/postgres_ods/_data",
        "Name": "postgres_ods",
        "Options": null,
        "Scope": "local"
    }
]

what am I doing wrong?


Solution

  • Wrong folder mapped, that is why the data didn't stick around:

    version: "3.9"
    services:
      db:
        image: "postgres:latest"
        ports:
          - "5432:5432"
        volumes:
          - ods:/var/lib/postgresql/data
        environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_DB=ods
    
    volumes:
      ods: