Postgres mounting docker volume to local

Viewed 28

I want to connect not only the schema in my local postgres to docker volume but also the data inside it.

This is my docker-compose.yml:

version: '3.8'
services:
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 5000:5000
    volumes:
      - .:/app
    links:
      - db:db
    depends_on:
      - pgadmin
      - db

  db:
    image: postgres:14.5
    restart: always
    volumes:
      - .dbdata:/var/lib/postgresql/data
    hostname: postgres
    environment:
      POSTGRES_PASSWORD: [my password]
      POSTGRES_DB: employee-manager-db

  pgadmin:
    image: 'dpage/pgadmin4'
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: [my email]
      PGADMIN_DEFAULT_PASSWORD: [my password]
    ports:
      - "5050:80"
    depends_on:
      - db

When I change the db volumes from ".dbdata:/var/lib/postgresql/data" to ".dbdata:/var/lib/postgresql", it runs without error and copies my schema, but it does not have any data in the tables. However, when I have ".dbdata:/var/lib/postgresql/data" as db volumes and do "docker-compose up", I get the following error:

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

I am not trying to make a new database system. Can someone help me with this? Thank you so much

0 Answers
Related