连接到PostgreSQL:Postico和TablePlus DB GUI无法连接,但是Docker中的psql有效(致命:用户的密码验证失败)

时间:2019-03-18 16:05:51

标签: postgresql docker docker-compose postico

我已经在Docker本地部署了PostgreSQL数据库。这是我的Docker Compose文件:

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.30-alpha
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        # uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security
        # managementApiSecret: my-secret
        prototype: true
        databases:
          default:
            connector: postgres
            host: postgres
            user: prisma
            password: prisma
            port: 5432
  postgres:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
  postgres:

我从docker-compose up -d开始。

Docker容器正在运行:

CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                    NAMES
26b14120f89f        prismagraphql/prisma:1.30-alpha   "/bin/sh -c /app/sta…"   17 minutes ago      Up 17 minutes       0.0.0.0:4466->4466/tcp   newdm1_prisma_1
05dfdaeaf609        postgres                          "docker-entrypoint.s…"   17 minutes ago      Up 17 minutes       5432/tcp                 newdm1_postgres_1

现在,由于某种原因我无法使用数据库GUI连接(尝试过Postico和TablePlus)。在两个客户端中,我都收到以下错误:

  

FATAL:用户“ prisma”的密码身份验证失败

我100%确保提供了prisma作为Docker Compose文件中指定的密码。

此外,当我从psql Docker容器内部使用postgres连接到数据库时,它确实起作用:

docker exec -it 05dfdaeaf609 bash

然后在Docker容器中执行以下操作:

root@05dfdaeaf609:/# psql -U prisma -W prisma
Password: 
psql (11.1 (Debian 11.1-1.pgdg90+1))
Type "help" for help.

prisma=# \l
                              List of databases
   Name    | Owner  | Encoding |  Collate   |   Ctype    | Access privileges 
-----------+--------+----------+------------+------------+-------------------
 postgres  | prisma | UTF8     | en_US.utf8 | en_US.utf8 | 
 prisma    | prisma | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0 | prisma | UTF8     | en_US.utf8 | en_US.utf8 | =c/prisma        +
           |        |          |            |            | prisma=CTc/prisma
 template1 | prisma | UTF8     | en_US.utf8 | en_US.utf8 | =c/prisma        +
           |        |          |            |            | prisma=CTc/prisma
(4 rows)

prisma=# \c prisma
Password for user prisma: 
You are now connected to database "prisma" as user "prisma".

我在psql中提供的密码是prisma,类似于我在Postico和TablePlus中提供的密码。

在Docker容器中连接到PostgreSQL数据库时,我需要做些特别的事情吗?

1 个答案:

答案 0 :(得分:1)

PORTS
0.0.0.0:4466->4466/tcp
5432/tcp

如果检查docker ps命令的ports列,您将意识到Postgres端口没有公开供主机使用。

要解决此问题,您需要将以下内容添加到docker-compose.yml文件中:

ports:
  - "5432:5432"

使完整文件看起来像这样:

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.30-alpha
    restart: always
    ports:
    - "4466:4466"
    environment:
      PRISMA_CONFIG: |
        port: 4466
        # uncomment the next line and provide the env var PRISMA_MANAGEMENT_API_SECRET=my-secret to activate cluster security
        # managementApiSecret: my-secret
        prototype: true
        databases:
          default:
            connector: postgres
            host: postgres
            user: prisma
            password: prisma
            port: 5432
  postgres:
    image: postgres
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
  postgres:

,然后您需要运行docker-compose up -d来应用新的更改。正确设置后,PORTS的{​​{1}}列应如下所示

docker ps