pg_restore in postgres docker container

19,927

Solution 1

Here is a way to restore from a file located on the host machine:

docker exec -i container_name pg_restore -U postgres_user -v -d database_name < /dir_backup_outside_container/file_name.tar

Solution 2

I don't think the backup restore can be done during the initialization phase. Start your container and then upload the db.

docker run -d --name mydb mypgimage
docker exec mydb sh -c "pg_restore -C -d DB /var/lib/postgresql/backup/DB.backup"
Share:
19,927

Related videos on Youtube

Vaibhav Kumar
Author by

Vaibhav Kumar

Updated on June 15, 2022

Comments

  • Vaibhav Kumar
    Vaibhav Kumar almost 2 years

    I am trying to restore database in PostgreSQL docker container using pg_restore from a shellscript taht will be called from docker file .I am getting following error "ERROR: canceling autovacuum task CONTEXT: automatic analyze of table 'tablename'".

    DockerFile:

        FROM postgres:9.3
        ENV POSTGRES_USER postgres
        ENV POSTGRES_PASSWORD Abcd1234
        ENV POSTGRES_DB Clarion1
        COPY DB.backup /var/lib/postgresql/backup/DB.backup
        COPY initialize.sh /docker-entrypoint-initdb.d/initialize.sh
    

    initialize.sh

        #!/bin/bash
        set -e
        set -x
    
        echo "******PostgreSQL initialisation******"
        pg_restore -C -d DB /var/lib/postgresql/backup/DB.backup
    

    Log:

        server started
        CREATE DATABASE
        /docker-entrypoint.sh: running /docker-entrypoint-initdb.d/initialize.sh
        ++ echo '******PostgreSQL initialisation******'
        ++ pg_restore -C -d Clarion1 /var/lib/postgresql/backup/Clarion53.backup
        ******PostgreSQL initialisation******
        ERROR:  canceling autovacuum task
    

    But if I try to restore DB from command prompt in host machine from same backup file , it is working fine.