Postgresql 只读角色和用户

标签 postgresql readonly

我找不到这个问题的答案:为什么在授予权限后从表中选择失败?

-- create new role
CREATE ROLE readonly;

-- grant access to all existing tables
GRANT CONNECT ON DATABASE shop TO readonly;
GRANT USAGE ON SCHEMA public TO readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO readonly;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO readonly;

-- grant access to all table which will be created in the future
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON SEQUENCES TO readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT EXECUTE ON FUNCTIONS TO readonly;

-- create user and grant role to this user
CREATE USER b_readonly WITH PASSWORD 'reAdOnLy123';
GRANT readonly TO b_readonly;

我从 db 得到的错误信息如下:

ERROR: permission denied for relation customer_search_query SQL state: 42501

Postgresql 9.6.5有什么新技巧吗?

最佳答案

如果 pg 版本 < 14 尝试:

postgres=# CREATE ROLE readaccess;
postgres=# CREATE USER read_user WITH PASSWORD 'read_password';
postgres=# GRANT readaccess TO read_user;
    
--- INPORTANT (select needed db)---
postgres=# \с your_db;
your_db=# GRANT CONNECT ON DATABASE your_db TO readaccess;
your_db=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;

如果 pg 版本 >= 14

 GRANT pg_read_all_data TO readaccess;

关于Postgresql 只读角色和用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48663891/

相关文章:

swift - 快速观察者

haskell - 如何以持久的只读模式打开 Sqlite3 数据库?

sql - 使用包含在 postgres psql 命令中的文件设置 search_path

php - 在 Windows 中使用 PHP 和 Postgres 开发 Web 应用程序与在 Linux 中开发 Web 应用程序相同吗?

java - 值不会被插入到数据库中,即使我没有收到任何错误

sql - 优化PG查询

database - PostgreSQL 覆盖继承的列

mysql - 只读/tmp 能否导致 "Unknown table engine ' InnoDB'"?

c# - 为什么 resharper 建议只读字段

perl - 如何使用 mod_perl 导出只读变量?