1. 创建只读用户:
create user test_user with encrypted password '12345678ab!';
2. 设置默认事务只读:
alter user test_user set default_transaction_read_only=on;
3. 赋予用户连接数据库mytest的权限:
grant CONNECT on DATABASE mytest to test_user ;
4. 切换到指定库mytest:
\c mytest system
5. 赋予用户表、序列查看权限,进入指定db运行:
5.1. 把当前库现有的所有在public这个schema下的表的使用权限赋给用户readonly
GRANT USAGE ON SCHEMA public to test_user;
5.2. 默认把当前库之后新建在public这个schema下的表的使用权限赋给readonly
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO test_user;
5.3. 赋予用户readonly所有public下的序列的查看权
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO test_user;
5.4. 赋予用户readonly所有public下的表的select权
GRANT SELECT ON ALL TABLES IN SCHEMA public TO test_user;