Postgresql 如何在Docker中使用secrets通过命令行登录Postgres,在容器外部不使用'Docker exec-it<;图像>;psql-h localhost-U postgres`?

Postgresql 如何在Docker中使用secrets通过命令行登录Postgres,在容器外部不使用'Docker exec-it<;图像>;psql-h localhost-U postgres`?,postgresql,docker,docker-compose,psql,Postgresql,Docker,Docker Compose,Psql,我可以使用docker compose-f postgres.yaml build构建以下文件并打开它,我可以通过 docker exec-it psql-h localhost-U postgres 然后输入密码。但是,我需要能够通过localhost的命令行登录 如psql-h localhost-U postgres,然后输入密码。当我尝试这样做时,我得到: 致命:用户“postgres”的密码身份验证失败。密码与用户“postgres”不匹配。如果在撰写文件中硬编码密码,则可以通过loc

我可以使用
docker compose-f postgres.yaml build
构建以下文件并打开它,我可以通过

docker exec-it psql-h localhost-U postgres

然后输入密码。但是,我需要能够通过localhost的命令行登录

psql-h localhost-U postgres
,然后输入密码。当我尝试这样做时,我得到:

致命:用户“postgres”的密码身份验证失败。密码与用户“postgres”不匹配。
如果在撰写文件中硬编码密码,则可以通过localhost命令行登录

我的Docker撰写文件:

version: '3.1'

services:
  db:
    image: postgres:latest
    build:
      context: .
      dockerfile: Dockerfile.postgres
    restart: always
    secrets:
      - postgres_db_password
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD_FILE: ./run/secrets/postgres_db_password
    ports:
      - 5432:5432

secrets:
  postgre_db_password:
    file: .postgres_db_password.txt
Docker.postgres文件:
来自postgis/postgis:10-3.0-alpine

您应该检查您的机密中是否没有如本文所述的尾随字符(如行尾)。

就是这样!!!天啊,我在这上面花了好几个小时!!谢谢伟大的您可以验证我的答案:)