我正在为PostgresSQL 9.4.5数据库构建一个Docker容器,我想在容器启动时创建表结构。在我的/docker-entrypoint-initdb.d/
目录中包含的脚本是这一系列命令:
#!/usr/bin/env bash
export PGUSER=postgres
echo "***CREATING DATABASE, USERS, AND ROLES***"
psql <<-EOSQL
CREATE USER vpager WITH PASSWORD '<pass>';
CREATE DATABASE vpager;
GRANT ALL PRIVILEGES ON DATABASE vpager TO vpager;
REVOKE ALL ON DATABASE vpager FROM PUBLIC;
CREATE ROLE standarduser;
CREATE USER vpageruser WITH PASSWORD '<pass>';
\connect vpager
CREATE SCHEMA ticketing;
GRANT ALL PRIVILEGES ON SCHEMA ticketing TO vpager;
GRANT CONNECT ON DATABASE vpager TO standarduser;
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA ticketing TO standarduser;
GRANT standarduser TO vpageruser;
EOSQL
echo "***CREATING TABLE STRUCTURES***"
psql -U vpager vpager < /docker-entrypoint-initdb.d/create-vpager-tables.sql
SQL脚本:
CREATE TABLE ticketing.merchant
(
merchant_id SERIAL PRIMARY KEY,
now_serving INT
);
CREATE TABLE ticketing.ticket
(
ticket_id SERIAL,
merchant_id INT REFERENCES ticketing.merchant(merchant_id),
create_ts TIMESTAMP,
PRIMARY KEY (ticket_id, merchant_id)
);
如您所见,我们的想法是创建一个非特权用户来进行CRUD操作。问题是当create-vpager-tables.sql
命令运行时,它无法看到postgres创建的模式:
ERROR: schema "ticketing" does not exist
STATEMENT: CREATE TABLE ticketing.merchant
(
merchant_id SERIAL PRIMARY KEY,
now_serving INT
);
ERROR: schema "ticketing" does not exist
ERROR: schema "ticketing" does not exist
然而,如果我在CREATE SCHEMA
的顶部添加create-vpager-tables.sql
语句,则表示它已存在:
ERROR: schema "ticketing" already exists
如何解决这些错误?我尝试在SQL文件的顶部设置我的搜索路径,但同样的事情发生了;它就像一个&#34;薛定谔&#34;架构。
答案 0 :(得分:0)
我明白了。我将create-vpager-tables.sql
放在/docker-entrypoint-initdb.d
目录中,显然图像也尝试在那里获取SQL文件。将它移动到/ tmp(并将模式上的USAGE
授予标准用户)修复了问题。