title: Gunicorn: UndefinedTable: relation "user" does not exist
body: <p>Here is the scenario, I'm deploying the Django app on an ec2 instance. I'm following this guide <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04" rel="nofollow noreferrer">Digitalocean deployment guide</a>. when I run my app using <code>python manange.py runserver 0.0.0.0:800</code> and access the admin portal via http://server-ip-address:8000. everything works fine.
  But when I run <code>gunicorn --bind 0.0.0.0:8000 apbackend.wsgi</code> and try to access the admin page it gives me 500 server errors and exception:</p>
  <pre><code>UndefinedTable: relation &quot;user&quot; does not exist
  LINE 1: ...r&quot;, &quot;user&quot;.&quot;is_staff&quot;, &quot;user&quot;.&quot;is_superuser&quot; FROM &quot;user&quot; WHE...
  </code></pre>
  <p>Setting.py</p>
  <pre><code>INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'rest_framework_simplejwt.token_blacklist',
      'rest_framework_simplejwt',
      'corsheaders',
      'drf_yasg',
      'django_extensions',
      'src',
      'communications',
      'rest_framework',
      'registration',
  
  ]
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.middleware.clickjacking.XFrameOptionsMiddleware',
      'corsheaders.middleware.CorsMiddleware',
      # 'django.contrib.auth.middleware.AuthenticationMiddleware',
      
      
  ]
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.postgresql_psycopg2',
          'NAME': os.environ.get(&quot;DATABASE_NAME&quot;),
          'USER': os.environ.get(&quot;DATABASE_USER&quot;),
          'PASSWORD': os.environ.get(&quot;DATABASE_PASSWORD&quot;),
          'HOST': os.environ.get(&quot;DATABASE_HOST&quot;),
          'PORT': '5432',
      }
  }
  
  WSGI_APPLICATION = 'apbackend.wsgi.application'
  AUTH_USER_MODEL = 'registration.User'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [BASE_DIR / 'templates'],
          'APP_DIRS': True,
          'OPTIONS': {
              'context_processors': [
                  'django.template.context_processors.debug',
                  'django.template.context_processors.request',
                  'django.contrib.auth.context_processors.auth',
                  'django.contrib.messages.context_processors.messages',
              ],
          },
      },
  ]
  
  AUTH_PASSWORD_VALIDATORS = [
      {
          'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
      },
  ]
  
  REST_FRAMEWORK = {
      'DEFAULT_PERMISSION_CLASSES': (
          'rest_framework.permissions.IsAuthenticated',
          'rest_framework.permissions.IsAdminUser'
      ),
      'DEFAULT_AUTHENTICATION_CLASSES': (
          'rest_framework_simplejwt.authentication.JWTAuthentication',
          'rest_framework.authentication.SessionAuthentication',
          'rest_framework.authentication.BasicAuthentication',
          'rest_framework.authentication.TokenAuthentication',
  
      ),
  }
  
  </code></pre>
  <p>registration.models.py</p>
  <pre><code>from django.db import models
  
  # Create your models here.
  import uuid
  
  from django.db import models
  from django.contrib.auth.models import PermissionsMixin
  from django.contrib.auth.base_user import AbstractBaseUser
  from django.utils import timezone
  
  from .managers import CustomUserManager
  
  from rest_framework_simplejwt.tokens import RefreshToken
  
  AUTH_PROVIDERS = {'facebook': 'facebook', 'google': 'google',
                    'twitter': 'twitter', 'email': 'email'}
  # Create your models here.
  
  class User(AbstractBaseUser, PermissionsMixin):
      # These fields tie to the roles!
      A = 1
      I = 2
      C = 3
  
      USER_ROLE = (
          (A, 'A'),
          (I, 'I'),
          (C, 'C')
      )
  
      # Roles created here
      uuid = models.UUIDField(unique=True, editable=False, default=uuid.uuid4)
      username = models.CharField(max_length=255, unique=True, db_index=True)
      email = models.EmailField(unique=True)
      first_name = models.CharField(max_length=50, blank=True)
      last_name = models.CharField(max_length=50, blank=True)
      role = models.PositiveSmallIntegerField(choices=USER_ROLE, blank=True, null=True)
      date_joined = models.DateTimeField(auto_now_add=True)
      is_verified = models.BooleanField(default=False)
      is_active = models.BooleanField(default=True)
      is_deleted = models.BooleanField(default=False)
      created_date = models.DateTimeField(default=timezone.now)
      modified_date = models.DateTimeField(default=timezone.now)
      auth_provider = models.CharField(
          max_length=255, blank=False,
          null=False, default=AUTH_PROVIDERS.get('email'))
      is_staff = models.BooleanField(default=False)
      is_superuser = models.BooleanField(default=False)
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['username', 'role']
  
      objects = CustomUserManager()
  
      class Meta:
          db_table = 'user'
          verbose_name = 'user'
          verbose_name_plural = 'users'
  
      def __str__(self):
          return self.email
  
      def save(self, *args, **kwargs):
          self.is_staff = True if self.role == self.ADMIN else False
          self.is_superuser = True if self.role == self.ADMIN else False
          super(User, self).save(*args, **kwargs)
  
      def tokens(self):
          refresh = RefreshToken.for_user(self)
          return {
              'refresh': str(refresh),
              'access': str(refresh.access_token)
          }
  
  
  </code></pre>
  <p>I have spent a lot of time debugging this. still haven't found anything.</p>