无法连接到 PostgreSQL 侦听端口 5432

Ass*_*vie 99 postgresql

我安装了包含 PostgreSQL 8.4的Bitnami Django 堆栈

当我运行时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"?
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

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# 
Run Code Online (Sandbox Code Playgroud)

小智 99

此问题来自安装postgres没有版本号的软件包。虽然postgres会安装并且是正确的版本,但是设置集群的脚本不会正确运行;这是一个包装问题。

如果您对postgres有一个脚本感到满意,您可以运行它来创建此集群并开始postgres运行。但是,还有一种更简单的方法。

首先清除旧的 postgres 安装,这将删除旧安装的所有内容,包括数据库,因此请先备份您的数据库。. 目前的问题在于 9.1,所以我假设这就是你安装的

sudo apt-get remove --purge postgresql-9.1
Run Code Online (Sandbox Code Playgroud)

现在只需重新安装

sudo apt-get install postgresql-9.1
Run Code Online (Sandbox Code Playgroud)

记下带有版本号的包名称。哈。


小智 32

错误消息指的是 Unix 域套接字,因此您需要调整netstat调用以不排除它们。因此,请在没有选项的情况下尝试-t

netstat -nlp | grep 5432
Run Code Online (Sandbox Code Playgroud)

我猜想服务器实际上是在侦听套接字,/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设置指向正确的目录。
  • 不要使用第三方软件包。


小智 26

这对我有用:

编辑:postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf
Run Code Online (Sandbox Code Playgroud)

启用或添加:

listen_addresses = '*'
Run Code Online (Sandbox Code Playgroud)

重启数据库引擎:

sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)

此外,您可以检查文件 pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf
Run Code Online (Sandbox Code Playgroud)

并添加您的网络或主机地址:

host    all             all             192.168.1.0/24          md5
Run Code Online (Sandbox Code Playgroud)

  • Listen_address = '*' 成功了。它只在“localhost”上侦听,而不是在 127.0.0.1 上侦听。谢谢你! (3认同)

Ric*_*rri 19

您可以使用psql -U postgres -h localhost强制连接通过 TCP 而不是 UNIX 域套接字发生;您的netstat输出显示 PostgreSQL 服务器正在侦听 localhost 的端口 5432。

您可以通过使用netstat的不同调用来找出 PostgrSQL 服务器使用的本地 UNIX 套接字:

netstat -lp --protocol=unix | grep postgres
Run Code Online (Sandbox Code Playgroud)

无论如何,PostgreSQL 服务器侦听的接口都是在postgresql.conf.


小智 19

只需创建一个这样的软链接:

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

  • ln: 未能创建符号链接 '/var/run/postgresql/.s.PGSQL.5432': 文件存在 (2认同)

小智 9

我通过这样做来使它工作:

dpkg-reconfigure locales
Run Code Online (Sandbox Code Playgroud)

选择您喜欢的语言环境,然后运行

pg_createcluster 9.5 main --start
Run Code Online (Sandbox Code Playgroud)

(9.5 是我的 postgresql 版本)

/etc/init.d/postgresql start
Run Code Online (Sandbox Code Playgroud)

然后它起作用了!

sudo su - postgres
psql
Run Code Online (Sandbox Code Playgroud)


小智 6

我不得不在 Debian Squeeze 上编译 PostgreSQL 8.1,因为我使用的是基于 OpenACS 的 Project Open,不会在更新的 PostgreSQL 版本上运行。

默认编译配置将unix_socketin 放入/tmp,但依赖于 PostgreSQL 的 Project Open 将不起作用,因为它会查找unix_socketat /var/run/postgresql

有一个设置postgresql.conf来设置插座的位置。我的问题是,无论是我能为设置/tmppsql工作,但没有项目打开,或者我可以将其设置为/var/run/postgresqlpsql不工作,但项目的开放做了。

该问题的一种解决方案是根据 Peter 的建议为 设置套接字/var/run/postgresql然后运行psql,如下所示:

psql -h /var/run/postgresql
Run Code Online (Sandbox Code Playgroud)

这使用本地权限在本地运行。唯一的缺点是它比简单的“psql”打字更多。

有人提出的另一个建议是在两个位置之间创建一个符号链接。这也有效,但是,链接在重新启动后消失了。仅使用 -h 参数可能更容易,但是,我从/etc/init.d. 我将符号链接创建命令放在“开始”部分。当然,当我发出停止和启动或重新启动命令时,它会尝试重新创建一个现有的符号链接,但除了警告消息外,这可能没有坏处。

在我的情况下,而不是:

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

我有

ln -s /var/run/postgresql/.s.PGSQL.5432 /tmp/.s.PGSQL.5432
Run Code Online (Sandbox Code Playgroud)

并已将 unix_socket 显式设置为/var/run/postgresql/.s.PGSQL.5432in postgresql.conf


小智 6

如果您的 Postgres 服务已启动并运行,没有任何错误,或者启动 Postgres 服务时没有错误,但您仍然收到上述错误,请按照以下步骤操作

第1步:运行pg_lsclusters将列出您设备上运行的所有postgres集群

例如:

Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log
Run Code Online (Sandbox Code Playgroud)

您的情况和 postgres 服务的状态很可能会下降

步骤2:重新启动pg_ctlcluster

#format is pg_ctlcluster <version> <cluster> <action>
sudo pg_ctlcluster 9.6 main start

#restart postgresql service
sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)

第3步:第2步失败并抛出错误

如果此过程不成功,则会抛出错误。登录就可以看到错误/var/log/postgresql/postgresql-9.6-main.log

我的错误是:

FATAL: could not access private key file "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to the group `ssl-cert`
Run Code Online (Sandbox Code Playgroud)

步骤 4:检查 postgres 的所有权

确保其postgres所有者是/var/lib/postgresql/version_no/main

如果没有,请运行

sudo chown postgres -R /var/lib/postgresql/9.6/main/
Run Code Online (Sandbox Code Playgroud)

步骤5:检查postgres用户是否属于ssl-cert用户组

事实证明,我错误地从ssl-cert组中删除了 Postgres 用户。运行以下代码来修复用户组问题并修复权限

#set user to group back with
sudo gpasswd -a postgres ssl-cert

# Fix ownership and mode
sudo chown root:ssl-cert  /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key

# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgresql restart
Run Code Online (Sandbox Code Playgroud)


par*_*lad 5

我发现卸载 Postgres 听起来不太令人信服。这有助于解决我的问题:

  1. 启动 postgres 服务器:

    sudo systemctl start postgresql
    
    Run Code Online (Sandbox Code Playgroud)
  2. 确保服务器在启动时启动:

    sudo systemctl enable postgresql
    
    Run Code Online (Sandbox Code Playgroud)

详细信息可以在 DigitalOcean 网站上找到