Home > Enterprise >  Django local server not starting after hard reboot
Django local server not starting after hard reboot

Time:03-03

I am a newbie developing a Django site in a pipenv virtual environment. The server has been starting and working with no problems. After my computer froze and I restarted with a hard reboot, I can't get the Django server to work.

Here is the error I see after running, python manage.py runserver

((site1) ) userone@theusers-MBP site1 % 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 "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/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
    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
    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 "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check_migrations()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/core/management/base.py", line 459, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django_tenants/postgresql_backend/base.py", line 122, in _cursor
    cursor = super()._cursor()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/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
    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
    Is the server running on that host and accepting TCP/IP connections?

CodePudding user response:

Seems that after reboot you do not have postgres running. Can you try logging into pgadmin? Usually it gets installed with postgres default settings. So go to your windows -start menu - programs - pgadmin. See if you can connect from there so we can make sure that you have Postgres running before investigating further. Let me know how it goes.

CodePudding user response:

First confirm that the default port for postgres is mentioned as 5432 in the settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',                      
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Next, start the Postgres by calling:

# Linux
sudo systemctl start PostgreSQL

# Macos
brew services start postgresql 
  • Related