简体   繁体   中英

how to restore postgres database in docker when docker container not start?

I want to create a database in PostgreSQL and restore a backup in a docker container. I am able to create the database and run the docker container, and then run the pg_restore to restore the backup. My Dockerfile is:

FROM postgres:latest

ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD 123qwe 
ENV POSTGRES_DB docker_pg 

COPY createTable.sql /docker-entrypoint-initdb.d/

VOLUME /var/lib/postgresql/data

Then I run the command for restore the backup:

docker exec -i 0d96d6b59d74 pg_restore -U postgres -d docker_pg< backup_latest.sql

It is working fine.

But my requirement is when I run the command for create the docker container database creation and restore the backup both work done in same time, mean at the time of container creation.

How can I do this?

But my requirement is when I run the command for create the docker container database creation and restore the backup both work done in same time, mean at the time of container creation.

Both tasks can be performed by the Docker container all you need to place the restore script in the docker-entrypoint-initdb.d folder.

COPY createTable.sql /docker-entrypoint-initdb.d/a_createTable.sql
COPY backup_latest.sql /docker-entrypoint-initdb.d/

As I changed createTable.sql to a_createTable.sql , so it will first create Table and then it will restore the backup.

These initialization files will be executed in sorted name order as defined by the current locale

Initialization scripts

Or the other option is to create single SQL file and the order will be

ALL DDL 
# then 
ALL DML

so something like

COPY db.sql /docker-entrypoint-initdb.d/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM