创建表“django_migrations”(“id”bigint NOT NULL PRIMA

P.G*_*sad 9 psql django-migrations

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...
                     ^
)

Run Code Online (Sandbox Code Playgroud)

在创建数据库并以这种方式链接到 settings.py 后,我尝试了这个“命令 python3 manage.py migrate”...:-

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

    }
}

Run Code Online (Sandbox Code Playgroud)

我尝试过在 psql 中授予“命令”,但没有用

小智 16

应用以下命令为我解决了这个问题,而无需授予新用户所有权限,只需将他设置为项目数据库的所有者即可。

ALTER DATABASE <db_name> OWNER TO <db_user>;
Run Code Online (Sandbox Code Playgroud)


Muh*_*had 6

我多次遇到这个确切的问题,并且每次都通过提供许可来解决。任何遇到相同情况的人都可以在 psql CLI 中尝试以下命令:

GRANT postgres TO <user>;
Run Code Online (Sandbox Code Playgroud)


小智 1

如果您使用的是 Windows,这可能会对您有所帮助。

打开PG-ADMIN登录本地服务器。 在此输入图像描述

右键单击您的用户。单击属性并选择权限选项卡。为您的用户选择超级用户选项,如下所示。 在此输入图像描述

并再次运行您的迁移和迁移命令,它应该可以工作。

如果您使用的是 Linux,则必须检查权限。您可以点击以下链接

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