postgresql-用户管理

1、连接数据库

远程连接

$ psql -U postgres -h 192.168.3.105 -d postgres -p 1921 -W

允许远程连接需要配置以下两个配置

配置监听地址

$ vim /pgdata/postgresql.conf
listen_addresses = '0.0.0.0'

客户端认证配置

$ vim /pgdata/pg_hba.conf
host    all             all             0.0.0.0/0               md5
2、用户管理

在 pg 中用户和角色是没有区别的,唯一的不同就是创建角色默认没有登录权限,需手动赋予。

列出数据库中的用户

postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

创建用户,常用的选项有

  • superuser | nosuperuser 用户是否为超级用户
  • createdb | nocreatedb 用户是否有create database 的权限
  • createrole | nocreaterole 用户受否有创建角色的权限
  • inherit | noinherit 角色是否继承其他角色的权限
  • login | nologin 角色是否有登录权限
  • connection limit n :限制角色并发连接数量,默认是 -1 ,没有限制
postgres=# create role test with superuser login password 'test';
CREATE ROLE
postgres=# create role test1 with login password 'test1';
CREATE ROLE

修改用户

postgres=# alter role test1 with superuser login password 'test1';
ALTER ROLE

删除用户

postgres=# drop role test1;
DROP ROLE
3、权限管理

权限级别

  • cluster 权限,实例权限通过 pg_hba.conf 配置

  • database 权限,grant create on database test to test;

  • schema 权限,alter schema test_schema owner to test; grant select,insert,update,delete on all tables in schema test_schema to test;

  • object 权限,grant select,insert,update,delete on a.b to user;

查看权限信息

postgres=# select * from pg_roles;
test=# select * from information_schema.table_privileges;
posted @   原来是你~~~  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示