首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能连接到码头的Postgres?

为什么我不能连接到码头的Postgres?
EN

Unix & Linux用户
提问于 2020-02-08 14:25:04
回答 2查看 51.7K关注 0票数 8

我使用了码头工开放式服务项目. ,这两个码头集装箱成功启动。

代码语言:javascript
复制
kshnkvn@kshnkvn-vb:~$ docker ps
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
10fafbab73dc        openpoiservice_gunicorn_flask   "/ops_venv/bin/gunic…"   23 minutes ago      Up 22 minutes       0.0.0.0:5000->5000/tcp   openpoiservice_gunicorn_flask_1
a66fe5691455        kartoza/postgis:11.0-2.5        "/bin/sh -c /docker-…"   23 minutes ago      Up 22 minutes       5432/tcp                 openpoiservice_psql_postgis_db_1

但是,当试图检查服务的功能时,它无法连接到数据库。

代码语言:javascript
复制
kshnkvn@kshnkvn-vb:~$ docker exec -it 10fafbab73dc /bin/bash
root@10fafbab73dc:/deploy/app# psql -h localhost -U gis_admin-gis
psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# 

奇怪,检查一下,以防集装箱网络的类型是桥:

代码语言:javascript
复制
kshnkvn@kshnkvn-vb:~$ docker network ls
NETWORK ID          NAME                         DRIVER              SCOPE
81001dac99c0        bridge                       bridge              local
8e65fb4ef6f8        host                         host                local
94ce4e1605ef        none                         null                local
a3f48ac3facc        openpoiservice_default       bridge              local
e3d4286df013        openpoiservice_poi_network   bridge              local

已检查的Postgres启动日志:

代码语言:javascript
复制
kshnkvn@kshnkvn-vb:~$ docker logs a66fe5691455
Add rule to pg_hba: 0.0.0.0/0
Add rule to pg_hba: replication replicator 
Setup master database
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
2020-02-08 13:50:20.675 UTC [25] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-02-08 13:50:20.683 UTC [25] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:20.756 UTC [37] LOG:  database system was interrupted; last known up at 2020-02-08 13:35:17 UTC
2020-02-08 13:50:21.830 UTC [48] postgres@postgres FATAL:  the database system is starting up
psql: FATAL:  the database system is starting up
2020-02-08 13:50:22.726 UTC [37] LOG:  database system was not properly shut down; automatic recovery in progress
2020-02-08 13:50:22.730 UTC [37] LOG:  redo starts at 0/21CCC50
2020-02-08 13:50:22.730 UTC [37] LOG:  invalid record length at 0/21CCC88: wanted 24, got 0
2020-02-08 13:50:22.730 UTC [37] LOG:  redo done at 0/21CCC50
2020-02-08 13:50:22.867 UTC [25] LOG:  database system is ready to accept connections
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

postgres ready
Setup postgres User:Password
Creating superuser gis_admin
ALTER ROLE
Creating replication user replicator
ALTER ROLE
gis db already exists
                              List of databases
   Name    |   Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+-----------+----------+---------+---------+-----------------------
 gis       | gis_admin | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |           |          |         |         | postgres=CTc/postgres
(4 rows)

2020-02-08 13:50:24.785 UTC [25] LOG:  received smart shutdown request
2020-02-08 13:50:24.799 UTC [25] LOG:  background worker "logical replication launcher" (PID 58) exited with exit code 1
2020-02-08 13:50:24.801 UTC [53] LOG:  shutting down
2020-02-08 13:50:24.838 UTC [25] LOG:  database system is shut down
Postgres initialisation process completed .... restarting in foreground
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-02-08 13:50:25.842 UTC [148] LOG:  listening on IPv6 address "::", port 5432
2020-02-08 13:50:25.850 UTC [148] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-02-08 13:50:25.880 UTC [150] LOG:  database system was shut down at 2020-02-08 13:50:24 UTC
2020-02-08 13:50:25.887 UTC [148] LOG:  database system is ready to accept connections

看起来Postgres是从IP地址启动的,几乎是0.0.0.0。

我查看了Docker ip addr show命令使用的IP。试图使用这个IP重新连接:

代码语言:javascript
复制
psql: could not connect to server: Connection refused
    Is the server running on host "172.17.0.1" and accepting
    TCP/IP connections on port 5432?
root@10fafbab73dc:/deploy/app# psql -h 172.17.255.255 -U gis_admin-gis
psql: could not connect to server: Connection timed out
    Is the server running on host "172.17.255.255" and accepting
    TCP/IP connections on port 5432?

我能做什么来连接脚本到数据库呢?

EN

回答 2

Unix & Linux用户

回答已采纳

发布于 2020-02-08 18:04:31

TLDR;

代码语言:javascript
复制
psql -h psql_postgis_db -U gis_admin gis
# or
psql -h psql_postgis_db gis gis_admin

服务器地址

问题

你正在尝试的所有IP实际上都是正确的。

  • 127.0.0.1是本地主机地址。因为您从您的烧瓶容器中启动了命令,所以没有posgres服务在那里运行。
  • 172.17.0.1是码头桥的ip。这实际上是您的码头引擎主机的ip,正如您在同一桥上的容器所看到的那样。除非您的计算机上运行了postgres并侦听该ip,否则您将得不到答案(而且这也不是正确的postgres服务器)
  • 172.17.255.255是前一桥接网络的网络广播地址。

从您的开始日志中,我们可以看到您的postgres应该正确地侦听。0.0.0.0实际上不是一个真正的ip:它代表“在此主机上配置的任何ip”。

您可以查找postgres容器的ip来联系它(例如,请参阅这个答案是这样的),但您甚至不必联系它。docker/docker-compose通过将同一网络上的容器/服务名称自动映射到各自的IP上,使您更容易做到这一点。因此,可以使用服务名psql_postgis_db访问db服务器。

问题与用户和数据库名称

我并不真正理解您在-U选项中所写的内容到psql命令(用户+ db名称的组合.)。无论如何,它应该是您想要连接到postgres服务器的用户名。在您的撰写文件中,它是gis_admin。由于没有与用户同名的db,所以需要指定要连接到的db名称。您可以使用-U选项(psql -U )或使用位置参数(psql )。

票数 12
EN

Unix & Linux用户

发布于 2021-03-26 00:49:08

只需将端口参数添加到docker-compose.yml。

示例:

代码语言:javascript
复制
# Use postgres/example user/password credentials
version: '3.1'

services:

  db:
    image: postgres
    restart: always
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: example
    ports:
      - "5432:5432"
票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/566456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档