PostgreSQL 默认超级用户不存在。

huangapple go评论67阅读模式
英文:

Postgres default superuser doesn't exist

问题

所以,我一直在尝试通过进入 Docker 镜像的 shell 并输入 psql -U postgres 来访问我的 PostgreSQL 数据库,但它返回了一个错误 psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist。以前可以正常工作,但突然间不工作了...
如果有助于更好地理解问题,我可以提供 Docker Compose 文件。

我可能做错了什么?

感谢您的时间。

编辑 1:docker-compose.yml

version: "3"
services:
    app:
        build: .
        ports:
            - "80:80"
        networks:
            - app-net
        depends_on:
            db:
                condition: service_healthy
    db:
        image: "postgres:14"
        hostname: "postgres"
        user: "postgres"
        container_name: postgres
        environment:
            POSTGRES_DB: dei_acolhimento_v1
            POSTGRES_USER: dei_acolhimento_root
            POSTGRES_PASSWORD: 6h7jf8kg3d
            PGPASSWORD: 6h7jf8kg3d
        healthcheck:
            test: ["CMD-SHELL", "pg_isready"]
            interval: 30s
            timeout: 5s
            retries: 5
        ports:
            - "5432:5432"
        volumes:
            - pgdata:/var/lib/postgresql/data
            - ./init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
        networks:
            - app-net
volumes:
    pgdata:
networks:
    app-net:
英文:

so I've been trying to enter my Postgres database in a Docker image by going to the image's shell and typing psql -U postgres but it returns an error psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "postgres" does not exist. It used to work, and out of nowhere it stopped working...
I'm free to provide the docker-compose file if it helps you better understand the problem.

What could I have done wrong?

Thanks alot for your time.

Edit 1: docker-compose.yml

version: "3"
services:
    app:
        build: .
        ports:
            - "80:80"
        networks:
            - app-net
        depends_on:
            db:
                condition: service_healthy
    db:
        image: "postgres:14"
        hostname: "postgres"
        user: "postgres"
        container_name: postgres
        environment:
            POSTGRES_DB: dei_acolhimento_v1
            POSTGRES_USER: dei_acolhimento_root
            POSTGRES_PASSWORD: 6h7jf8kg3d
            PGPASSWORD: 6h7jf8kg3d
        healthcheck:
            test: ["CMD-SHELL", "pg_isready"]
            interval: 30s
            timeout: 5s
            retries: 5
        ports:
            - "5432:5432"
        volumes:
            - pgdata:/var/lib/postgresql/data
            - ./init-database.sh:/docker-entrypoint-initdb.d/init-database.sh
        networks:
            - app-net
volumes:
    pgdata:
networks:
    app-net:

答案1

得分: 1

您的设置设置了POSTGRES_USER环境变量,因此不使用默认的postgres用户,而是在您的情况下使用dei_acolhimento_root用户。请参阅文档以扩展/修改Postgres Docker容器。

请注意,数据库的名称与用户的名称再次不同,因为POSTGRES_DB被设置为dei_acolhimento_v1

英文:

Your setup sets the POSTGRES_USER environment variable and therefore does not use the default postgres user but in your case dei_acolhimento_root. See the documentation for extending/modifying the Postgres docker container.

Note that the name of the database again differs from the name of the user because POSTGRES_DB is set to dei_acolhimento_v1.

huangapple
  • 本文由 发表于 2023年7月11日 05:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定