根据these docs,我可以使用env var POSTGRES_DB指定postgres docker映像创建的数据库的名称.我已经在docker compose文件中设置了它,但它没有被创建.

以下是 compose 文件的相关部分:

pg:
    image: postgres:10
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: user-auth
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres

以下是日志(log):

Attaching to userauth_pg_1
pg_1   | 2018-02-05 18:05:54.803 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
pg_1   | 2018-02-05 18:05:54.803 UTC [1] LOG:  listening on IPv6 address "::", port 5432
pg_1   | 2018-02-05 18:05:54.806 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_1   | 2018-02-05 18:05:54.817 UTC [24] LOG:  database system was interrupted; last known up at 2018-02-05 18:03:26 UTC
pg_1   | 2018-02-05 18:05:54.942 UTC [24] LOG:  database system was not properly shut down; automatic recovery in progress
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  redo starts at 0/1633ED0
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  invalid record length at 0/1633F08: wanted 24, got 0
pg_1   | 2018-02-05 18:05:54.944 UTC [24] LOG:  redo done at 0/1633ED0
pg_1   | 2018-02-05 18:05:54.955 UTC [1] LOG:  database system is ready to accept connections
pg_1   | 2018-02-05 18:05:59.140 UTC [31] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:06:15.528 UTC [32] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:08:46.120 UTC [33] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:08:46.151 UTC [34] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:02.138 UTC [35] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:02.926 UTC [36] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.244 UTC [37] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.273 UTC [38] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.602 UTC [39] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:04.910 UTC [40] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.777 UTC [41] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.823 UTC [42] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:05.878 UTC [43] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:06.663 UTC [44] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:14:06.716 UTC [45] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:16:32.713 UTC [46] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:47:04.603 UTC [47] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 18:51:34.413 UTC [1] LOG:  received smart shutdown request
pg_1   | 2018-02-05 18:51:34.417 UTC [1] LOG:  worker process: logical replication launcher (PID 30) exited with exit code 1
pg_1   | 2018-02-05 18:51:34.419 UTC [25] LOG:  shutting down
pg_1   | 2018-02-05 18:51:34.434 UTC [1] LOG:  database system is shut down
pg_1   | 2018-02-05 19:08:42.934 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
pg_1   | 2018-02-05 19:08:42.934 UTC [1] LOG:  listening on IPv6 address "::", port 5432
pg_1   | 2018-02-05 19:08:42.937 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
pg_1   | 2018-02-05 19:08:42.951 UTC [25] LOG:  database system was shut down at 2018-02-05 18:51:34 UTC
pg_1   | 2018-02-05 19:08:42.956 UTC [1] LOG:  database system is ready to accept connections
pg_1   | 2018-02-05 19:09:04.316 UTC [32] FATAL:  database "user-auth" does not exist
pg_1   | 2018-02-05 19:09:18.081 UTC [33] FATAL:  database "user-auth" does not exist

推荐答案

您很可能在没有设置env变量的情况下启动了容器.

您需要删除该卷,然后重试.

学分:https://github.com/docker-library/postgres/issues/453#issuecomment-393939412

Postgresql相关问答推荐

Qt,PostgreSQL -从NUERIC列检索max int64_t/uint64_t值

将real[]数组作为完整的浮点值从postquist返回

org.postgresql. util.PSQLException:使用docker + docker—compose + kafka + springboot时try 连接失败

Psql:Windows 10上奇怪的错误输出编码

Postgres 使用不同元素数据类型的订单数据

IF 块中的 CREATE FUNCTION 语句抛出错误,同时运行它自己的作品

Upper() 不会大写重音字符

计算每行的多列中的非 NULL 元素

如何展平也在关系中使用的区分大小写的列

Docker - 判断 postgres 是否准备好

postgres 和 python

我如何在docker中备份数据库

SQLAlchemy 和多个进程的连接问题

如何为 postgres 连接设置 application_name?

如何使用 psql 执行 *.sql 文件

如何从 CSV 为 PostgreSQL 副本生成模式

postgres - 比较两个数组

将数据推送到 Heroku 时出错:time zone displacement out of range

安装了 Postgres.app 但它不起作用

使用 SQLAlchemy 进行 PostgreSQL ILIKE 查询