【问题标题】:PostgreSQL: Could not connect to server: TCP/IP connections on port 5432PostgreSQL:无法连接到服务器:端口 5432 上的 TCP/IP 连接
【发布时间】:2019-08-15 01:05:08
【问题描述】:

我对 PostgreSQL 有一个错误:

SQLSTATE[08006] [7] 无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(::1) 上运行并且 接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:连接被拒绝 服务器是否在主机“localhost”(127.0.0.1) 上运行并接受端口 5432 上的 TCP/IP 连接?

以下命令未显示 5432 端口已打开:

sudo netstat -plunt |grep postgres

我猜问题出在 5432 端口,所以我在 netstat 没有看到它。
如何为 PostgreSQL 打开端口?

  1. postgresql.confpg_hba.conf 文件正常(见下文)。
  2. postgres 服务器正在运行
  3. 我的postgresql.conf文件:
listen_addresses = '*'
port = 5432
  1. 我的pg_hba.conf文件:
local   all             postgres                                md5
local   all             all                                     md5
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust
host    all             all             ::1/128                 trust
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
  1. 我运行了命令:$ netstat -atu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:6379          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:http            0.0.0.0:*               LISTEN
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:https           0.0.0.0:*               LISTEN
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11807 ESTABLISHED
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11258 ESTABLISHED
tcp        0      0 30secondboomer.com:ssh  109-252-90-59.nat:11797 ESTABLISHED
tcp6       0      0 localhost:6379          [::]:*                  LISTEN
tcp6       0      0 [::]:http               [::]:*                  LISTEN
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN
tcp6       0      0 [::]:https              [::]:*                  LISTEN
udp        0      0 localhost:domain        0.0.0.0:*
udp        0      0 30secondboomer.c:bootpc 0.0.0.0:*

【问题讨论】:

  • 请检查日志。 PostgreSQL 服务器显然(netstat 输出)没有运行。
  • 我使用命令“sudo service postgresql status” ● postgresql.service - PostgreSQL RDBMS 已加载:已加载(/lib/systemd/system/postgresql.service;已启用;供应商预设:已启用) 活动:活动(已退出)自 2019 年 3 月 24 日星期日 21:01:12 MSK 起; 6 分钟前进程:24974 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 24974 (code=exited, status=0/SUCCESS) Mar 24 21:01:12 cs451508 systemd[1]:正在启动 PostgreSQL RDBMS... 3 月 24 日 21:01:12 cs451508 systemd[1]: 启动 PostgreSQL RDBMS。
  • 本地主机应该对自己开放。您是否要在该端口上的服务器之间跳转?还是您在同一台服务器上连接到数据库?
  • 在同一台服务器上
  • 服务器似乎没有运行。以下是 PostgreSQL 日志: 致命:pg_hba.conf 拒绝主机“109.252.90.59”的连接 LOG:收到快速关闭请求 LOG:中止任何活动事务 LOG:后台工作人员“逻辑复制启动器”(PID 421)退出,退出代码为 1 LOG : 关闭

标签: postgresql database-connection database-server


【解决方案1】:

在设置 PostgreSQL 以使用 Python/Django 项目时遇到类似错误。

  1. 再次尝试stoprestart 服务器。
  2. 如果您怀疑确实是端口5432,您可以尝试在另一个端口上start postgres5433,看看会发生什么。
  3. 对于那些database server 根本没有运行的人,在阅读了PostgreSQL-9.6.16 官方文档后,这个solution 对我有用。

【讨论】:

    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多