首页 > 解决方案 > 在入口点 docker 的容器中恢复 pgadmin4 创建的数据库

问题描述

我从我的数据库备份pgadmins4并存储到我的 dockerfile 位置。

现在我通过我的 docker 文件创建一个自定义 docker 映像:

FROM mdillon/postgis:11-alpine
LABEL MAINTAINER groot
ENV LANG en_US.utf8
ENV DBNAME pglocations
ENV USERNAME postgres

COPY init.sh /docker-entrypoint-initdb.d/
COPY ./pglocations.sql .

我建立了一个图像,当我想创建容器时,我得到了这个错误:

server started

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh
CREATE DATABASE
2019-08-27 06:07:31.790 UTC [43] ERROR:  index "user_device_id" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT:  DROP INDEX public.user_device_id;

2019-08-27 06:07:31.790 UTC [43] ERROR:  index "uid" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT:  DROP INDEX public.uid;

2019-08-27 06:07:31.790 UTC [43] ERROR:  index "timestamp" does not exist
2019-08-27 06:07:31.790 UTC [43] STATEMENT:  DROP INDEX public."timestamp";

2019-08-27 06:07:31.791 UTC [43] ERROR:  index "temp_mileage_location_user_id_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT:  DROP INDEX public.temp_mileage_location_user_id_idx;

2019-08-27 06:07:31.791 UTC [43] ERROR:  index "temp_mileage_location_shape_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT:  DROP INDEX public.temp_mileage_location_shape_idx;

2019-08-27 06:07:31.791 UTC [43] ERROR:  index "temp_mileage_location_device_id_idx" does not exist
2019-08-27 06:07:31.791 UTC [43] STATEMENT:  DROP INDEX public.temp_mileage_location_device_id_idx; 
... 

在我的数据库中,我添加了postgisuuid.ossp扩展以及 2 个函数。

通过 Pgadmin4 我可以恢复这个数据库。

这是我的 init.sh

#!/bin/sh
set -e

psql -v ON_ERROR_STOP=1 --dbname template1 --username postgres <<-EOSQL
    CREATE DATABASE "$DBNAME"
    WITH OWNER = postgres
    ENCODING = 'UTF8'
    LC_COLLATE = 'English_United States.1252'
    TABLESPACE = pg_default
    CONNECTION LIMIT = -1
    TEMPLATE template0;
EOSQL

#start postgres database
pg_restore -d "$DBNAME" pglocations.sql -c -U "$USERNAME"

这是我的容器安装位置:

docker inspect -f '{{json .Mounts }}' pg-docker

[{"Type":"volume","Name":"a0334d571a61c0a17a23ca4be6a191143f39657967c52b3326abf8c011f54861","Source":"/var/lib/docker/volumes/a0334d571a61c0a17a23ca4be6a191143f39657967c52b3326abf8c011f54861/_data","Destination":"/var/lib/postgresql/data","Driver":"local","Mode":"","RW":true,"Propagation":""}]

标签: dockerdockerfilepgadmin-4

解决方案


首先按照这些步骤

/var/lib/postgresql/data第二:你不应该在你的阶段复制任何文件Build- 将 docker 文件更改为:

FROM mdillon/postgis:11-alpine

LABEL MAINTAINER groot
ENV LANG en_US.utf8
ENV DBNAME pglocations
ENV USERNAME postgres
COPY init.sh /docker-entrypoint-initdb.d/

然后在构建新图像container后开始您的:

docker run --name=pg-docker -p 5433:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=s123 -v  /path/to/pglocations.sql:/docker-entrypoint-initdb.d/pglocations.sql safapostgis:11.2

推荐阅读