CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA
Asked Answered
V

8

13
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

Vicinage answered 27/10, 2022 at 5:39 Comment(0)
C
22

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>;
Caseycash answered 20/2, 2023 at 16:53 Comment(0)
R
7

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>;
Randalrandall answered 9/12, 2022 at 4:19 Comment(0)
B
4

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>;

Bandsman answered 15/3, 2023 at 21:7 Comment(0)
M
1

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

Melainemelamed answered 2/11, 2022 at 16:3 Comment(0)
C
1

I got this error many times when dealing with postgres in Django.

Here is how you can solve it. Go to your terminal and switch to postgres user

sudo su posgres & psql

Make sure you select your database first before running the commands

postgres=# \c my_db;

You are now connected to database "my_db" as user "postgres".

Then grant the privilege to your user

my_db=# GRANT ALL ON SCHEMA public TO my_user;
GRANT
Contracture answered 4/3 at 5:33 Comment(0)
K
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;

Kong answered 24/3, 2023 at 16:20 Comment(0)
Y
0

The missing link for me was that you need to give access to the public schema. I do not see this documented anywhere but here.

GRANT ALL ON SCHEMA public TO <db_user>;
Ylem answered 13/2 at 15:22 Comment(0)
C
0

Thanks to update Postgresql 15 now it become a problem during deploy! use this command

ALTER DATABASE your_database OWNER TO user_name; 

this option work for sure, have downsides but work.

Cleruchy answered 20/7 at 2:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.