Как правильно подключить PostgreSQL к Django?

Я работаю над проектом Django и использую PostgreSQL в качестве базы данных. Я обновил свой файл settings.py следующим образом:

**

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

**

Однако, когда я запускаю python manage.py migrate, я получаю следующую ошибку:

django.db.utils.OperationalError: could not connect to server: Connection refused

У меня установлен и запущен PostgreSQL. Я также проверил, что мои учетные данные верны. В чем может быть проблема?

Have you installed psycopg2, **pip install psycopg2 ==2.9.9** to your dev environment, and ensure you freeze it to your requirements.txt file? You can try using 127.0.0.1 as your HOST instead of localhost, and ensure that you disable firewalls. Ensure you have created your database via using psql command and grant all privileges. Then you can go ahead and run your python manage.py migrate.

If the error persist which i doubt if you had set everything correctly, then run on your postgres bash (terminal) to diagnose further.

bash 

psql -U <myuser> -d <mydatabase> -h localhost -p 5432 
Вернуться на верх