Load Postgres dump after docker-compose up

后端 未结 5 1838
广开言路
广开言路 2021-02-02 12:42

I have a dump.sql file that I would like to load with docker-compose.

docker-compose.yml:

services:
  postgres:
    environment:
      POSTGRES_DB: my_d         


        
相关标签:
5条回答
  • 2021-02-02 12:57

    You can use pg_restore as well ... for example:

    cat {BACKUP.SQL.File} | docker exec -i {working_container_name} pg_restore \
    --verbose --clean --no-acl --no-owner -U {USER} -d {YOURDATABASE}
    
    0 讨论(0)
  • 2021-02-02 13:02
    CONTAINER_NAME="postgres"
    DB_USER=postgres
    LOCAL_DUMP_PATH="..."
    docker run --name "${CONTAINER_NAME}" postgres
    docker exec -i "${CONTAINER_NAME}" psql -U "${DB_USER}" < "${LOCAL_DUMP_PATH}"
    
    0 讨论(0)
  • 2021-02-02 13:02

    After the docker-compose up, do docker ps it will give you a list of active docker containers. From that, you can get the container ID.

    Then,

    docker exec -i {CONTAINER_ID} psql -U {USER} < {.SQL FILE}

    0 讨论(0)
  • 2021-02-02 13:06

    Reading https://hub.docker.com/_/postgres/, the section 'Extend this image' explains that any .sql in /docker-entrypoint-initdb.d will be executed after build.

    I just needed to change my Dockerfile.db to:

    FROM postgres
    
    ADD ./devops/db/dummy_dump.sql /docker-entrypoint-initdb.d
    

    And it works!

    0 讨论(0)
  • 2021-02-02 13:07
    sudo docker exec postgres psql -U postgres my_db_name < dump.sql
    
    0 讨论(0)
提交回复
热议问题