首页 > 解决方案 > PostgreSQL 服务器没有监听

问题描述

大约一个月前,我在 Ubuntu 21.04 上使用 command 安装了 PostgreSQL sudo apt install postgresql,直到今天才忘记它。今天,我尝试了 psql 并得到一个错误,表明它没有在 Unix 域套接字上侦听:

$ sudo -i -u postgres psql
psql: error: 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"?
$

netstat表明它也没有在端口 5432 上侦听。

端口 5432 没有被任何防火墙或其他反恶意软件阻止:我可以nc -l -p 5432从另一台机器连接到它。

我试过检查其他东西:

$ sudo systemctl is-active postgresql
active
$ sudo systemctl is-enabled postgresql
enabled
$ sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2021-07-20 12:38:20 BST; 3 weeks 1 days ago
    Process: 706 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 706 (code=exited, status=0/SUCCESS)

Jul 20 12:38:20 pluto systemd[1]: Starting PostgreSQL RDBMS...
Jul 20 12:38:20 pluto systemd[1]: Finished PostgreSQL RDBMS.
$ ls -al /etc/postgresql
total 16
drwxr-xr-x   2 postgres postgres  4096 Feb 26 00:21 .
drwxr-xr-x 163 root     root     12288 Aug 11 08:36 ..
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
$ 

为了让服务器运行,我应该做apt install一些我没有做的设置吗?

标签: postgresqlubuntu

解决方案


感谢@AdrianKlaver 在他的评论中的提示,我已经使用这些命令运行了服务器:

$ sudo -i -u postgres
$ /usr/lib/postgresql/13/bin/initdb -D /tmp/pgdata  #In /tmp as I'm just playing with it
$ /usr/lib/postgresql/13/bin/pg_ctl -D /tmp/pgdata -l logfile start

这使我可以连接到正在侦听端口 5432 的服务器,但我不确定每次重新启动时如何启动它。


推荐阅读