【问题标题】:pg_restore in postgres docker containerpostgres docker 容器中的 pg_restore
【发布时间】:2017-01-09 02:13:51
【问题描述】:

我正在尝试使用 shellscript 中的 pg_restore 恢复 PostgreSQL docker 容器中的数据库,taht 将从 docker 文件中调用。我收到以下错误 "ERROR: canceling autovacuum task CONTEXT:自动分析表'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

日志:

    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

但是,如果我尝试从同一备份文件的主机中的命令提示符恢复数据库,它工作正常。

【问题讨论】:

  • 我认为您可能需要等待数据库完全初始化后再运行还原。

标签: postgresql docker dockerfile


【解决方案1】:

我认为无法在初始化阶段完成备份还原。启动您的容器,然后上传数据库。

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

【讨论】:

    【解决方案2】:

    这是一种从位于主机上的文件恢复的方法:

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

    【讨论】:

      【解决方案3】:

      这个来自pg_dump -Fc'pg_dump_Fc_file'自定义(压缩)数据库转储为我工作:

      docker exec -i container_name pg_restore -Fc -U admin_username -d database_name < pg_dump_Fc_file
      

      【讨论】:

        【解决方案4】:

        结合most votedHeroku's guide,我想出了这个:

        docker exec -i mohe-bc_db_1 pg_restore --verbose --clean --no-acl --no-owner -U postgres -d mohe-bc_development < ~/Downloads/de8dc786-b133-4ae2-a040-dcf34f12c3de
        

        mohe-bc_db_1docker psNAMES 列显示的 pg 容器名称

        postgres:pg 用户名

        mohe-bc_development: 数据库名称

        ~/Downloads/de8dc786-b133-4ae2-a040-dcf34f12c3de: pg db dump 的文件路径

        它有效:

        pg_restore: connecting to database for restore
        pg_restore: dropping CONSTRAINT webhooks webhooks_pkey
        pg_restore: dropping CONSTRAINT schema_migrations schema_migrations_pkey
        pg_restore: dropping CONSTRAINT ar_internal_metadata ar_internal_metadata_pkey
        pg_restore: dropping DEFAULT webhooks id
        pg_restore: dropping SEQUENCE webhooks_id_seq
        pg_restore: dropping TABLE webhooks
        pg_restore: dropping TABLE schema_migrations
        pg_restore: dropping TABLE ar_internal_metadata
        pg_restore: creating TABLE "public.ar_internal_metadata"
        pg_restore: creating TABLE "public.schema_migrations"
        pg_restore: creating TABLE "public.webhooks"
        pg_restore: creating SEQUENCE "public.webhooks_id_seq"
        pg_restore: creating SEQUENCE OWNED BY "public.webhooks_id_seq"
        pg_restore: creating DEFAULT "public.webhooks id"
        pg_restore: processing data for table "public.ar_internal_metadata"
        pg_restore: processing data for table "public.schema_migrations"
        pg_restore: processing data for table "public.webhooks"
        pg_restore: executing SEQUENCE SET webhooks_id_seq
        pg_restore: creating CONSTRAINT "public.ar_internal_metadata ar_internal_metadata_pkey"
        pg_restore: creating CONSTRAINT "public.schema_migrations schema_migrations_pkey"
        pg_restore: creating CONSTRAINT "public.webhooks webhooks_pkey"
        

        【讨论】:

          【解决方案5】:

          另一种变体,以防所有较短的都不起作用:

          docker exec -i container_name pg_restore -U db_user --verbose --clean --no-acl --no-owner -h localhost -d db_name < db_backup_file
          

          另外,请注意--format 选项。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-05-22
            • 1970-01-01
            • 2019-10-05
            • 2018-07-20
            • 1970-01-01
            • 2020-02-20
            • 2021-02-27
            相关资源
            最近更新 更多