エラー: Postgres を使用したシーケンス cities_id_seq の権限が拒否されました 質問する

エラー: Postgres を使用したシーケンス cities_id_seq の権限が拒否されました 質問する

データベースで次の SQL スクリプトを実行しました。

create table cities (
id serial primary key,
name text not null
);

create table reports (
id serial primary key,
cityid integer not null references cities(id),
reportdate date not null,
reporttext text not null
);

create user www with password 'www';

grant select on cities to www;
grant insert on cities to www;
grant delete on cities to www;

grant select on reports to www;
grant insert on reports to www;
grant delete on reports to www;

grant select on cities_id_seq to www;
grant insert on cities_id_seq to www;
grant delete on cities_id_seq to www;

grant select on reports_id_seq to www;
grant insert on reports_id_seq to www;
grant delete on reports_id_seq to www;

ユーザー www が次の操作を実行しようとすると:

insert into cities (name) values ('London');

次のエラーが発生します:

ERROR: permission denied for sequence cities_id_seq

問題はシリアル タイプにあることはわかっています。そのため、*_id_seq の選択、挿入、削除の権限を www に付与しました。それでも問題は解決しません。何が足りないのでしょうか?

ベストアンサー1

PostgreSQL 8.2 以降では以下を使用する必要があります。

GRANT USAGE, SELECT ON SEQUENCE cities_id_seq TO www;

GRANT USAGE - シーケンスの場合、この権限により currval および nextval 関数の使用が許可されます。

また、コメントで @epic_fil が指摘しているように、次のようにしてスキーマ内のすべてのシーケンスに権限を付与できます。

GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO www;

注意\c <database_name>:権限付与コマンドを実行する前に、データベース( )を選択することを忘れないでください。

おすすめ記事