从主机连接到docker容器中的postgres

mef*_*ef_ 11 postgresql docker docker-compose

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

泊坞窗,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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

从容器我可以成功连接.来自app容器和db容器.

在容器内运行psql的dbs和用户列表:

# 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)
Run Code Online (Sandbox Code Playgroud)

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

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

# 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
Run Code Online (Sandbox Code Playgroud)

我试过两个没有运气的用户

$ 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
Run Code Online (Sandbox Code Playgroud)

在我的主机上看起来已经在该端口上运行了PSQL.我怎么检查呢?

Sre*_*A R 11

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

改变

"5432:5432" 
Run Code Online (Sandbox Code Playgroud)

"5433:5432"
Run Code Online (Sandbox Code Playgroud)

重启 docker-compose

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

psql -p 5433 -d db_name -U user -h localhost
Run Code Online (Sandbox Code Playgroud)


Tar*_*ani 3

我在 Ubuntu 16.04 上运行了这个

$ psql -h localhost -U www-data app_db
Password for user www-data:
psql (9.5.13)
Type "help" for help.

app_db=# \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                                                  | {}
Run Code Online (Sandbox Code Playgroud)

下面是从我的 Mac 到运行 docker 的虚拟机(192.168.33.100 是 docker 虚拟机的 IP 地址)

$ psql -h 192.168.33.100 -U www-data app_db
Password for user www-data:
psql (9.6.9, server 9.5.13)
Type "help" for help.

app_db=# \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                                                  | {}
Run Code Online (Sandbox Code Playgroud)

他们都为我工作。

虚拟机上的 PSQL 版本

$ psql --version
psql (PostgreSQL) 9.5.13
Run Code Online (Sandbox Code Playgroud)

Mac 上的 PSQL 版本

$ psql --version
psql (PostgreSQL) 9.6.9
Run Code Online (Sandbox Code Playgroud)

在职的