从主机连接到 docker 容器中的 postgres [英] Connect to postgres in docker container from host machine

查看:202
本文介绍了从主机连接到 docker 容器中的 postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从主机连接到 docker 中的 postgres?

How can I connect to postgres in docker from a host machine?

docker-compose.yml

docker-compose.yml

version: '2'

networks:
    database:
        driver: bridge
services:
    app:
        build:
            context: .
            dockerfile: Application.Dockerfile
        env_file:
            - docker/Application/env_files/main.env
        ports:
            - "8060:80"
        networks:
           - database
        depends_on:
            - appdb

    appdb:
        image: postdock/postgres:1.9-postgres-extended95-repmgr32
        environment:
            POSTGRES_PASSWORD: app_pass
            POSTGRES_USER: www-data
            POSTGRES_DB: app_db
            CLUSTER_NODE_NETWORK_NAME: appdb
            NODE_ID: 1
            NODE_NAME: node1
        ports:
            - "5432:5432"
        networks:
            database:
                aliases:
                    - database

docker-compose ps

docker-compose ps

           Name                          Command               State               Ports
-----------------------------------------------------------------------------------------------------
appname_app_1     /bin/sh -c /app/start.sh         Up      0.0.0.0:8060->80/tcp
appname_appdb_1   docker-entrypoint.sh /usr/ ...   Up      22/tcp, 0.0.0.0:5432->5432/tcp

从容器我可以成功连接.来自应用容器和数据库容器.

From container I can connect successfully. Both from app container and db container.

在容器内运行 psql 的数据库和用户列表:

List of dbs and users from running psql inside container:

# psql -U postgres
psql (9.5.13)
Type "help" for help.

postgres=# \du
                                       List of roles
    Role name     |                         Attributes                         | Member of
------------------+------------------------------------------------------------+-----------
 postgres         | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 replication_user | Superuser, Create role, Create DB, Replication             | {}
 www-data         | Superuser                                                  | {}

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

DB 图像不是官方的 postgres 图像.但是 GitHub 中的 Dockerfile 看起来不错.

DB image is not official postgres image. But Dockerfile in GitHub seem looking fine.

来自数据库容器的cat/var/lib/postgresql/data/pg_hba.conf:

cat /var/lib/postgresql/data/pg_hba.conf from DB container:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                trust
#host    replication     postgres        127.0.0.1/32            trust
#host    replication     postgres        ::1/128                 trust

host all all all md5
host replication replication_user 0.0.0.0/0 md5

我尝试了两个用户都没有运气

I tried both users with no luck

$ psql -U postgres -h localhost
psql: FATAL:  role "postgres" does not exist
$ psql -h localhost -U www-data appdb -W
Password for user www-data:
psql: FATAL:  role "www-data" does not exist

看起来在我的主机上,该端口上已经运行了 PSQL.如何检查?

Looks like on my host machine there is already PSQL running on that port. How can I check it?

推荐答案

我相信问题是你在本地机器上的 5432 端口上运行了 postgres.问题可以通过将 docker 容器的端口 5432 映射到主机中的另一个端口来解决.这可以通过在 docker-compose.yml 中进行更改来实现

I believe the problem is you have postgres running on the local machine at port 5432. Issue can be resolved by mapping port 5432 of docker container to another port in the host machine. This can be achieved by making a change in docker-compose.yml

改变

"5432:5432" 

"5433:5432"

重启docker-compose

Restart docker-compose

现在docker容器postgres在5433上运行.(本地安装的postgres在5432上)可以尝试连接docker容器.

Now the docker container postgres is running on 5433. (Locally installed postgres is on 5432) You can try connecting to the docker container.

psql -p 5433 -d db_name -U user -h localhost

这篇关于从主机连接到 docker 容器中的 postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