postgres
Docker 镜像的文档 says以下是关于环境变量 POSTGRES_DB
的内容:
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
我发现这根本不是真的。例如,使用此配置:
version: '3.7'
services:
db:
image: postgres:11.3-alpine
restart: always
container_name: store
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
environment:
- POSTGRES_USER=custom
- POSTGRES_DB=customname
- POSTGRES_PASSWORD_FILE=/run/secrets/db_password
secrets:
- db_password
volumes:
postgres_data:
secrets:
db_password:
file: config/.secrets.db_password
默认数据库名为 postgres
,而不是我指定的 customname
:
$ docker exec -it store psql -U custom customname
psql: FATAL: database customname does not exist
$ docker exec -it store psql -U custom postgres
psql (11.3)
Type help for help.
postgres=# ^D
我是否漏掉了一些明显的东西?
最佳答案
像您所做的那样提供环境变量,应该在容器初始化时创建 customname
数据库。无需在 /docker-entrypoint-initdb.d/
的初始化脚本中创建用户名和数据库。
我会确保没有任何挂起的 postgres_data
卷。如果您之前在未指定环境变量的情况下启动了容器,则会为默认的 postgres
数据库创建卷。下次启动容器(指定 POSTGRES_DB
环境)时,将跳过数据库创建部分。
只是为了确保,删除任何创建的卷(名称应该类似于 *_postgres_data
)
docker volume ls
docker volume rm <volume_name>
参见 User and DB were not created from environment variable arguments以及。希望有帮助
关于postgresql - Postgres Docker 镜像未使用自定义名称创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657683/