Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 612
ojoaovitoraraujo Avatar asked Sep 01 '25 01:09

ojoaovitoraraujo


1 Answers

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.

like image 110
Oresztesz Avatar answered Sep 02 '25 13:09

Oresztesz