sql - 错误 : permission denied for sequence cities_id_seq using Postgres

标签 sql postgresql permissions auto-increment

我在我的数据库上运行了以下 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。但这并不能解决我的问题。我错过了什么?

最佳答案

从 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>)

关于sql - 错误 : permission denied for sequence cities_id_seq using Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9325017/

相关文章:

sql - PostgreSQL 错误,没有给出唯一约束匹配

windows - 您需要 "Myself"的权限才能删除文件夹 - 如何修复

android - 在 android marshmallow 中未授予所有权限

mysql - 为什么 MySQL 会发出文件排序?

postgresql - 如何在 psql 中获取多个函数?

mysql - 用于查找具有特定属性的项目和相关非外键项目的 SQL 查询

ruby-on-rails - 查看Rails数据库登录开发

rust - 如何确保在Linux上以root权限运行Rust可执行文件?

mysql - 如何用 JOIN 替换 NOT EXISTS?

mysql - 如何通过交换两列值来更新MySQL表?