简体   繁体   English

无法连接到 Docker 托管的 Postgres

[英]Can't connect to Docker Hosted Postgres

My attempt to start a PostgreSQL Docker container fails when doing docker-compose.在执行 docker-compose 时,我尝试启动 PostgreSQL Docker 容器失败。 I can connect to it if I do docker run instead.如果我改为使用 docker run ,我可以连接到它。 I don't know what the difference is.我不知道有什么区别。 My intent with the compose is to have it create a database usable for development and that I can connect to with user postgres.我对 compose 的意图是让它创建一个可用于开发的数据库,并且我可以通过用户 postgres 连接到该数据库。

docker-compose.yml:码头工人-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:

Attempting to connect to jdbc:postgresql://localhost:5432/helloworld using user postgres and password postgres.尝试使用用户 postgres 和密码 postgres 连接到jdbc:postgresql://localhost:5432/helloworld Please pardon any security concerns you may perceive.请原谅您可能察觉到的任何安全问题。 This will be tightened for development, and done much more securely when deployed to production.这将在开发中得到加强,并且在部署到生产中时会更加安全。

The reported error as reported by SquirrelSQL is: SquirrelSQL 报告的错误是:

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

The image hashcode is: 9dbc24674f25eb449df11179ed3717c47348fb3aa985ae14b3936d54c2c09dde图像哈希码为: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"

I hope for a Docker based answer.我希望有一个基于 Docker 的答案。 If there's some PostgreSQL command I need to do (or not do), please tell me how I can pass that through Docker.如果有一些 PostgreSQL 命令我需要执行(或不执行),请告诉我如何通过 Docker 传递它。

Thanks, Woodsman谢谢,樵夫

Your main error is:您的主要错误是:

Role "postgres" does not exist.角色“postgres”不存在。

It seems because whitespace here in docker-compose.yml:似乎是因为 docker-compose.yml 中的空格:

  • POSTGRES_USER= postgres POSTGRES_USER=postgres

In first start postgres was initialized, so when You change "POSTGRES_USER" in variable, role will not create.在第一次启动 postgres 时已初始化,因此当您更改变量中的“POSTGRES_USER”时,不会创建角色。 You can delete you volume and start docker-compose again.您可以删除您的卷并再次启动 docker-compose。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM