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

标签 postgresql docker docker-compose

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

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

           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

从容器我可以成功连接。来自 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)

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

来自 DB 容器的 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

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

$ 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。如何查看?

最佳答案

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

改变

"5432:5432" 

"5433:5432"

重启 docker-compose

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

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

关于postgresql - 从主机连接到 docker 容器中的 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50923408/

相关文章:

sql - PostgreSQL 单列索引与多列索引以获得最佳 SELECT 性能

docker - 通过 OWASP zap 在 docker 环境中扫描 Rest API

docker - 在 $PATH 中找不到可执行文件

postgresql - 拆分数组,仅提取唯一值,然后重新合并到 Postgresql 中的数组

mysql - 在MySql服务器中恢复postgreSql的.backup文件

sql - 在 PostgreSQL 中按字母顺序对字符串中的字母进行排序

amazon-web-services - Docker 私有(private)注册表 : ping attempt failed

docker - Gitlab-runner 本地构建 - 从非 TTY 设备登录

angularjs - 更改我的项目文件不会更改 Docker 机器内的文件

tomcat - Docker 为 Tomcat 集群编写错误加载 conf/server.xml 文件