'How to restore postgreSQL in docker-compose

I have the following docker-compose part

postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_PASSWORD: "postgres"
    ports:
      - "15432:5432"
    volumes:
      - /root/database:/var/lib/postgresql/data
    networks:
      - production-network
    restart: unless-stopped
    depends_on:
      - rest-proxy
      - broker

What should I do to run a .sql file and restore the db as soon as I run docker-compose ?



Solution 1:[1]

Based on the docker image documentation you can include initialization scripts if you mount under /docker-entrypoint-initdb.d. It should work with both *.sql, *.sql.gz, or *.sh. So in your example, the compose file should look something like this:

postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_PASSWORD: "postgres"
    ports:
      - "15432:5432"
    volumes:
      - /root/database:/var/lib/postgresql/data
      - /root/database-init/init-user-db.sql:/docker-entrypoint-initdb.d/init-user-db.sql:ro
    networks:
      - production-network
    restart: unless-stopped
    depends_on:
      - rest-proxy
      - broker

Assuming that init-user-db.sql file contains your initialization scripts.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oresztesz