Hi,

I followed both of these document

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04

for setting up our production hosting server but this doesn’t describe about productions apps. The reason why because Postgres uses ‘public’ schema for newly created users. The question I’ve now is what are the correct steps for Users and Roles for postgres users for production based DJango applications or any other applications?

I currently have these settings for our App Database

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 vikreya   |                                                            | {}


postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 vikreya   | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =Tc/postgres         +
           |          |          |         |         | postgres=CTc/postgres+
           |          |          |         |         | vikreya=CTc/postgres
(4 rows)

postgres=# 

but running our application is throwing errors as follows.

(vkenv) shami@ubuntu-webapp-01:~/vkproject$ python manage.py loaddata vikreya120319afterdroppingcron.json
Installed 82150 object(s) from 1 fixture(s)
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ 
(vkenv) shami@ubuntu-webapp-01:~/vkproject$
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ python manage.py makemigrations
No changes detected
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ 
(vkenv) shami@ubuntu-webapp-01:~/vkproject$
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pages, sessions
Running migrations:
  Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "name" of relation "django_content_type" does not exist


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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 124, in _alter_field
    new_db_params, strict,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 685, in _alter_field
    params,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "name" of relation "django_content_type" does not exist

$ 
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ python manage.py loaddata vikreya120319afterdroppingcron.json
Installed 82150 object(s) from 1 fixture(s)
(vkenv) shami@ubuntu-webapp-01:~/vkproject$ python manage.py migrate --fake-initial
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pages, sessions
Running migrations:
  Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "name" of relation "django_content_type" does not exist


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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 124, in _alter_field
    new_db_params, strict,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 685, in _alter_field
    params,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "name" of relation "django_content_type" does not exist

(vkenv) shami@ubuntu-webapp-01:~/vkproject$ packet_write_wait: Connection to 206.189.78.157 port 22: Broken pipe
RMs-MacBook-Pro:~ ram.mullapudi$ 

Best regards

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

As a next step I tried the following but the result is is same.

  1. Deleted/Dropped ‘djangocontenttype’ table from database
  2. I upgraded Django to version 3.0 from 2.2.7 in my virtual environment and ran the following commands
1. $ python manage.py migrate --fake
2. $ python manage.py migrate --fake--initial
3. $ python manage.py migrate

But I still see same error

django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...

Please let me know if you have any clue on this?

Best regards

I solved this issue as follows:

Restore the database in Postgres database (used pgAdmin tool for this)
(virtualenv)python manage.py loaddata dumpfile.json
Dropping django_migrations table from database (used pgAdmin tool for this)
(virtualenv)python manage.py makemigrations
(virtualenv)python manage.py migrate –fake
(virtualenv)python manage.py migrate
(virtualenv)python manage.py collectstatic
(virtualenv)python manage.py runserver 0.0.0.0:8000

Cheers!

Submit an Answer