6
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/p.gauthamprasad/Downloads/pb/pbapp/manage.py", line 22, in <module>
    main()
  File "/Users/p.gauthamprasad/Downloads/pb/pbapp/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 349, in handle
    post_migrate_state = executor.migrate(
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 107, in migrate
    self.recorder.ensure_schema()
  File "/Users/p.gauthamprasad/Downloads/pb/pbvenv/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 72, in ensure_schema
    raise MigrationSchemaMissing(
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA...
                     ^
)

I have tried this "command python3 manage.py migrate" after creating database and linking in settings.py in this way...:-

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<name>',
        'USER': '<username>',
        'PASSWORD': 'admin',
        'HOST': 'localhost',
        'PORT': '5432',

    }
}

i have tried grant "commands" in psql there is no use

5 Answers5

8

Applying the following command solves it for me without giving the new user all privileges, just setting him as the owner of the project db.

ALTER DATABASE <db_name> OWNER TO <db_user>;
Kop3sh
  • 179
  • 3
  • 7
5

I faced the exact issue several times and solved by providing permission every single time. Anyone who stumbled in same scenario can try below command in psql CLI:

GRANT postgres TO <user>;
Muhammmed Nihad
  • 161
  • 1
  • 1
  • 12
2

This database access error. This is a step by step guide.

  1. Create a new role

    CREATE USER <user name> WITH PASSWORD <'password'> CREATEDB;

  2. Create a database and specify its owner

    CREATE DATABASE <database name> WITH OWNER <user name>;

If the database was created earlier and you need to change the owner, use the command

`ALTER DATABASE <database name> OWNER TO <user name>;`
  1. Give the user all rights to the database

    GRANT ALL PRIVILEGES ON DATABASE <db name> TO <user name>;

Саша
  • 21
  • 2
0

If you are on windows this might help you.

Open PG-ADMIN login to local server. enter image description here

Right click on your user. click on properties & select the privileges tab. Select the superuser option for your user as follows. enter image description here

And run your migrations and migrate command again it should work.

If your are on linux you have to check for the permissions. You can follow below link

https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-20-04

Arun
  • 3,440
  • 1
  • 11
  • 19
0

I got this error, after deleting the data, by running the command DROP SCHEMA public CASCADE; and I resolved it by creating the missing schema CREATE SCHEMA public;

bbahd
  • 49
  • 8