gpt4 book ai didi

postgresql - Postgres Docker 容器不运行创建 SQL 脚本

转载 作者:行者123 更新时间:2023-12-03 23:41:32 25 4
gpt4 key购买 nike

docker-compose.yml

version: '3'
services:
db:
image: "postgres"
build: .
ports:
- "5432:5432"
env_file:
- database.env
Dockerfile
FROM postgres:9.3
ADD CreateDB.sql /docker-entrypoint-initdb.d/
CreateDB.sql
CREATE TABLE data_actions (
username VARCHAR NOT NULL,
json_payload VARCHAR NOT NULL,
cache_id VARCHAR,
action_timestamp TIMESTAMP
)
database.env
DOCKER_USER=docker
DOCKER_DB=docker
DOCKER_PASSWORD=docker
这些是我用来创建这个容器的文件。我已验证 CreateDB.sql 脚本已复制到容器中并且位于正确的位置。当我构建并运行容器并检查 pgAdmin 时,docker 数据库中没有表。我觉得我在这里遗漏了一些明显的东西?
更新
这是启动时的日志->
LOG:  database system was interrupted; last known up at 2020-12-23 18:21:18 UTC
LOG: database system was not properly shut down; automatic recovery in progress
LOG: record with zero length at 0/17A7030
LOG: redo is not required
LOG: MultiXact member wraparound protections are now enabled
LOG: autovacuum launcher started
LOG: database system is ready to accept connections
LOG: received smart shutdown request
LOG: autovacuum launcher shutting down

最佳答案

问题是您错误地命名了环境变​​量。 postgres image期望环境名称以 POSTGRES_ 开头(因此, POSTGRES_DBPOSTGRES_USER 等)。
重命名后 database.env所以我有:

POSTGRES_USER=docker
POSTGRES_DB=docker
POSTGRES_PASSWORD=docker
我可以开始您的撰写堆栈,然后:
$ docker-compose exec db psql -U docker
psql (9.3.25)
Type "help" for help.

docker=# \c docker
You are now connected to database "docker" as user "docker".
docker=# \dt
List of relations
Schema | Name | Type | Owner
--------+--------------+-------+--------
public | data_actions | table | docker
(1 row)

docker=#
用你的原版 database.env , Postgres 会创建您的 data_actions默认表中的 postgres数据库。

关于postgresql - Postgres Docker 容器不运行创建 SQL 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65430153/

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