admin管理员组

文章数量:1278822

Context: I'm deploying a localhost version of a website to do some troubleshooting. It's a Django website running Mezzanine (yes it's very old, hence the debugging).

The website has a Postgres database which has been dumped via:

sudo -u postgres pg_dump --role "postgres" --format custom --blobs 
     --encoding UTF8 --verbose --no-unlogged-table-data --file /tmp/backup.bak

I have been able to restore this database on my own computer using pg_restore, however when trying to compose docker containers, the database is created but my db_init service does not seem to restore the database.

Here is the .yml file I use:

version: '3'

services:
  db:
    image: postgres:13
    environment:
      POSTGRES_DB: pg_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "postgres"]
      interval: 10s
      retries: 5

  db_init:
    image: postgres:13
    container_name: pg_db_init
    command: ["sh", "-c", 
              "./wait-for-it.sh db:5432 -- pg_restore --clean --if-exists \
               --no-owner --no-privileges -h db -d pg_db -U postgres /backup.bak"] 
    environment:
      POSTGRES_DB: pg_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      PGPASSWORD: postgres
    volumes:
      - ./backup.bak:/backup.bak
      - ./wait-for-it.sh:/wait-for-it.sh
    depends_on:
      - db

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    working_dir: /var/www/website.au    
    depends_on:
      - db
    environment:
      - DJANGO_SETTINGS=project.settings
    user: ${UID}:${GID}
    volumes:
      - .:/var/www/website.au
    ports:
      - "8000:8000"
    tty: true

volumes:
  postgres_data:

docker-compose.yml file creates the service database, the service db_init then tries to restore a dumped pg file into the database (yes it appears all the paths are successful) - all instances run.

No errors can be seen in the db_init container.

Here's the tail of the db container log:

2025-02-25 17:22:30 2025-02-25 09:22:30.693 UTC [1] LOG:  database system is ready to accept connections
2025-02-25 17:22:30 2025-02-25 09:22:30.913 UTC [72] ERROR:  relation "shipping_shippingcountry" does not exist at character 47
2025-02-25 17:22:30 2025-02-25 09:22:30.913 UTC [72] STATEMENT:  SELECT "shipping_shippingcountry"."code" FROM "shipping_shippingcountry" WHERE "shipping_shippingcountry"."enabled" = false ORDER BY "shipping_shippingcountry"."code" ASC
2025-02-25 17:22:32 2025-02-25 09:22:32.238 UTC [73] ERROR:  relation "shipping_shippingcountry" does not exist at character 47
2025-02-25 17:22:32 2025-02-25 09:22:32.238 UTC [73] STATEMENT:  SELECT "shipping_shippingcountry"."code" FROM "shipping_shippingcountry" WHERE "shipping_shippingcountry"."enabled" = false ORDER BY "shipping_shippingcountry"."code" ASC

These missing relations occur because the database hasn't restored any tables at all, which I checked by using the container's terminal, connecting to the Postgres database and using the \dt command

本文标签: