[Python 3.8 Django 3.2] Cannot connect to postgres database

Issues information

  • OS: Windows
  • databases: Postgres 12.6.0
  • Programming language and version: Python 3.8.0
  • Link to your project on Github/Gitlab: fnxL/mcq-qovery (github.com)

Your issue

I am not able to get my django app connect to my postgres database.
I have double checked all the environment/secret variables to be used in settings.py file, I even tried to delete the DB and create a new one, but the issue still persists. Also made sure to set port 8000 from the app web console.
Database is successfully deployed and running. Everything else seems to work fine.
I just couldn’t get postgres to connect to my app no matter what I do.
I also couldn’t connect to it using psql

Error Log

File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection

25 Jul, 23:16:16.589
connection = Database.connect(**conn_params)
25 Jul, 23:16:16.589
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
25 Jul, 23:16:16.589
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
25 Jul, 23:16:16.589
django.db.utils.OperationalError: could not connect to server: Connection refused
25 Jul, 23:16:16.589
Is the server running on host "z2bdea11f-postgresql.qovery.io" (45.55.124.49) and accepting
25 Jul, 23:16:16.589
TCP/IP connections on port 5432?

This error is on running python manage.py migrate command after deploying the app.

settings.py database config

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': os.environ.get('QOVERY_POSTGRESQL_Z2BDEA11F_DEFAULT_DATABASE_NAME', 'postgres),
    'USER': os.environ.get('QOVERY_POSTGRESQL_Z2BDEA11F_LOGIN', 'postgres'),
    'PASSWORD': os.environ.get('QOVERY_POSTGRESQL_Z2BDEA11F_PASSWORD'),
    'HOST': os.environ.get('QOVERY_POSTGRESQL_Z2BDEA11F_HOST', 'localhost'),
    'PORT': os.environ.get('QOVERY_POSTGRESQL_Z2BDEA11F_PORT', 5432)
  }
}

Dockerfile content (if any)

FROM python:3.8.0-alpine

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apk update && apk add postgresql-dev zlib-dev jpeg-dev gcc python3-dev musl-dev
RUN apk add git

RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
COPY example /usr/src/app/
COPY entrypoint.sh /usr/src/app
RUN chmod +x ./entrypoint.sh

EXPOSE 8000

CMD ["./entrypoint.sh"]

entrypoint.sh contents

#!/bin/sh
set -euo pipefail

python manage.py migrate

exec python manage.py runserver 0.0.0.0:8000

Thank you

Issue got solved on its own, I redeployed the app and its running.