无法连接到 Docker 托管的 Postgres

Can't connect to Docker Hosted Postgres

我在执行 docker-compose 时尝试启动 PostgreSQL Docker 容器失败。如果我改为 docker 运行 ,我可以连接到它。我不知道有什么区别。我对 compose 的意图是让它创建一个可用于开发的数据库,并且我可以与用户 postgres 连接。

docker-compose.yml:

version: '3.7'
services:
  my_app:
    build:
      dockerfile: ./dockerfile-my-app
      context: .
    command:  tail -f /dev/null
    depends_on:
      - db
    env_file: ./.env.development
    ports:
      - "8080:8080"
    volumes:
      - /c/Users/woodsman/linux-mint-woodsman/home/dev:/home/dev

  db:
    image: postgres:latest
    env_file: ./.env.development
    environment:
      - POSTGRES_USER= postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=helloworld
      - PGDATA=/var/lib/postgresql/data/helloworld2/


    ports:
      - "5432:5432"
    restart: "no"
    volumes:
      - /c/Users/woodsman/linux-mint-woodsman/dbdata:/var/lib/postgresql/data/pgdata


volumes:
  dbdata:

正在尝试使用用户 postgres 和密码 postgres 连接到 jdbc:postgresql://localhost:5432/helloworld。请原谅您可能察觉到的任何安全问题。这将在开发中得到加强,并在部署到生产中时更加安全地完成。

SquirrelSQL 报告的错误是:

hello-world: FATAL: password authentication failed for user "postgres"
class org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"

图像哈希码是: 9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde

>docker logs c4c2
2022-05-16 05:28:50.560 UTC [1] LOG:  starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
2022-05-16 05:28:50.560 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-05-16 05:28:50.560 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2022-05-16 05:28:50.564 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2022-05-16 05:28:50.571 UTC [63] LOG:  database system was shut down at 2022-05-16 05:28:50 UTC
2022-05-16 05:28:50.579 UTC [1] LOG:  database system is ready to accept connections
2022-05-16 05:29:17.184 UTC [70] FATAL:  password authentication failed for user "postgres"
2022-05-16 05:29:17.184 UTC [70] DETAIL:  Role "postgres" does not exist.
        Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"
2022-05-16 05:36:41.983 UTC [78] FATAL:  password authentication failed for user "postgres"
2022-05-16 05:36:41.983 UTC [78] DETAIL:  Role "postgres" does not exist.
        Connection matched pg_hba.conf line 100: "host all all all scram-sha-256"

我希望得到一个基于 Docker 的答案。如果我需要执行(或不执行)某些 PostgreSQL 命令,请告诉我如何通过 Docker.

传递它

谢谢, 樵夫

你的主要错误是:

Role "postgres" does not exist.

似乎是因为 docker-compose.yml:

中有空格
  • POSTGRES_USER= postgres

在第一次启动时postgres被初始化,所以当你改变变量中的“POSTGRES_USER”时,角色不会创建。您可以删除卷并重新开始 docker-compose。