Postgresql 是本地服务器 运行 并接受 Unix 域上的连接

Postgresql is the server running locally and accepting connection on Unix domain

我正在尝试在我的 ubuntu 16.04 上安装 postgresql-9.5postgresql-9.614.04 机器,在使用以下命令完成安装过程后。

sudo apt-get install update
sudo apt-get install postgresql postgresql-contrib

但是当我尝试使用以下命令从我的终端打开psql

sudo su postgres 
psql 

每次都只是显示。

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"?

我已经尝试了大约 10 whosebug.com 种解决方案,但都没有成功。在我扯掉所有头发之前请帮助我:(

$ 服务 postgresql 状态

● postgresql.service - PostgreSQL RDBMS
   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
   Active: active (**exited) since রবি 2017-03-12 21:45:56 BDT; 23min ago
  Process: 917 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
 Main PID: 917 (code=exited, status=0/SUCCESS)
    Tasks: 0
   Memory: 0B
      CPU: 0
   CGroup: /system.slice/postgresql.service

仔细看这行为什么退出了? 活动:自 2017 年 3 月 12 日以来活动(退出)21:45:56 BDT; 23 分钟前

你可以运行下面的命令来检查postgresql是否运行ning:

service postgresql status

如果 PostgreSQL 未启动,您可以使用以下命令启动它:

service postgresql start
service postgresql status

如果没有正常启动,可以看看日志是怎么回事:

tail /var/log/postgresql/postgresql-9.6-main.log

您可能需要 运行 createdb 来初始化数据库(参见 https://www.postgresql.org/docs/current/static/app-createdb.html):

createdb demo

更新

如果当您执行 service postgresql status 时 PostgreSQL 运行ning 但您仍然无法连接,您应该检查 PostgreSQL 是否确实 运行ning 在端口 5432 或另一个端口上:

netstat -na | grep postgres

更新 2

由于 debian/ubuntu 上用于 PostgreSQL 的本机软件包很烂,我建议您改为安装这些软件包:https://www.gab.lc/articles/install_postgresql_9-5_debian_ubuntu(将 9-2 替换为 9-6)。

这就是删除现有 postgresql 版本和目录后最终解决我的问题的方法

sudo apt-get --purge remove postgresql-*
sudo rm -Rf /etc/postgresql /var/lib/postgresql
sudo apt-get install postgresql