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

I am using the console. neon and I got an error while
“python manage.py make migrations”

Error:
makemigrations.py:160: RuntimeWarning: Got an error checking a consistent migration history performed for database connection ‘default’: connection to server at “localhost” (::1), port 5432 failed: server does not support SSL, but SSL was required

warnings.warn(
No changes detected

i am also giving a correct details
like in settings.py file

"
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql’,
‘NAME’: getenv(‘PGDATABASE’),
‘USER’: getenv(‘PGUSER’),
‘PASSWORD’: getenv(‘PGPASSWORD’),
‘HOST’: getenv(‘PGHOST’),
‘PORT’: getenv(‘PGPORT’, 5432),
‘OPTIONS’: {
‘sslmode’: ‘require’,
},
}
}
"

and it provided credentials in the .env file.

You could remove the sslmode and upgrade your PostgreSQL.
Try this with os.environ:

import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get('PGDATABASE'),
        'USER': os.environ.get('PGUSER'),
        'PASSWORD': os.environ.get('PGPASSWORD'),
        'HOST': os.environ.get('PGHOST'),
        'PORT': os.environ.get('PGPORT', 5432),
    }
}

I’m also facing this same issue, but my error is happening in render ui dashboard, I’ve been using it normally until deployment. Pls i need help.

Here is my error:

?: (staticfiles.W004) The directory ‘/opt/render/project/src/backview/static’ in the STATICFILES_DIRS setting does not exist.

Sep 20 08:46:14 PMTraceback (most recent call last):

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 279, in ensure_connection

Sep 20 08:46:14 PM self.connect()

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 256, in connect

Sep 20 08:46:14 PM self.connection = self.get_new_connection(conn_params)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py”, line 332, in get_new_connection

Sep 20 08:46:14 PM connection = self.Database.connect(**conn_params)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/psycopg2/init.py”, line 122, in connect

Sep 20 08:46:14 PM conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PMpsycopg2.OperationalError: connection to server at “localhost” (::1), port 5432 failed: Connection refused

Sep 20 08:46:14 PM Is the server running on that host and accepting TCP/IP connections?

Sep 20 08:46:14 PMconnection to server at “localhost” (127.0.0.1), port 5432 failed: Connection refused

Sep 20 08:46:14 PM Is the server running on that host and accepting TCP/IP connections?

Sep 20 08:46:14 PM

Sep 20 08:46:14 PM

Sep 20 08:46:14 PMThe above exception was the direct cause of the following exception:

Sep 20 08:46:14 PM

Sep 20 08:46:14 PMTraceback (most recent call last):

Sep 20 08:46:14 PM File “/opt/render/project/src/backview/manage.py”, line 22, in

Sep 20 08:46:14 PM main()

Sep 20 08:46:14 PM File “/opt/render/project/src/backview/manage.py”, line 18, in main

Sep 20 08:46:14 PM execute_from_command_line(sys.argv)

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 442, in execute_from_command_line

Sep 20 08:46:14 PM utility.execute()

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/init.py”, line 436, in execute

Sep 20 08:46:14 PM self.fetch_command(subcommand).run_from_argv(self.argv)

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/base.py”, line 413, in run_from_argv

Sep 20 08:46:14 PM self.execute(*args, **cmd_options)

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/base.py”, line 459, in execute

Sep 20 08:46:14 PM output = self.handle(*args, **options)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/base.py”, line 107, in wrapper

Sep 20 08:46:14 PM res = handle_func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/core/management/commands/migrate.py”, line 118, in handle

Sep 20 08:46:14 PM executor = MigrationExecutor(connection, self.migration_progress_callback)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/migrations/executor.py”, line 18, in init

Sep 20 08:46:14 PM self.loader = MigrationLoader(self.connection)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/migrations/loader.py”, line 58, in init

Sep 20 08:46:14 PM self.build_graph()

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/migrations/loader.py”, line 235, in build_graph

Sep 20 08:46:14 PM self.applied_migrations = recorder.applied_migrations()

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/migrations/recorder.py”, line 89, in applied_migrations

Sep 20 08:46:14 PM if self.has_table():

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/migrations/recorder.py”, line 63, in has_table

Sep 20 08:46:14 PM with self.connection.cursor() as cursor:

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 320, in cursor

Sep 20 08:46:14 PM return self._cursor()

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 296, in _cursor

Sep 20 08:46:14 PM self.ensure_connection()

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 278, in ensure_connection

Sep 20 08:46:14 PM with self.wrap_database_errors:

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/utils.py”, line 91, in exit

Sep 20 08:46:14 PM raise dj_exc_value.with_traceback(traceback) from exc_value

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 279, in ensure_connection

Sep 20 08:46:14 PM self.connect()

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/base/base.py”, line 256, in connect

Sep 20 08:46:14 PM self.connection = self.get_new_connection(conn_params)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/utils/asyncio.py”, line 26, in inner

Sep 20 08:46:14 PM return func(*args, **kwargs)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/django/db/backends/postgresql/base.py”, line 332, in get_new_connection

Sep 20 08:46:14 PM connection = self.Database.connect(**conn_params)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PM File “/opt/render/project/src/.venv/lib/python3.11/site-packages/psycopg2/init.py”, line 122, in connect

Sep 20 08:46:14 PM conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

Sep 20 08:46:14 PM ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sep 20 08:46:14 PMdjango.db.utils.OperationalError: connection to server at “localhost” (::1), port 5432 failed: Connection refused

Sep 20 08:46:14 PM Is the server running on that host and accepting TCP/IP connections?

Sep 20 08:46:14 PMconnection to server at “localhost” (127.0.0.1), port 5432 failed: Connection refused

Sep 20 08:46:14 PM Is the server running on that host and accepting TCP/IP connections?

Pls HELP!