Cannot connect with psql to dockerized postgres

佐手、 提交于 2021-02-10 15:13:32

问题


I'm having a problem connecting to a dockerized postgres

This is the container:

root@29de79c3cfa2:/# psql -U postgres -W
Password for user postgres: 
psql (9.5.14)
Type "help" for help.

postgres=# 

From Mac terminal:

psql --host localhos -p 5234 -U postgres -W
Password for user postgres: 
psql: FATAL:  role "postgres" does not exist

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
29de79c3cfa2        postgres:9.5        "docker-entrypoint.s…"   14 minutes ago      Up 7 minutes        0.0.0.0:5234->5432/tcp   postgres_1

It seems ports match (I do get the connection on 5234), but for some reason it does not recognize the role? How can this be possible if ssh'ing into the container allows me to connect to with that role?


回答1:


It is working in my case. This is what I did

  1. Run postgres docker image

    [root@yellowdog ]# docker run -d --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres:9.5 5bf259c6d3f8be43aa3dc2aed4496a4992a8d1ba5b999507652fd13fcc109c25

  2. Test the working of postgres from docker container from a different postgresql container

    [root@yellowdog ]# docker run -it --rm --link postgres:postgres postgres psql -h postgres -U postgres

    Password for user postgres:

    psql (11.0 (Debian 11.0-1.pgdg90+2), server 9.5.14)

    Type "help" for help.

    postgres=# select 1;

    ?column?

        1
    

    (1 row)

    postgres=# \q

  3. Finally test the working from host machine

    [root@yellowdog ]# psql -h localhost -U postgres

    Password for user postgres:

    psql (10.4, server 9.5.14)

    Type "help" for help.

    postgres=# select 1;

    ?column?

        1
    

    (1 row)

    postgres=# \q



来源:https://stackoverflow.com/questions/52911026/cannot-connect-with-psql-to-dockerized-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!