将docker postgres卷挂载到ubuntu现有的postgres数据

时间:2019-11-27 16:26:20

标签: postgresql docker docker-compose

我的ubuntu 18.04已在本地安装了postgresql。我想将现有数据挂载到Docker。

以下是我的docker-compose.yml

version: '3'
services:
  db:
    image: postgres:10-alpine
    restart: always
    environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD: postgres
    ports:
      - "15432:5432"
    volumes:
      - /etc/postgresql/10/main:/var/lib/postgresql/data

我没有在Ubuntu中更改默认的postgres 10配置。默认情况下,我认为配置位于/etc/postgresql/10/main中,数据位于/var/lib/postgresql/10/main中。

如果我设置

volumes:
   - /var/lib/postgresql/10/main:/var/lib/postgresql/data

它警告

db_1     | postgres: could not access the server configuration file "/var/lib/postgresql/data/postgresql.conf": No such file or directory

如果我更改为

volumes:
      - /etc/postgresql/10/main:/var/lib/postgresql/data

它警告

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

如何解决而不用docker volume create pgdata创建新的本地docker vol

1 个答案:

答案 0 :(得分:0)

您应该可以将两者都指定到同一目录中

volumes:
    - "/var/lib/postgresql/10/main:/var/lib/postgresql/data"
    - "/etc/postgresql/10/main/postgresql.conf:/var/lib/postgresql/data/postgresql.conf"