docker-compose postgres利用時にエラー「Error: Database is uninitialized and superuser password is not specified.」が発生した場合の対処法

docker-compose postgres利用時にエラー「Error: Database is uninitialized and superuser password is not specified.」が発生した場合の対処法

docker composeでpostgresを利用時にエラー「Error: Database is uninitialized and superuser password is not specified.」が発生した場合の対処法を記述してます。

環境

  • OS CentOS Stream release 8
  • docker 19.03.13
  • docker-compose 1.25.4

エラー全文

以下のエラーが発生

Error: Database is uninitialized and superuser password is not specified.
       You must specify POSTGRES_PASSWORD to a non-empty value for the
       superuser. For example, "-e POSTGRES_PASSWORD=password" on "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

原因

以下のように「docker-compose.yml」にsuperuserのパスワードが設定されていないことが原因

version: '3.6'
services:
  postgres:
    image: postgres
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data

対処法

パスワードを設定する

version: '3.6'
services:
  postgres:
    image: postgres
    restart: always
    volumes:
    - db_data:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgrespassword