【问题标题】:Docker | Postgres Database is uninitialized and superuser password is not specified码头工人 | Postgres 数据库未初始化且未指定超级用户密码
【发布时间】:2020-08-05 09:50:31
【问题描述】:

我正在使用 docker-compose.yml 创建多个正在运行的容器,但无法启动 Postgres docker 服务器,并带有以下日志,是的,我搜索了许多相关的 SO 帖子,但它们没有帮助我。

Creating network "complex_default" with the default driver
Creating complex_server_1   ... done
Creating complex_redis_1    ... done
Creating complex_postgres_1 ... done
Attaching to complex_postgres_1, complex_redis_1, complex_server_1
postgres_1  | Error: Database is uninitialized and superuser password is not specified.
postgres_1  |        You must specify POSTGRES_PASSWORD to a non-empty value for the
postgres_1  |        superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
postgres_1  |
postgres_1  |        You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
postgres_1  |        connections without a password. This is *not* recommended.
postgres_1  |
postgres_1  |        See PostgreSQL documentation about "trust":
postgres_1  |        https://www.postgresql.org/docs/current/auth-trust.html
complex_postgres_1 exited with code 1

下面是我的 docker-compose 配置:

version: '3'
services:
    postgres:
        image: 'postgres:11-alpine'
    redis:
        image: 'redis:latest'
    server:
        build:
            dockerfile: Dockerfile.dev
            context: ./server
        volumes:
            - /app/node_modules
            - ./server:/app
        environment:
            - REDIS_HOST=redis
            - REDIS_PORT=6379
            - PGUSER=postgres
            - PGHOST=postgres
            - PGDATABASE=postgres
            - PGPASSWORD=postgres_password
            - PGPORT=5432

以及server 目录内的package.json 如下:

{
    "dependencies": {
        "body-parser": "^1.19.0",
        "cors": "^2.8.4",
        "express": "^4.16.3",
        "nodemon": "^2.0.4",
        "pg": "7.4.3",
        "redis": "^2.8.0"
    },
    "scripts": {
        "dev": "nodemon",
        "start": "node index.js"
    }
}

为了更好地考虑,我附上了我的动手项目结构:

一年前它实际上工作正常,有谁知道,现在我的 docker-compose 文件中出了什么问题。

