使用PostgreSQL的CI/CD流程失败,显示"数据库未初始化且未指定超级用户密码"错误。

25

我正在使用Bitbucket管道与PosgreSQL进行CI/CD。根据这篇文档,PostgreSQL服务在bitbucket-pipelines.yml中被描述为:

definitions:
  services:
    postgres:
      image: postgres:9.6-alpine

直到现在它一直运作正常。但是我最新的所有管道都失败了,并出现以下错误:

   Error: Database is uninitialized and superuser password is not specified.
   You must specify POSTGRES_PASSWORD for the superuser. Use
   "-e POSTGRES_PASSWORD=password" to set it in "docker run".

   You may also use POSTGRES_HOST_AUTH_METHOD=trust to allow all connections
   without a password. This is *not* recommended. See PostgreSQL
   documentation about "trust":
   https://www.postgresql.org/docs/current/auth-trust.html

我该如何修复它?bitbucket-pipelines.yml文件中没有任何更改可能是这种错误出现的原因。

3个回答

25

看起来原因是Docker镜像的更新(github issue)。 最新版本不允许在没有密码的情况下从任何地方连接到数据库。 因此,您需要指定用户名/密码:

definitions:
  services:
    postgres:
      image: postgres:9.6-alpine
      environment:
         POSTGRES_DB: pipelines
         POSTGRES_USER: test_user
         POSTGRES_PASSWORD: test_user_password

或者,如果您仍然不想使用密码,您可以设置POSTGRES_HOST_AUTH_METHOD=trust环境变量:

definitions:
  services:
    postgres:
      image: postgres:9.6-alpine
      environment:
        POSTGRES_HOST_AUTH_METHOD: trust

你怎么打开这些设置? - Lukas Salich
@LukasSalich 它在 bitbucket-pipelines.yml 文件的内部。 - neverwalkaloner
最终,关于运行容器的坏命令出现了另一个问题,所以它与此无关。 - Lukas Salich

6
这是最近的一个变化,大约在一周前。避免这种情况的一种方法是将您的postgres版本硬编码为不是最新的版本,例如更改为postgres:9.5.18postgres:9.5.20-alpine
另一种方法是传递一个虚假密码:
services:
  db:
    image: postgres
    ports:
      - "8001:5432"
    environment:
      - POSTGRES_PASSWORD=fake_password

在这里查看讨论:https://github.com/docker-library/postgres/issues/681

2
如果您第一次通过Docker连接Django和PostgreSQL时遇到问题,请将POSTGRES_HOST_AUTH_METHOD: trust添加到您的docker-compose.yml文件中: db: image: postgres:11 environment: POSTGRES_HOST_AUTH_METHOD: trust 这解决了我的连接问题。
请注意,“这是不推荐的。请参阅有关“信任”的PostgreSQL文档:https://www.postgresql.org/docs/current/auth-trust.html

1
如果这是不推荐的,为什么要建议它?提醒不推荐是好的,但是提供为什么建议或提供解决方法也会很有帮助。这只是一个意见。 - Nikhileshwar
一个好的解决方法是为超级用户指定POSTGRES_PASSWORD。您可以使用“-e POSTGRES_PASSWORD=password”在“docker run”中设置它。 - Adrian
1
“not recommended”是针对生产环境而言的。当在Docker中并在本地机器上运行时,实际上几乎没有任何问题。事实上,这非常有意义! - Octopus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接