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

本文档详细解释了在Red Hat Enterprise Linux 6环境下,使用psql命令连接PostgreSQL数据库遇到“No such file or directory”错误的原因及解决方案。通过确认postgresql服务是否运行在默认TCP端口5432上,并提供了检查服务运行状态和更改连接参数的方法。

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

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

 SOLUTION 已验证 - 已更新 2015年三月27日19:52 - 

English 

环境

  • Red Hat Enterprise Linux 6 (RHEL)
  • PostgreSQL database

问题

  • We are unable to login the PostgreSQL database of Jon server application using the command psql. Tried to login as postgres and root user, but getting the same error. Please see the error details below and guide us fix this error ASAP.

Raw

bash-4.3$ psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

决议

  • The error message indicates that postgresql service is not running and listening to default tcp port 5432. Use the below command and confirm tcp port used by postgresql service. Then use correct command to connect it. For example :

Raw

# ps -ef | grep postgres

postgres  6043     1  0 16:20 ?        00:00:00 /usr/bin/postgres -D /var/lib/pgsql/data -p 5454
postgres  6044  6043  0 16:20 ?        00:00:00 postgres: logger process   
postgres  6046  6043  0 16:20 ?        00:00:00 postgres: checkpointer process   
postgres  6047  6043  0 16:20 ?        00:00:00 postgres: writer process   
postgres  6048  6043  0 16:20 ?        00:00:00 postgres: wal writer process   
postgres  6049  6043  0 16:20 ?        00:00:00 postgres: autovacuum launcher process   

# bash-4.3$ psql  rhq --host=localhost --port=5454 --username=rhqadmin --password 
Password for user rhqadmin: 
psql (9.2.0)
Type "help" for help.
rhq=>

根源

  • The postgresql service was running on port 5454 instead of default port 5432.

诊断步骤

  • Collect the following commands output and check tcp port used by postgresql service

Raw

# ps -ef | grep postgres
# netstat -tulnp | grep postgres
出现该错误的原因可能是 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、付费专栏及课程。

余额充值