psql: error: connection to server on socket “/tmp/.s.PGSQL.5432“ failed: No such file or directory

Posted 打败起床气的阿鑫儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了psql: error: connection to server on socket “/tmp/.s.PGSQL.5432“ failed: No such file or directory相关的知识,希望对你有一定的参考价值。

当我许久不用postgresl之后,突然有一天需要连接使用这个数据库
当我连接时,出现了以下的错误

因为我是mac os系统,我用brew进行postgresql的卸载安装和更新均不起作用,
后续又查看是否环境变量配置或者是端口占用等问题也是没有成功修复。

随后灵机一闪使用了brew info postgres这个命令,可以查看最新版本的psql的信息

看到红框中,第一句命令是重启psql服务,试过了没用,那就试一试第二个命令:
/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres

果然报了一个错误

抱着试一试的心态进入目录/usr/local/var/postgres
删除了postmaster.pid
重启了psql就可以正常连接psql了,暂时还不清楚这个是一个什么原理,或者删除了有什么后果,有懂的大神可以评论区告知一下。
另外,本地环境的psql有这个问题的话实在连接不上可以死马当活马医试一试这个方法,生产环境的库的话慎用!!!


命令汇总

brew info postgres
/usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
FATAL:  lock file "postmaster.pid" already exists(本条为报错信息不是执行命令)
cd /usr/local/var/postgres
rm /usr/local/var/postgres/postmaster.pid
brew services restart postgresql

完事!!!

“psql: could not connect to server: Connection refused” Error when connecting to remote database(示例代

问题: 

I am trying to connect to a postgres database installed in a remote server using the following command:

psql -h host_ip -U db_username -d db_name

This the error that occurs:

psql: could not connect to server: Connection refused Is the server running on host "" and accepting TCP/IP connections on port 5432?

  1. Postgres installed version is 9.4.
  2. Host operating system : Ubuntu 15.04
  3. Client operating system : Centos 7

I already tried the following but the issue remains unresolved:

  1. Edited pg_hba.conf file to include

host all all 0.0.0.0/0 md5

  1. Edited ‘postgresql.conf‘ and changed the listen parameter to

listen_addresses=‘*‘

  1. Restarted postgres service.
  2. Disabled firewall and iptables on host and client.
  3. I checked by running the psql command locally and it worked.
  4. I tried the second solution given in this question. Running nmap gave me the following output:

Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http

Am I missing something. Hope someone can help.

shareedit
 
    
firewall is still blocking port 5432. check your configs for iptables. – Dmitry Savinkov Sep 7 ‘15 at 17:03
    
@Dmitry I disabled iptables by running sudo ufw disable, but I‘m still getting the same error. – Poonam Anthony Sep 8 ‘15 at 10:25
    
Can you try a ‘netstat -na|grep 5432‘ and see if postgresql binds to all interfaces? – kometen Dec 15 ‘15 at 

 

 

----------------------------------------------------------------------------

cd /etc/postgresql/9.x/main/

open file named postgresql.conf

sudo vi postgresql.conf

add this line to that file

listen_addresses = ‘*‘

then open file named pg_hba.conf

sudo vi pg_hba.conf

and add this line to that file

host  all  all 0.0.0.0/0 md5

It allows access to all databases for all users with an encrypted password

restart your server

sudo /etc/init.d/postgresql restart

以上是关于psql: error: connection to server on socket “/tmp/.s.PGSQL.5432“ failed: No such file or directory的主要内容,如果未能解决你的问题,请参考以下文章

psql 将参数传递给 sql 脚本

docker 踩坑笔记之 psql: could not connect to server

ERROR 2002 (HY000): Can’t connect to local MySQL

mongodb使用mongo报错: Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed:

ERROR 2003 (HY000): Can't connect to MySQL server

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)