Skip to content

Permission 拒绝

错误信息

                permission 拒绝 for 表
              

快速摘要

数据库用户你 是已连接 as 做不 有必要 privileges (SELECT, INSERT, UPDATE, DELETE) on 表.

为什么发生

  • 用户有 不是 granted 必需 privilege

  • 你是 已连接 as 用户使用有限 permissions

  • privilege 是 revoked

  • 用户有 privileges on 不同数据库 or Schema

最小示例

✗ 错误代码
-- User 'reader' tries to insert
INSERT INTO users (name) VALUES ('Ada');
-- → permission denied for table users
✓ 修复代码
-- Grant insert privilege first
GRANT INSERT ON users TO reader;
INSERT INTO users (name) VALUES ('Ada');

用户 lacked INSERT permission. Granting 它 (as superuser) 允许 insert 到 proceed.

如何诊断

  • 检查用户's privileges: \du 用户名 in psql or 查询 information_schema

  • 验证你 是已连接 as 正确用户

  • 检查如果 privilege 是 granted on 正确表/Schema/数据库

如何修复

  • Grant 必需 privilege: GRANT SELECT ON 表 TO 用户

  • 连接 as 用户使用 sufficient privileges

  • Grant privileges on Schema: GRANT USAGE ON SCHEMA Schema TO 用户

如何预防

  • 设置 roles 使用 appropriate privileges and assign 用户到 roles

  • Document 哪个用户需要哪个 privileges

  • Use 最少-privilege 原则 — grant 仅什么是 需要

相关资源

相关术语

相关课程

← 返回语言