How to use a PostgreSQL container with existing data?

前端 未结 2 1132
不思量自难忘°
不思量自难忘° 2021-01-29 22:21

I am trying to set up a PostgreSQL container (https://hub.docker.com/_/postgres/). I have some data from a current PostgreSQL instance. I copied it from /var/lib/postgresq

2条回答
  •  自闭症患者
    2021-01-29 22:57

    It looks like the PostgreSQL image is having issues with mounted volumes. FWIW, it is probably more of a PostgreSQL issue than Dockers, but that doesn't matter because mounting disks is not a recommended way for persisting database files, anyway.

    You should be creating data-only Docker containers, instead. Like this:

    postgres9:
      image: postgres:9.4
      ports:
        - 5432:5432
      volumes_from:
        - pg_data
      environment:
        POSTGRES_PASSWORD: postgres
        POSTGRES_USER: postgres
        PGDATA : /var/lib/postgresql/data/pgdata
    
    pg_data:
      image: alpine:latest
      volumes:
        - /var/lib/postgresql/data/pgdata
      command: "true"
    

    which I tested and worked fine. You can read more about data-only containers here: Why Docker Data Containers (Volumes!) are Good

    As for: how to import initial data, you can either:

    1. docker cp, into the data-only container of the setup, or
    2. Use an SQL dump of the data, instead of moving binary files around (which is what I would do).

提交回复
热议问题