简体   繁体   中英

How to dump and restore correctly a postgresql db from docker

I stuck with this error when trying to backup and restore my database from a docker django app environnement: error

I first did this command to backup my whole DB

docker exec -t project_final-db-1 pg_dumpall -c -U fred2020 > ./db/dump.sql

And then trying to restory with this command

cat dump.sql | docker exec -i --user fred2020 catsitting-db-1 psql -U fred2020 -d postgres 

I have two containers, one for my django app named catsitting-web-1 and one for my postgresql named catsitting-db-1

I don't understand why it gaves me that error, my db user is the same that I specified on the dockerfile...

Any clue? For purpose help, here is my docker files configuration:

Dockerfile

FROM python:3.9
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN pip install Pillow
COPY . /code/

docker-compose.yml

version: "3.9"
   
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=fred2020
      - POSTGRES_PASSWORD=p*******DD
    expose:
      - "5432"
    ports:
      - 5432:5432
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

requirements.txt

Django>=3.0,<4.0
psycopg2-binary>=2.8
Pillow==8.1.0

And that's my process to migrate from laptop1 to laptop2:

Installation Run a command line go into a root directory and run: git clone https://github.com/XXXXXXXXXXXXXXXX

In the command line go into the root directory: cd catsitting

In the same command line window, run: docker-compose build --no-cache

In the command line window you need first to migrate the database for Django, run: docker-compose run web python manage.py migrate

In the command line window then you need to apply the migrations, run: docker-compose run web python manage.py makemigrations

In the command line window then you need to import database, run: cat dump.sql | docker exec -i --user fred2020 catsitting-db-1 psql -U fred2020 -d postgres cat dump.sql | docker exec -i --user fred2020 catsitting-db-1 psql -U fred2020 -d postgres (for dumping my DB I used docker exec -t project_final-db-1 pg_dumpall -c -U fred2020 >./db/dump.sql )

You can now run: docker-compose up

Is there something I get wrong?

For purpose help, here is my docker files configuration:

Dockerfile

FROM python:3.9
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN pip install Pillow
COPY . /code/

docker-compose.yml

version: "3.9"
   
services:
  db:
    image: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=fred2020
      - POSTGRES_PASSWORD=p*******DD
    expose:
      - "5432"
    ports:
      - 5432:5432
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

requirements.txt

Django>=3.0,<4.0
psycopg2-binary>=2.8
Pillow==8.1.0

And that's my process to migrate from laptop1 to laptop2:

Installation Run a command line go into a root directory and run: git clone https://github.com/XXXXXXXXXXXXXXXX

In the command line go into the root directory: cd catsitting

In the same command line window, run: docker-compose build --no-cache

In the command line window you need first to migrate the database for Django, run: docker-compose run web python manage.py migrate

In the command line window then you need to apply the migrations, run: docker-compose run web python manage.py makemigrations

In the command line window then you need to import database, run: cat dump.sql | docker exec -i --user fred2020 catsitting-db-1 psql -U fred2020 -d postgres cat dump.sql | docker exec -i --user fred2020 catsitting-db-1 psql -U fred2020 -d postgres (for dumping my DB I used docker exec -t project_final-db-1 pg_dumpall -c -U fred2020 >./db/dump.sql )

You can now run: docker-compose up

Is there something I get wrong?

I solved. It was a problem in misconfiguration in the pg_hba.conf inside my docker postgresql I changed the value from scram-sha-256 to md5 and it works now I can display my webapp with the current db !!

Do you know how to specifie md5 when I build my docker environnement? by default it puts scram-sha-256

Do you know why when I restore my dump in the new environnement by default in the container the pg_hba.conf set the authentification methode to scram-sha-256 and to do my connection working I need to edit that file and to put the authentification method set to md5?

# TYPE  DATABASE        USER        ADDRESS     METHOD
local   all             all                     md5

Ok sorry folks I found the solution. I've put that line in my docker-compose.yml:

environment:
- POSTGRES_HOST_AUTH_METHOD=trust

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM