psql: could not connect to server: no such file or directory

当尝试连接PostgreSQL数据库时遇到'psql: could not connect to server: No such file or directory'的错误。问题可能源于安装的PostgreSQL版本或配置。解决方案包括检查服务器状态、修改连接参数、配置文件设置以及修复套接字路径。文章列举了10种不同的解决方法,适用于不同情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

问题描述

我安装了包含PostgreSQL 8.4的Bitnami Django stack

当我运行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正在运行的”Proof”:

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# 

最佳解决方案

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

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

首先清除旧的postgres安装。目前的问题在于9.1,所以我假设你已经安装了

sudo apt-get remove --purge postgresql-9.1

现在只需重新安装

sudo apt-get install postgresql-9.1
出现该错误的原因可能是 PostgreSQL 服务器没有在指定的端口或 Unix 域套接字文件上运行。您可以按照以下步骤来解决此问题: 1. 检查 PostgreSQL 服务器是否正在运行。您可以使用以下命令检查: ``` ps -ef | grep postgres ``` 如果 PostgreSQL 服务器正在运行,则应该会看到类似以下的输出: ``` postgres 12345 1 0 10:00 ? 00:00:01 /usr/lib/postgresql/9.6/bin/postgres -D /var/lib/postgresql/9.6/main -c config_file=/etc/postgresql/9.6/main/postgresql.conf ``` 其中 12345 是 PostgreSQL 服务器进程的 PID。 2. 检查 PostgreSQL 服务器是否在指定的端口上运行。您可以使用以下命令检查: ``` netstat -an | grep 2345 ``` 如果 PostgreSQL 服务器正在监听指定的端口,则应该会看到类似以下的输出: ``` tcp 0 0 127.0.0.1:2345 0.0.0.0:* LISTEN ``` 3. 检查 PostgreSQL 服务器是否在指定的 Unix 域套接字文件上运行。您可以使用以下命令检查: ``` ls -l /tmp/.s.PGSQL.2345 ``` 如果 PostgreSQL 服务器正在监听指定的 Unix 域套接字文件,则应该会看到类似以下的输出: ``` srwxrwxrwx 1 postgres postgres 0 10月 1 10:00 /tmp/.s.PGSQL.2345 ``` 如果文件不存在,则说明 PostgreSQL 服务器没有在该 Unix 域套接字文件上运行。 如果 PostgreSQL 服务器正在运行,并且在指定的端口或 Unix 域套接字文件上监听连接,则您可以尝试使用默认端口(5432)连接到服务器,或通过指定 Unix 域套接字文件的完整路径来连接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值