【问题讨论】:

    标签: postgresql docker docker-compose


    【解决方案1】:

    一年前它实际上工作正常,有谁知道,现在我的 docker-compose 文件中出了什么问题。

    好像您提取了新图像,在新图像中您应该指定 Postgres 用户密码。您可以查看 Dockerhub,图像是 更新 一个月前

    postgress-11-alpine

      db:
        image: postgres:11-alpine
        restart: always
        environment:
          POSTGRES_PASSWORD: example
    

    因为错误信息是不言自明的

    You must specify POSTGRES_PASSWORD to a non-empty value for the
            superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
    

    POSTGRES_HOST_AUTH_METHOD=trust不推荐使用这个。

    You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
     connections without a password. This is *not* recommended.
    

    POSTGRES_PASSWORD

    此环境变量是必需的,您才能使用 PostgreSQL 映像。 不能为空或未定义。此环境变量设置 PostgreSQL 的超级用户密码。默认超级用户由POSTGRES_USER 环境变量定义。

    Environment Variables

    【讨论】:

    • 这有点糟糕,它从一开始就破坏了我项目的开发环境。 为什么它被改变是有原因的吗?
    【解决方案2】:

    @Adiii 是的,你几乎是对的,所以我必须明确提到 postgres 图像的环境,但没有 db 父标记。

    所以在这里,我明确提到了docker-compose.yaml 配置以帮助其他人更好地理解,现在我使用的是最近稳定的postgres 映像版本12-alpine,目前最新的是postgres:12.3

    version: '3'
    services:
        postgres:
            image: 'postgres:12-alpine'
            environment:
                POSTGRES_PASSWORD: postgres_password
        redis:
            image: 'redis:latest'
        server:
            build:
                dockerfile: Dockerfile.dev
                context: ./server
            volumes:
                - /app/node_modules
                - ./server:/app
            environment:
                - REDIS_HOST=redis
                - REDIS_PORT=6379
                - PGUSER=postgres
                - PGHOST=postgres
                - PGDATABASE=postgres
                - PGPASSWORD=postgres_password
                - PGPORT=5432
    

    所以docker-compose up之后的创建和运行日志如下:

    PS E:\docker\complex> docker-compose up
    Creating network "complex_default" with the default driver
    Creating complex_postgres_1 ... done
    Creating complex_redis_1    ... done
    Creating complex_server_1   ... done
    Attaching to complex_redis_1, complex_postgres_1, complex_server_1
    postgres_1  | The files belonging to this database system will be owned by user "postgres".
    postgres_1  | This user must also own the server process.
    postgres_1  |
    postgres_1  | The database cluster will be initialized with locale "en_US.utf8".
    postgres_1  | The default database encoding has accordingly been set to "UTF8".
    postgres_1  | The default text search configuration will be set to "english".
    postgres_1  |
    postgres_1  | Data page checksums are disabled.
    postgres_1  |
    postgres_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
    postgres_1  | creating subdirectories ... ok
    postgres_1  | selecting dynamic shared memory implementation ... posix
    postgres_1  | selecting default max_connections ... 100
    postgres_1  | selecting default shared_buffers ... 128MB
    postgres_1  | selecting default time zone ... UTC
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Redis version=6.0.6, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1     | 1:C 05 Aug 2020 14:24:48.692 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    postgres_1  | creating configuration files ... ok
    redis_1     | 1:M 05 Aug 2020 14:24:48.693 * Running mode=standalone, port=6379.
    redis_1     | 1:M 05 Aug 2020 14:24:48.693 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 # Server initialized
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis_1     | 1:M 05 Aug 2020 14:24:48.694 * Ready to accept connections
    postgres_1  | running bootstrap script ... ok
    server_1    |
    server_1    | > @ dev /app
    server_1    | > nodemon
    server_1    |
    postgres_1  | performing post-bootstrap initialization ... sh: locale: not found
    postgres_1  | 2020-08-05 14:24:50.153 UTC [29] WARNING:  no usable system locales were found
    server_1    | [nodemon] 2.0.4
    server_1    | [nodemon] to restart at any time, enter `rs`
    server_1    | [nodemon] watching path(s): *.*
    server_1    | [nodemon] watching extensions: js,mjs,json
    server_1    | [nodemon] starting `node index.js`
    postgres_1  | ok
    server_1    | Listening
    postgres_1  | syncing data to disk ... ok
    postgres_1  |
    postgres_1  |
    postgres_1  | Success. You can now start the database server using:
    postgres_1  |
    postgres_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
    postgres_1  |
    postgres_1  | initdb: warning: enabling "trust" authentication for local connections
    postgres_1  | You can change this by editing pg_hba.conf or using the option -A, or
    postgres_1  | --auth-local and --auth-host, the next time you run initdb.
    postgres_1  | waiting for server to start....2020-08-05 14:24:51.634 UTC [34] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
    postgres_1  | 2020-08-05 14:24:51.700 UTC [34] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    postgres_1  | 2020-08-05 14:24:51.981 UTC [35] LOG:  database system was shut down at 2020-08-05 14:24:50 UTC
    postgres_1  | 2020-08-05 14:24:52.040 UTC [34] LOG:  database system is ready to accept connections
    postgres_1  |  done
    postgres_1  | server started
    postgres_1  |
    postgres_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
    postgres_1  |
    postgres_1  | waiting for server to shut down....2020-08-05 14:24:52.121 UTC [34] LOG:  received fast shutdown request
    
    postgres_1  | 2020-08-05 14:24:52.186 UTC [34] LOG:  aborting any active transactions
    postgres_1  | 2020-08-05 14:24:52.188 UTC [34] LOG:  background worker "logical replication launcher" (PID 41) exited with exit code 1
    postgres_1  | 2020-08-05 14:24:52.188 UTC [36] LOG:  shutting down
    postgres_1  | 2020-08-05 14:24:52.669 UTC [34] LOG:  database system is shut down
    postgres_1  |  done
    postgres_1  | server stopped
    postgres_1  |
    postgres_1  | PostgreSQL init process complete; ready for start up.
    postgres_1  |
    postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.3.0) 9.3.0, 64-bit
    postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
    postgres_1  | 2020-08-05 14:24:52.832 UTC [1] LOG:  listening on IPv6 address "::", port 5432
    postgres_1  | 2020-08-05 14:24:52.954 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
    postgres_1  | 2020-08-05 14:24:53.136 UTC [43] LOG:  database system was shut down at 2020-08-05 14:24:52 UTC
    postgres_1  | 2020-08-05 14:24:53.194 UTC [1] LOG:  database system is ready to accept connections
    

    希望这会对很多人有所帮助。

    【讨论】:

    • 我现在面临同样的问题。但是如果我运行 docker run posgres:9.6 (它来自教程),我找不到图像的 docker 文件。它让我疯狂。如果找不到 dockerfile,如何设置密码?
    • @Čamo 你找到 docker 文件了吗?
    • 不记得了。那是几个月前的事了。
    【解决方案3】:

    加上 ArifMustafa 的回答,这对我有用。

      postgres:
        image:  'postgres:12-alpine'
        environment:
            POSTGRES_PASSWORD: mypassword
        expose:
          - 5432
        volumes: 
          - postgres_data:/var/lib/postgres/data/
    
    
    volumes:
       postgres_data:
    

    【讨论】:

      【解决方案4】:

      在我的情况下,项目级别 settings.py 文件中的 POSTGRES_PASSWORD(在 docker-compose.yml 中)和 PASSWORD 的数据库设置存在错误。

      在 docker-compose.yml 中提供密码后

        db:   # For the PostgreSQL database
          image: postgres:11
          environment:
            - POSTGRES_PASSWORD=example
      

      必须提供相同的密码才能访问两个容器之间的 POSTGRES 数据库(在我的例子中是 Web 应用程序和依赖于 Web 应用程序的数据库)。在project_level settings.py中:

      # Database
      # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
      
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.postgresql',
              'NAME': 'postgres',
              'USER': 'postgres',
              'PASSWORD': 'example',
              'HOST': 'db',
              'PORT': 5432
          }
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-19
        • 2020-06-01
        • 2021-12-04
        • 1970-01-01
        • 2015-08-10
        • 2018-06-23
        • 2019-01-01
        • 2014-12-25
        相关资源
        最近更新 更多