25

I'm using Bitbucket pipeline with PosgreSQL for CI/CD. According to this documentation PostgreSQL service has been described in bitbucket-pipelines.yml this way:

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

It worked just fine until now. But all my latest pipelines failed with following error:

   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

How can I fix it? There was no changes in bitbucket-pipelines.yml file which could be the reason of such error..

neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100

3 Answers3

25

Looks like the reason is docker image's updates (github issue). Latest versions do not allow to connect to DB without a password from anywhere. So you need to specify username/password:

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

Or if you still don't want to use password, you can just set POSTGRES_HOST_AUTH_METHOD=trust environment variable:

definitions:
  services:
    postgres:
      image: postgres:9.6-alpine
      environment:
        POSTGRES_HOST_AUTH_METHOD: trust
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
6

This is a very recent change, as of about a week ago. One way to avoid it is to hardcode your postgres version to not the latest, eg changing to postgres:9.5.18 or postgres:9.5.20-alpine

Another way is to pass a fake password:

services:
  db:
    image: postgres
    ports:
      - "8001:5432"
    environment:
      - POSTGRES_PASSWORD=fake_password

See the discussion here: https://github.com/docker-library/postgres/issues/681

ehacinom
  • 8,070
  • 7
  • 43
  • 65
2

If you are having issues connecting Django to PostgreSQL via Docker for the first time, then add the POSTGRES_HOST_AUTH_METHOD: trust to your docker-compose.yml file:

db: image: postgres:11 environment: POSTGRES_HOST_AUTH_METHOD: trust

This solve the connection issue for me.

Please also be aware that "This is not recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html"

Adrian
  • 177
  • 1
  • 5
  • 1
    if this is NOT RECOMMENDED, why suggest it? Alerting that it is not recommended is good, but also providing why you suggest it or providing a work around over it. Will definitely help. Just an Opinion. – Nikhileshwar Feb 18 '20 at 07:32
  • A good work around this is to specify a POSTGRES_PASSWORD for the superuser. You can use "-e POSTGRES_PASSWORD=password" to set it in "docker run" – Adrian Feb 18 '20 at 07:56
  • 1
    "not recommended" is for a production environment. When inside a docker and running on your local machine there is practically no concern. In fact, it makes a lot of sense! – Octopus Sep 07 '22 at 23:35