无法连接到5432端口的postgresql

我安装了Bitnami Django stack,其中包括PostgreSQL 8.4。

当我运行psql -U postgres时,我得到以下错误。

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

PG肯定在运行,pg_hba.conf文件看起来像这样。

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

什么原因?

"证明"pg正在运行。

root@assaf-desktop:/home/assaf# ps axf | grep postgres
14338 ?        S      0:00 /opt/djangostack-1.3-0/postgresql/bin/postgres -D /opt/djangostack-1.3-0/postgresql/data -p 5432
14347 ?        Ss     0:00  \_ postgres: writer process                                                                        
14348 ?        Ss     0:00  \_ postgres: wal writer process                                                                    
14349 ?        Ss     0:00  \_ postgres: autovacuum launcher process                                                           
14350 ?        Ss     0:00  \_ postgres: stats collector process                                                               
15139 pts/1    S+     0:00              \_ grep --color=auto postgres
root@assaf-desktop:/home/assaf# netstat -nltp | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      14338/postgres  
tcp6       0      0 ::1:5432                :::*                    LISTEN      14338/postgres  
root@assaf-desktop:/home/assaf# 

错误信息指的是一个Unix域的套接字,所以你需要调整你的netstat调用,不要把它们排除在外。 因此,请尝试不使用选项 "t"。

netstat -nlp | grep 5432

我猜测服务器实际上是在监听套接字/tmp/.s.PGSQL.5432,而不是你的客户端试图连接的/var/run/postgresql/.s.PGSQL.5432。 这是在Debian或Ubuntu上使用手工编译或第三方PostgreSQL包时的一个典型问题,因为Unix域套接字目录的源码默认是/tmp,但Debian打包时将其改为/var/run/postgresql

可能的解决方法。

  • 使用第三方软件包提供的客户端(调用/opt/djangostack-1.3-0/postgresql/bin/psql)。 可能完全卸载Ubuntu提供的软件包(由于其他反向依赖,可能会有困难)。
  • 修复第三方软件包的socket目录,使其与Debian/Ubuntu兼容。
  • 使用-H localhost来代替TCP/IP连接。
  • 使用-h /tmp或同等的PGHOST设置来指向正确的目录。
  • 不要使用第三方软件包。
评论(0)

你可以使用psql -U postgres -h localhost来强制连接到TCP而不是UNIX域套接字;你的netstat输出显示PostgreSQL服务器正在监听localhost'的端口5432。

你可以通过使用不同的调用netstat来了解PostgrSQL服务器使用的是哪个本地UNIX套接字。

netstat -lp --protocol=unix | grep postgres

无论如何,PostgreSQL服务器监听的接口是在postgresql.conf中配置的。

评论(0)

只要创建一个像这样的软链接。

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
评论(4)