gpt4 book ai didi

postgresql - 运行 DockerFile 以使用 pg_restore 创建和恢复转储时出错

转载 作者:行者123 更新时间:2023-12-02 18:17:46 31 4
gpt4 key购买 nike

我的 init.sql 文件是:

CREATE USER postgres WITH PASSWORD '123qwe';
CREATE DATABASE gmta_database ;
GRANT ALL PRIVILEGES ON DATABASE gmta_database TO postgres;
DockerFile 是:
FROM postgres:latest
COPY init.sql /docker-entrypoint-initdb.d/

ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD 123qwe
ENV POSTGRES_DB docker_pg

EXPOSE 5432
COPY test_latest.sql /

VOLUME /var/lib/postgresql/data

RUN pg_restore -U postgres -d docker_pg < test_latest.sql
当我使用命令运行时 docker build -t gmta-test-vol:1.0.0 . ,我收到这样的错误:
    Sending build context to Docker daemon  358.4kB
Step 1/7 : FROM postgres:latest
---> b97bae343e06
Step 2/7 : COPY init.sql /docker-entrypoint-initdb.d/
---> Using cache
---> 6f275b44db01
Step 3/7 : ENV POSTGRES_USER postgres
---> Using cache
---> 039924093b36
Step 4/7 : ENV POSTGRES_PASSWORD 123qwe
---> Using cache
---> 5e636686a2f7
Step 5/7 : ENV POSTGRES_DB docker_pg
---> Using cache
---> 9c0a773c138c
Step 6/7 : COPY gtma_latest.sql /
---> Using cache
---> 8dd99f79b403
Step 7/7 : RUN pg_restore -U postgres -d docker_pg < gtma_latest.sql
---> Running in 1e0a85650eb1
pg_restore: error: connection to database "docker_pg" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
我想创建数据库并使用单个 docker 命令恢复转储数据。
  • 我该如何解决这个问题?
  • 是否可以在不创建 docker 容器的情况下运行 pg_store 命令?

  • 请帮忙。提前致谢。

    最佳答案

    您不应该在构建时导入数据,因为数据库服务器还没有准备好,这也不会为后续层持久导入。
    所有你需要添加这个

    COPY test_latest.sql /docker-entrypoint-initdb.d/

    If you would like to do additional initialization in an image derived from this one, add one or more *.sql, *.sql.gz, or *.sh scripts under /docker-entrypoint-initdb.d (creating the directory if necessary). After the entrypoint calls initdb to create the default postgres user and database, it will run any *.sql files, run any executable *.sh scripts, and source any non-executable *.sh scripts found in that directory to do further initialization before starting the service.


    postgres init

    How can I solved the problem?


    只需放置 sql文件到 /docker-entrypoint-initdb.d/ Postgres 容器会处理它。

    Is it possible RUN pg_store command without create the docker container?


    不,在构建时放置副本,它会在容器启动时创建一个数据库。

    关于postgresql - 运行 DockerFile 以使用 pg_restore 创建和恢复转储时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62832829/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com