码头工人| Postgres数据库未初始化,并且未指定超级用户密码

时间:2020-08-05 09:50:31

标签: postgresql docker docker-compose

我正在使用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

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

{
    "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"
    }
}

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

complex multi-container docker project structure

一年前,它实际上运行良好,是否有人知道我的docker-compose文件中现在出了什么问题?

2 个答案:

答案 0 :(得分:4)

一年前,它实际上运行良好,是否有人知道,我的docker-compose文件中现在出了什么问题?

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

enter image description here

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

答案 1 :(得分:1)

@Adiii是的,您几乎是正确的,因此我也必须明确提及postgres图像的环境,但没有db父标记。

因此,在这里,我明确提到docker-compose.yaml配置以帮助其他人更好地理解,而且现在我正在使用最新的稳定的postgres映像版本12-alpine,当前最新的是{{1 }}

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

希望这会帮助很多人。