首页 > 解决方案 > 用户未在 postgres docker 映像中创建

问题描述

我有一个 docker 映像,它不接受 yaml docker-compose 文件中定义的用户的凭据。当我转到容器的 docker 控制台并检查用户时,它只列出postgres. 不知道我错过了什么 - 这是 yaml 文件:

version: '3.8'

services:
  db:
    container_name: drewreport_container
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: mpassword
      POSTGRES_USER: thedrewreport
      POSTGRES_DB: thedrewreportdb
    ports:
      - "5432:5432"
    volumes:
      - thedrewreportdata:/var/lib/postgresql/data/
volumes:
  thedrewreportdata:

有任何想法吗?

标签: postgresqldockerdocker-compose

解决方案


我无法重现您的问题。运行docker-compose up,我看到:

Creating network "docker_default" with the default driver
Creating volume "docker_thedrewreportdata" with default driver
Creating docker_client_1 ...
Creating docker_db_1     ...
Creating docker_client_1 ... done
Creating docker_db_1     ... done
Attaching to docker_db_1, docker_client_1
[...]
db_1      | 2021-07-19 23:03:39.676 UTC [1] LOG:  database system is ready to accept connections

如果我随后连接psql,我可以使用您在 中定义的用户名和密码进行身份验证docker-compose.yml

# psql -h localhost -U thedrewreport  thedrewreportdb
Password for user thedrewreport:
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.

thedrewreportdb=# \du
                                     List of roles
   Role name   |                         Attributes                         | Member of
---------------+------------------------------------------------------------+-----------
 thedrewreport | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

thedrewreportdb=#

请注意,您指定的任何卷docker-compose.yml都将在 adocker-compose down和 a之间持续存在docker-compose up,因此,如果您使用不同的凭据建立堆栈,除非您通过运行显式销毁卷,否则这些将永远不会被替换docker-compose down -v

如果您在运行时没有看到这样的消息,您可以判断这docker-compose是在重新使用卷docker-compose up

Creating volume "docker_thedrewreportdata" with default driver

推荐阅读