[Django]-Can't run the server on Django (connection refused)

26๐Ÿ‘

For me it was for postgres not being started and enabled. So I solved the problem by doing so:

sudo systemctl start postgresql
sudo systemctl enable postgresql

8๐Ÿ‘

Make port default to 5432

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database1',                      
    'USER': 'root',
    'PASSWORD': '123456',
    'HOST': 'localhost',
    'PORT': '5432',
    }
}

run the command:

python manage.py runserver 127.0.0.1:8000

6๐Ÿ‘

I just had the same problem and I just had forgotten to start my Postgres. Opening it from my Applications and selecting โ€œOpen psqlโ€ solved the problem.

5๐Ÿ‘

If you are using Postgresql and developing on a mac then you can use the below command to start Postgresql and it should fix your problem.

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

and to stop it

pg_ctl -D /usr/local/var/postgres stop -s -m fast

5๐Ÿ‘

Try removing host and port:

# settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_pass',
    }
}

The problem for me was specifying HOST and/or PORT here.

2๐Ÿ‘

I faced a similar issue and I solved it by changing the value of "HOST": "localhost" to "HOST": "127.0.0.1" and it worked.

0๐Ÿ‘

Credit to Addicted above. I checked the log and realized at some point I had upgraded my Postgres version.

$ nano server.log

  GNU nano 2.0.6                               File: server.log                                                                     

2020-12-16 02:19:32.273 EST [32414] FATAL:  database files are incompatible with server
2020-12-16 02:19:32.273 EST [32414] DETAIL:  The data directory was initialized by PostgreSQL version 12, which is not compatible with this version 13.0.

Which can be solved like this.

0๐Ÿ‘

no database server is hosted and running locally. For mac, you can turn on a server on port 5432 using the postgres desktop application.

0๐Ÿ‘

I Had the same problem but in my case its due to memory shortage.
Due to memory shortage the postgresql was not able to create the file .s.PGSQL.5432 inside /var/run/postgresql/.
So Look for the memory storage and if there is no memory available ,please free some memory and try restarting postgresql service.

0๐Ÿ‘

I had the same problem because I forgot to run docker.

Leave a comment