@ekolodenets

Как исправить ошибку при подключении PostgreSQL к Django?

1 создаю новый проект jango
2 python -m venv myvenv
3 myvenv\Scripts\activate
4 python -m pip install --upgrade pip
5 pip install django
6 django-admin startproject mysite .
7 качаю и устанавливаю postgresql-10.14-1-windows.exe
8 psycopg2
9 python manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK


10 mysite/settings.py меняю базу
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'jangobase',
        'USER': 'superuser',
        'PASSWORD': 'admin',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    			}
		}


11 PostgreSQL\10\scripts\runpsql.bat
postgres=# CREATE DATABASE jangobase;
	CREATE DATABASE
	postgres=# CREATE ROLE superuser WITH PASSWORD 'admin';
	CREATE ROLE
	postgres=# GRANT all ON DATABASE jangobase TO superuser;
	GRANT
	postgres=#


12 python manage.py runserver

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Ulsa\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Ulsa\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check_migrations()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\core\management\base.py", line 459, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\migrations\loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 259, in cursor
    return self._cursor()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 235, in _cursor
    self.ensure_connection()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 219, in ensure_connection
    self.connect()
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\base\base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\django\db\backends\postgresql\base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Ulsa\PycharmProjects\jango\myvenv\lib\site-packages\psycopg2\__init__.py", line 127, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
  • Вопрос задан
  • 61 просмотр
Пригласить эксперта
Ответы на вопрос 1
@ekolodenets Автор вопроса
после python manage.py dbshell пишет
CommandError: You appear not to have the 'psql' program installed or on your path.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Верфь Екатеринбург
от 70 000 до 140 000 ₽
Junto Москва
от 80 000 до 120 000 ₽
Большая Тройка Москва
от 120 000 до 200 000 ₽