投稿/コメントを表示します。

digital-ocianで python manage.py migrate まで来ましたが、以下のコードでエラーになってしまいます。
python manage.py migrate
Operations to perform:
  Apply all migrations: account, accounts, admin, auth, contenttypes, log, sessions, sites, socialaccount
Running migrations:
Traceback (most recent call last):
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
psycopg.errors.InsufficientPrivilege: permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA...
                     ^

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

Traceback (most recent call last):
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/migrations/recorder.py", line 70, in ensure_schema
    editor.create_model(self.Migration)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/base/schema.py", line 451, in create_model
    self.execute(sql, params or None)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/postgresql/schema.py", line 45, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/base/schema.py", line 201, in execute
    cursor.execute(sql, params)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/backends/utils.py", line 87, in _execute
    return self.cursor.execute(sql)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/psycopg/cursor.py", line 97, in execute
    raise ex.with_traceback(None)
django.db.utils.ProgrammingError: permission denied for schema public
LINE 1: CREATE TABLE "django_migrations" ("id" bigint NOT NULL PRIMA...
                     ^

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/webapp/mysite2/manage.py", line 22, in <module>
    main()
  File "/home/webapp/mysite2/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/base.py", line 106, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 356, in handle
    post_migrate_state = executor.migrate(
                         ^^^^^^^^^^^^^^^^^
  File "/home/webapp/mysite2/venv/lib/python3.12/site-packages/django/db/migrations/executor.py", line 107, in migrate
    self.recorder.ensure_schema()
  File "/home/webapp/mysite2/venv/lib/python3.12/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...
                     ^)

最初は自作したアプリの設定がまずいのかと思いましたが、 git clone https://github.com/k-brahma/django_photo_diary.git . で実行しても同様のエラーになってしまいます。

.envは
SECRET_KEY=xxxxxxxx

ALLOWED_HOSTS=xxx.xxx.xxx.xxx

DB_NAME=django_mysite_db
DB_USER=django_user
DB_PASSWORD=mysiteUserPass123
DB_HOST=localhost
DB_PORT=5432

EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_HOST_USER=あなたのGMAILアドレス
EMAIL_HOST_PASSWORD=生成されたアプリパスワード
EMAIL_USE_TLS=True

DEFAULT_FROM_EMAIL=あなたのGMAILアドレス

をほぼ丸写し、データベースも
CREATE DATABASE django_mysite_db;
CREATE USER django_user WITH PASSWORD 'mysiteUserPass123';
GRANT ALL PRIVILEGES ON DATABASE django_mysite_db TO django_user;

で作成しているつもりなのですが、、、
sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Sun 2024-12-15 13:35:51 UTC; 8min ago
    Process: 3298 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 3298 (code=exited, status=0/SUCCESS)
        CPU: 2ms

Dec 15 13:35:51 study-manage-server systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Dec 15 13:35:51 study-manage-server systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.

python manage.py check
System check identified no issues (0 silenced).

という感じです。

claudeに聞いてみたところ、
-- データベースユーザーに public スキーマの権限を付与
GRANT ALL ON SCHEMA public TO your_db_user;

-- データベースの所有権を付与
ALTER DATABASE your_database OWNER TO your_db_user;

とのこと。ユーザー名を変え、実行し、postgresqlを再起動しても同様のエラーになってしまいます。

どうしたらいいでしょうか?
2024/12/15 22:47