繁体   English   中英

docker-entrypoint-initdb.d 中的 Docker PostgreSQL 初始化脚本无法导入文件

[英]Docker PostgreSQL initialisation script laced in docker-entrypoint-initdb.d’ fails to import file

我正在尝试在 Docker 容器中设置 PostgreSQL。 我需要创建两个用户和多个数据库。

目录结构

pgdb/
   Dockerfile
   sql/
      ext_cfuncs/
      sprocs/
      init.sql
      db_init_data_globals.sql
      ...

Dockerfile 内容

FROM library/postgres:9.6
COPY sql /docker-entrypoint-initdb.d/

初始化程序

-- ##############################################
-- #                                            #
-- #  Create users                              #
-- #                                            #
-- ##############################################

-- Create superuser
-- need to be a superuser n order to create functions in C etc
CREATE ROLE dbuser1 ENCRYPTED PASSWORD '<redacted>' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

-- Create dbuser2
CREATE ROLE dbuser2 ENCRYPTED PASSWORD '<redacted';
CREATE DATABASE refglobals WITH ENCODING 'UTF8' TEMPLATE template0;
GRANT ALL PRIVILEGES ON DATABASE refglobals TO dbuser2;
GRANT ALL PRIVILEGES ON DATABASE refglobals TO dbuser1;

-- Import refglobals and news initial data
\c refglobals;
\i db_init_data_globals.sql;


# Create database to be used as template for other databases
CREATE DATABASE foobar WITH ENCODING 'UTF8' TEMPLATE template0;
\c foobar;

CREATE LANGUAGE 'plpgsql';
CREATE EXTENSION quantile;

-- # enable python in database
-- # http://www.vertabelo.com/blog/technical-articles/playing-around-with-python-in-postgresql
-- # /github.com/ihuston/plpython_examples/blob/master/simple_examples.sql
CREATE PROCEDURAL LANGUAGE 'plpythonu' HANDLER plpython_call_handler;
UPDATE pg_language SET lanpltrusted = true WHERE lanname LIKE 'plpythonu';

\i db_schema_foobar.sql;
\i ext_cfuncs/funcs_scanners_internal.sql;

我能够成功构建图像。 当我运行 docker run -it ` 时,这是控制台输出的一个片段:

****************************************************
WARNING: No password has been set for the database.
         This will allow anyone with access to the
         Postgres port to access your database. In
         Docker's default configuration, this is
         effectively any other container on the same
         system.

         Use "-e POSTGRES_PASSWORD=password" to set
         it in "docker run".
****************************************************
waiting for server to start....LOG:  could not bind IPv6 socket: Cannot assign requested address
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  database system was shut down at 2017-09-27 20:37:08 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
 done
server started
ALTER ROLE

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/create_databases.sql
CREATE ROLE
CREATE ROLE
CREATE DATABASE
GRANT
GRANT
You are now connected to database "refglobals" as user "postgres".
psql:/docker-entrypoint-initdb.d/init.sql:19: db_init_data_globals.sql: No such file or directory

为什么找不到文件db_init_data_globals.sql 它与 init.sql 位于同一文件夹中!

另外,为什么每次run命令时都会创建数据库、表等? 我认为初始化是在构建容器的过程中完成的?

我怀疑您的错误是由\\i命令的行为引起的:

\\i 或 \\include 文件名

从文件 filename 读取输入并执行它,就像它是在键盘上键入的一样。

library/postgres:9.6 docker-entrypoint.sh file可以看出, .sql文件是使用psql -f (作为脚本)执行的。 每个包含的文件都应该在 psql 的当前工作目录中

你应该使用\\ir

\\ir 或 \\include_relative 文件名

\\ir命令类似于\\i ,但解析相对文件名的方式不同。 在交互模式下执行时,这两个命令的行为相同。 但是,当从脚本调用时, \\ir解释相对于脚本所在目录的文件名,而不是当前工作目录

来源: Postgres 文档

如果您需要使其与旧版本的 Postgres 一起使用, 请查看此答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM