簡體   English   中英

postgres docker 容器中的 pg_restore

[英]pg_restore in postgres docker container

我正在嘗試使用 pg_restore 從將從 docker 文件調用的 shellscript 中恢復 PostgreSQL docker 容器中的數據庫。 我收到以下錯誤“錯誤:取消自動清理任務上下文:自動分析表'tablename'”

Docker文件:

    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

初始化.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

但是,如果我嘗試從同一備份文件中的主機中的命令提示符恢復數據庫,它工作正常。

這是從位於主機上的文件中恢復的一種方法:

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

我不認為可以在初始化階段完成備份還原。 啟動您的容器,然后上傳數據庫。

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

這個從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

結合投票最多Heroku 的指南,我想出了這個:

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_1 : docker docker ps NAMES 列顯示的 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"

另一種變體,以防所有較短的變體都不起作用:

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選項。

(僅針對 Windows 用戶添加,< 不受 powerShell 支持)。 在 powershell 下:

Get-Content C:\pathToDumpFolder\Mydump.sql | docker exec -i containername psql -U username -v -d dbname 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM