Docker-Compose + postgres:数据库和用户创建不起作用(无需撰写即可工作)

时间:2018-12-17 17:42:20

标签: postgresql docker docker-compose alpine

docs for the postgres Docker image解释说,您可以使用环境变量使映像创建用户并在创建数据库时创建数据库。

我似乎无法使用docker-compose来完成这项工作:

# docker-compose.yml
services:
  postgresql:
    image: postgres:alpine
    environment:
      POSTGRES_DB: iotplatform
      POSTGRES_USER: iotplatform
      POSTGRES_PASSWORD: iotplatform

这就是我的运行方式:

docker-compose up -d --force-recreate postgresql
docker-compose exec postgresql psql -U iotplatform
# psql: FATAL:  role "iotplatform" does not exist

运行docker-compose exec postgresql env时,我看到环境变量已配置。

日志中没有特别说明:

Attaching to iot-container-tracker_postgresql_1
postgresql_1  | 2018-12-17 17:35:05.754 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgresql_1  | 2018-12-17 17:35:05.754 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgresql_1  | 2018-12-17 17:35:05.757 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgresql_1  | 2018-12-17 17:35:05.770 UTC [21] LOG:  database system was shut down at 2018-12-17 17:35:03 UTC
postgresql_1  | 2018-12-17 17:35:05.772 UTC [1] LOG:  database system is ready to accept connections
postgresql_1  | 2018-12-17 17:35:22.639 UTC [34] FATAL:  role "iotplatform" does not exist

编辑:尝试不使用docker-compose,并且可以正常工作。

docker run --name some-postgres -e POSTGRES_PASSWORD=iotplatform -e POSTGRES_DB=iotplatform -e POSTGRES_USER=iotplatform -d postgres:alpine
docker exec -it some-postgres psql -U iotplatform
iotplatform=#

我想念什么?

1 个答案:

答案 0 :(得分:0)

运行downup后,它可以工作。显然,--force-recreate重置起来并不足够。

感谢@StéphaneJeandeaux的帮助。