ojoaovitoraraujo
ojoaovitoraraujo

Reputation: 97

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 ?

Upvotes: 4

Views: 5844

Answers (1)

Oresztesz
Oresztesz

Reputation: 2430

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.

Upvotes: 4

Related Questions