簡體   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