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 @ 2023-07-08 14:46  原来是你~~~  阅读(48)  评论(0)    收藏  举报