Search code examples
djangopostgresqlnetwork-programmingdjango-rest-frameworkport

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


I am doing a project using django rest framework. It was working ok, but at the moment I am getting error

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?

I knew that the problem with postgresql. Because I've tried to connect it with pgadmin, and also gives me this error.
I am using Ubuntu OS, when I checked ubuntu not listening port 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)

When I run service postgresql status, it says following.

● 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.

Here is the output of the 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              [::]:* 

Seems something caused blocked 5432 port. How to solve this problem ?

postgresql.log
enter image description here


Solution

  • The problem is that /etc directory needs to be root user, so I changed it to root user, and opened 5432 port, in my case /etc directory owned by other user, now everything is working.