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

I finally (think) installed successfully PostgreSQL and also de psycopg2 (I use Windows). Btw, is some way to check it's working properly?

Well, the thing now is that I can't start the server, while I type 'python manage.py runserver' it shows this (at the end of the command):

conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 8000?
could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 8000?

I've searched a lot of documentation about it, for example in this topic, but I can't find the way to make it work properly. I've tried several changes in the pg_hba and postgresql files, but with no exit. In this moment, pg_hba looks like:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1            md5
# IPv6 local connections:
host    all             all             127.0.0.1            md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

And postgresql conf looks like:

# - Connection Settings -

listen_addresses = 'localhost'      # what IP address(es) to listen on;
                # comma-separated list of addresses;
                # defaults to 'localhost'; use '*' for all
                # (change requires restart)
port = 8000             # (change requires restart)
max_connections = 100           # (change requires restart)
# Note:  Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = ''   # comma-separated list of directories
                # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation
                # (change requires restart)
#bonjour = off              # advertise server via Bonjour
                # (change requires restart)
#bonjour_name = ''          # defaults to the computer name

Btw, my settings.py of the database look like this:

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

I haven't created a database BTW, how should I do it? What are the applications of the PostgreSQL prompt?

I would highly appreciate help with this issue I've been days searching and trying but with no exit. Thank you very much.

EDIT 1: I tried changing the settings.py port to 5432, but now the error message is the same, just changing the port:

could not connect to server: Connection refused (0x0000274D/10061)
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

The config files are right this way? Should I change something? I can't find an answer. I tried with python manage.py runserver and both indicating the 127.0.0.1:8000 and 8001, but no changes in the error message. What's going wrong? Thank you very much.



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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.

Solution 4:[4]

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

Solution 5:[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.

Solution 6:[6]

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.

Solution 7:[7]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Jolig
Solution 2 Usman Maqbool
Solution 3 scollaco
Solution 4 Addicted
Solution 5 zMeadz
Solution 6
Solution 7 Uffa Modey