E
E
ekolodenets2020-08-26 23:39:51
PostgreSQL
ekolodenets, 2020-08-26 23:39:51

How to fix error when connecting PostgreSQL to Django?

1 create a new jango project
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 download and install 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 change base
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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
ekolodenets, 2020-08-29
@ekolodenets

after python manage.py dbshell writes

CommandError: You appear not to have the 'psql' program installed or on your path.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question