在Postgres Docker容器中使用pg_restore

31
我尝试使用来自shell脚本的pg_restore在PostgreSQL Docker容器中还原数据库。我遇到了以下错误:“ERROR:取消自动真空任务 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

但是,如果我尝试从主机命令提示符中使用相同的备份文件还原数据库,它可以正常工作。


我认为在运行还原之前,您可能需要等待数据库完全初始化。 - dnephin
7个回答

51

以下是从位于主机上的文件进行恢复的方法:

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

我们不能从主机访问容器网络吗?无法使用pg_restore --host=db...。 - sajid
“pg_restore --host=container_address ..” 这个怎么样? 我们不能从主机访问容器网络吗?看起来ev1只是为了得到赞而复制粘贴了相同的答案。 - sajid

8

结合最受欢迎的和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 ps命令的NAMES列中显示的pg容器名称

postgres:pg用户名

mohe-bc_development:db名称

~/Downloads/de8dc786-b133-4ae2-a040-dcf34f12c3de:pg数据库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"

7

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

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

2
(只针对Windows用户,在PowerShell中不支持<)。在PowerShell下:
Get-Content C:\pathToDumpFolder\Mydump.sql | docker exec -i containername psql -U username -v -d dbname 

0

以下是我使用 pg_dump -Fc 命令生成的自定义(压缩)数据库转储文件:

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

0
你可以在你的dockerfile中使用docker入口点。
RUN echo "pg_restore -U postgres [FILE_LOCATION_HERE]" > /docker-entrypoint-initdb.d/init.sh

工作得很顺利。

-1

如果所有较短的变体都不起作用,可以尝试另一种变体:

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 选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接