✅ 切换到你的数据库并以超级用户登录(例如 postgres):
admin#localhost: ~$ psql -U postgres -d lily
✅ 创建登录的账号机密吗
lily=> CREATE USER readonly_user WITH PASSWORD 'xxxxxxxxxxx';
✅ 确认你授予了这个表的读取权限:
lily=> GRANT SELECT ON actor_codes TO readonly_user;
✅ 如果有多个表要一起授权:
lily=> GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;
✅ 确保 schema 权限也没问题(你可能已经做过):
lily=> GRANT USAGE ON SCHEMA public TO readonly_user;
🔁 可选:一劳永逸让未来新表也有权限
lily=> ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO readonly_user;
🔍 验证权限是否生效
✅ 重新用 readonly_user 登录测试:
admin#localhost: ~$ psql -U readonly_user -d lily
lily=> SELECT * FROM actor_codes LIMIT 1;
cid | code
-------------------------------+---------------
sfefesfwsafe| kil/1/acc
(1 row)
如果这样之后还报错,可以检查:
-- 看表归属 schema(假如不是 public,需要改 GRANT 语句里的 schema 名)
lily=> SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name = 'actor_codes';
table_schema | table_name
--------------+-------------
public | actor_codes
(1 row)