django 连接到服务器“localhost”,端口5432失败:连接被拒绝服务器是否在该主机上运行并接受TCP/IP连接?

rqmkfv5c  于 2022-11-18  发布在  Go
关注(0)|答案(2)|浏览(764)

我正在做一个项目使用django休息框架。它工作正常,但目前我得到错误

connection to server at "localhost"(127.0.0.1), port 5432 failed:  
Connection refused Is the server running on that host and accepting TCP/IP connections?

我知道postgresql的问题。因为我试过用pgadmin连接它,也给了我这个错误。
我正在使用Ubuntu操作系统,当我检查ubuntu不监听端口5432时。

使用postgresql.conf配置文件

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for >
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/var/run/postgresql'        # comma-separated list >
                                        # (change requires restart)

当我运行service postgresql status时,它显示如下。

● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pr>
     Active: active (exited) since Tue 2022-05-17 09:22:03 +05; 1h 9min ago
    Process: 6270 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 6270 (code=exited, status=0/SUCCESS)

May 17 09:22:03 mirodil-vivobook systemd[1]: Starting PostgreSQL RDBMS...
May 17 09:22:03 mirodil-vivobook systemd[1]: Finished PostgreSQL RDBMS.

下面是ss -nlt的输出:

State   Recv-Q   Send-Q     Local Address:Port      Peer Address:Port  Process  
LISTEN  0        511            127.0.0.1:40915          0.0.0.0:*              
LISTEN  0        4096       127.0.0.53%lo:53             0.0.0.0:*              
LISTEN  0        5              127.0.0.1:631            0.0.0.0:*              
LISTEN  0        4096           127.0.0.1:9050           0.0.0.0:*              
LISTEN  0        5              127.0.0.1:39261          0.0.0.0:*              
LISTEN  0        5                0.0.0.0:35587          0.0.0.0:*              
LISTEN  0        128              0.0.0.0:25672          0.0.0.0:*              
LISTEN  0        511            127.0.0.1:6379           0.0.0.0:*              
LISTEN  0        511                    *:80                   *:*              
LISTEN  0        4096                   *:4369                 *:*              
LISTEN  0        5                  [::1]:631               [::]:*              
LISTEN  0        511                    *:35711                *:*              
LISTEN  0        128                    *:5672                 *:*              
LISTEN  0        511                [::1]:6379              [::]:*

5432端口似乎被阻塞了。如何解决此问题?

postgresql.log日志文件

nxagd54h

nxagd54h2#

问题是/etc目录需要root用户,所以我将其更改为root用户,并打开5432端口,在我的情况下/etc目录由其他用户拥有,现在一切都正常了。

相关问题