简体   繁体   English

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

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

I am trying to set up PostgreSQL in a Docker container.我正在尝试在 Docker 容器中设置 PostgreSQL。 I need to create two users and multiple databases.我需要创建两个用户和多个数据库。

Directory structure目录结构

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

Dockerfile contents Dockerfile 内容

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

init.sql初始化程序

-- ##############################################
-- #                                            #
-- #  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;

I am able to successfully build the image.我能够成功构建图像。 When I run docker run -it `, here is a snippet of the console output:当我运行 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

Why is the file db_init_data_globals.sql not found?为什么找不到文件db_init_data_globals.sql It's in the same folder as init.sql!它与 init.sql 位于同一文件夹中!

Also, why are the databases, tables etc being created every time I run the command?另外,为什么每次run命令时都会创建数据库、表等? I thought the initialisation was done during the building of the container?我认为初始化是在构建容器的过程中完成的?

I suspect that your error is caused by the behavior of the \\i command:我怀疑您的错误是由\\i命令的行为引起的:

\\i or \\include filename \\i 或 \\include 文件名

Reads input from the file filename and executes it as though it had been typed on the keyboard.从文件 filename 读取输入并执行它,就像它是在键盘上键入的一样。

As you can see from library/postgres:9.6 docker-entrypoint.sh file , .sql files are executed using psql -f (as scripts).library/postgres:9.6 docker-entrypoint.sh file可以看出, .sql文件是使用psql -f (作为脚本)执行的。 Every included file is expected to be in psql's current working directory .每个包含的文件都应该在 psql 的当前工作目录中

You should use \\ir :你应该使用\\ir

\\ir or \\include_relative filename \\ir 或 \\include_relative 文件名

The \\ir command is similar to \\i , but resolves relative file names differently. \\ir命令类似于\\i ,但解析相对文件名的方式不同。 When executing in interactive mode, the two commands behave identically.在交互模式下执行时,这两个命令的行为相同。 However, when invoked from a script, \\ir interprets file names relative to the directory in which the script is located, rather than the current working directory .但是,当从脚本调用时, \\ir解释相对于脚本所在目录的文件名,而不是当前工作目录

Source: Postgres doc .来源: Postgres 文档

If you need to make this working with older versions of Postgres, check this answer .如果您需要使其与旧版本的 Postgres 一起使用, 请查看此答案

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

相关问题 使用 docker-entrypoint-initdb.d 脚本初始化 PostgreSQL 容器 - Initialize PostgreSQL Container with docker-entrypoint-initdb.d script 如何解决空docker-entrypoint-initdb.d的问题? (PostgresQL + Docker) - How to solve problem with empty docker-entrypoint-initdb.d? (PostgresQL + Docker) Docker PostgreSQL - /docker-entrypoint-initdb.d中的脚本无法运行 - Docker PostgreSQL - Scripts in /docker-entrypoint-initdb.d doesn't run 无法启动 PostgreSQL Docker 容器 – “'/docker-entrypoint-initdb.d/': 不允许操作” - Cannot start PostgreSQL Docker container – "'/docker-entrypoint-initdb.d/': Operation not permitted" 初始化postgres数据库(不使用/docker-entrypoint-initdb.d) - Initialize postgres database (without using /docker-entrypoint-initdb.d) docker-entrypoint-initdb.d 错误解释器:权限被拒绝 - docker-entrypoint-initdb.d bad interpreter: Permission denied /docker-entrypoint-initdb.d 文件夹中的脚本被忽略 - Scripts in the /docker-entrypoint-initdb.d folder are ignored 在 docker-entrypoint-initdb.d 中创建 pg_cron 扩展失败 - creating pg_cron extension within docker-entrypoint-initdb.d fails Docker postgres 不在 docker-entrypoint-initdb.d 中运行 init 文件 - Docker postgres does not run init file in docker-entrypoint-initdb.d flyway 无法连接到 docker-entrypoint-initdb.d 脚本中的 postgres 容器 - flyway unable to connect to postgres container within docker-entrypoint-initdb.d script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM