Error in connecting PostgreSQL

I am trying to connect to postgresql using Django documentation.
However, I am getting following error.
I am using Windows 10.

(lecture3) C:\Users\admin01\Documents\edx cs50\CS50Web\iExam>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\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
    self.connect()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 211, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\postgresql\base.py", line 199, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
        Is the server running on that host and accepting TCP/IP connections?


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

Traceback (most recent call last):
  File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\core\management\commands\runserver.py", line 127, in inner_run
    self.check_migrations()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\core\management\base.py", line 505, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
    self.build_graph()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\migrations\loader.py", line 223, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 270, in cursor
    return self._cursor()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 246, in _cursor
    self.ensure_connection()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
    self.connect()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
    self.connect()
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\base\base.py", line 211, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\utils\asyncio.py", line 25, in inner
    return func(*args, **kwargs)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\django\db\backends\postgresql\base.py", line 199, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\admin01\Documents\edx cs50\CS50Web\django\lecture3\lib\site-packages\psycopg2\__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
        Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
        Is the server running on that host and accepting TCP/IP connections?

settings.py in django

DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.postgresql_psycopg2',
        'NAME':'iExam',
        'USER':'adm',
        'PASSWORD':'password',
        'HOST':'localhost',
        'PORT':'',
    }
}

Do you have PostgreSQL installed and running on your workstation? Is it using port 5432?

Do you have Window’s firewall running? If so, do you have port 5432 open?

Have you verified through other means such as psalm that those credentials are correct?

Are you able to connect to your local PostgreSQL instance using other tools such as psalm or PgAdmin?

Hi,

Thanks for reply.
I have imported only psycopg2 using python. I have not installed anything about postgresql otherwise. I was just following instructions on django documentation.

Windows firewall is not running on my machine.
But, anti-virus is running.

If you are going to try to connect to a PostgreSQL database, that database needs to exist somewhere.

Thank you for the reply.
I will try it.

I’m curious.

Specifically, which instructions were you following? (Which page?)

I was following below instructions.
https://docs.djangoproject.com/en/4.0/ref/databases/#postgresql-notes

Ok, it’s more clear on How to install Django | Django documentation | Django, specifically at Get your database running.

Thank you for this info.

As previously mentioned, you need to actually install the database software on the machine and have it running using the user/password settings you have declared in SETTINGS.

You can then create the database (named iExam in your case) using various tools such as PgAdmin, DBeaver, or TablePlus if you want something more light weight.

Thanks for the reply.

check db run or not and check db port