You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 20.04下多版本PostgreSQL无法启动及连接失败求助

Ubuntu 20.04下多版本PostgreSQL无法启动及连接失败求助

我在网上查了一大堆资料尝试解决PostgreSQL的问题,但折腾了好久还是没进展,实在没办法来求助了!

先说说我做过的操作和遇到的情况:

首先,我参考资料修改了PostgreSQL的配置文件postgresql.conf,修改后的相关配置如下:

listen_addresses = '*'
port= 5432
data_directory = '/var/lib/postgresql/9.3/main'
unix_socket_directories = '/var/run/postgresql,/tmp'    # comma-separated list
# (change requires restart)
unix_socket_group = ''                  # (change requires restart)
unix_socket_permissions = 0777          # begin with 0 to use octal notation
# (change requires restart)
bonjour = off                           # advertise server via Bonjour
# (change requires restart)
bonjour_name = ''

修改完后我执行了重启命令:

ali@ali:~$ sudo systemctl restart postgresql

然后尝试本地连接,结果报错:

ali@ali:~$ psql
psql: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?

我试着设置PGHOST环境变量再连接,还是失败:

ali@ali:~$ export PGHOST=localhost
ali@ali:~$ psql
psql: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

之后我也修改了pg_hba.conf文件,内容如下:

# Database administrative login by Unix domain socket
host   all             postgres                                peer
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             0.0.0.0/0              md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
host    all             all             192.168.1.0/24          md5

为了确保端口没问题,我配置了UFW防火墙允许5432端口:

ali@ali:$~ sudo ufw allow 5432
ali@ali:$~ sudo ufw status
WARN: /etc is world writable!
Status: active
To                         Action      From
--                         ------      ----
5432/tcp                   ALLOW       Anywhere
5432                       ALLOW       Anywhere
5433                       ALLOW       Anywhere
5432/tcp (v6)              ALLOW       Anywhere (v6)
5432 (v6)                  ALLOW       Anywhere (v6)
5433 (v6)                  ALLOW       Anywhere (v6)

接着用netstat查看监听端口,发现5432根本没在监听列表里:

ali@ali:$~ netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:46589         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:36287         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:37915         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:40483         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:42069           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:9050          0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:2628          0.0.0.0:*               LISTEN
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 ::1:631                 :::*                    LISTEN
tcp6       0      0 :::1716                 :::*                    LISTEN
udp        0      0 127.0.0.53:53           0.0.0.0:*
udp        0      0 0.0.0.0:631             0.0.0.0:*
udp        0      0 0.0.0.0:42069           0.0.0.0:*
udp        0      0 0.0.0.0:5353            0.0.0.0:*
udp        0      0 0.0.0.0:60941           0.0.0.0:*
udp6       0      0 :::5353                 :::*
udp6       0      0 :::56614                :::*
udp6       0      0 :::1716                 :::*

我还尝试用chmod把相关目录权限改成777,但还是没效果。

我的系统是Ubuntu 20.04.03,PostgreSQL我试过9.3、14、15、16这几个版本,每个版本都遇到同样的问题。

最后我查看了PostgreSQL的服务状态,发现16版本的集群启动失败了:

abolfazl@abolfazl-Lenovo-ideapad-310-15ISK:~$ sudo systemctl status 'postgresql*'
× postgresql@16-main.service - PostgreSQL Cluster 16-main
Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
Active: failed (Result: protocol) since Fri 2023-12-29 17:32:51 +0330; 10min ago
Process: 7640 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 16-main start (code=exited, status=255/EXCEPTION)
CPU: 57ms

Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: Starting PostgreSQL Cluster 16-main...
Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK postgresql@16-main[7640]: Insecure directory in $ENV{PATH} while running with -T switch at >
Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: postgresql@16-main.service: Can't open PID file /run/postgresql/16-main.pid (ye>
Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: postgresql@16-main.service: Failed with result 'protocol'.
Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: Failed to start PostgreSQL Cluster 16-main.

● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Fri 2023-12-29 17:32:51 +0330; 10min ago
Process: 7641 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 7641 (code=exited, status=0/SUCCESS)
CPU: 2ms

Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: Starting PostgreSQL RDBMS...
Dec 29 17:32:51 abolfazl-Lenovo-ideapad-310-15ISK systemd[1]: Finished PostgreSQL RDBMS.

现在实在不知道该怎么解决了,有没有大佬能指点一下?

备注:内容来源于stack exchange,提问作者Abol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 13:29:35