PostgreSQL用户角色及其属性介绍


1.CREATE ROLE创建的用户默认不带LOGIN属性,而CREATE USER创建的用户默认带有LOGIN属性,如下:

  1. postgres=# CREATE ROLE pg_test_user_1; /*默认不带LOGIN属性*/  
  2. CREATE ROLE  
  3. postgres=# CREATE USER pg_test_user_2; /*默认具有LOGIN属性*/  
  4. CREATE ROLE  
  5. postgres=# \du  
  6.                List of roles  
  7.    Role name    |  Attributes  | Member of  
  8. ----------------+--------------+-----------  
  9.  pg_test_user_1 | Cannot login | {}  
  10.  pg_test_user_2 |              | {}  
  11.  postgres       | Superuser    | {}  
  12.                 : Create role  
  13.                 : Create DB  
  14.   
  15. postgres=#   

2.在创建用户时赋予角色属性

  1. postgres=# CREATE  ROLE pg_test_user_3 CREATEDB;   /*具有创建数据库的属性*/  
  2. CREATE ROLE  
  3. postgres=# \du  
  4.                List of roles  
  5.    Role name    |  Attributes  | Member of  
  6. ----------------+--------------+-----------  
  7.  pg_test_user_1 | Cannot login | {}  
  8.  pg_test_user_2 |              | {}  
  9.  pg_test_user_3 | Create DB    | {}  
  10.                 : Cannot login  
  11.  postgres       | Superuser    | {}  
  12.                 : Create role  
  13.                 : Create DB  
  14.   
  15. postgres=# CREATE ROLE pg_test_user_4 CREATEDB PASSWORD '123456'; /*具有创建数据库及带有密码登陆的属性 */    
  16. CREATE ROLE  
  17. postgres=# \du  
  18.                List of roles  
  19.    Role name    |  Attributes  | Member of  
  20. ----------------+--------------+-----------  
  21.  pg_test_user_1 | Cannot login | {}  
  22.  pg_test_user_2 |              | {}  
  23.  pg_test_user_3 | Create DB    | {}  
  24.                 : Cannot login  
  25.  pg_test_user_4 | Create DB    | {}  
  26.                 : Cannot login  
  27.  postgres       | Superuser    | {}  
  28.                 : Create role  
  29.                 : Create DB  
  30.   
  31. postgres=#  

3.给已存在用户赋予各种权限

使用ALTER ROLE即可。

  1. postgres=# \du  
  2.                List of roles  
  3.    Role name    |  Attributes  | Member of  
  4. ----------------+--------------+-----------  
  5.  pg_test_user_3 | Create DB    | {}  
  6.                 : Cannot login  
  7.  pg_test_user_4 | Create DB    | {}  
  8.                 : Cannot login  
  9.  postgres       | Superuser    | {}  
  10.                 : Create role  
  11.                 : Create DB  
  12.   
  13. postgres=# ALTER ROLE pg_test_user_3 WITH LOGIN; /*赋予登录权限*/  
  14. ALTER ROLE  
  15. postgres=# \du  
  16.                List of roles  
  17.    Role name    |  Attributes  | Member of  
  18. ----------------+--------------+-----------  
  19.  pg_test_user_3 | Create DB    | {}  
  20.  pg_test_user_4 | Create DB    | {}  
  21.                 : Cannot login  
  22.  postgres       | Superuser    | {}  
  23.                 : Create role  
  24.                 : Create DB  
  25.   
  26. postgres=# ALTER ROLE pg_test_user_4 WITH CREATEROLE;/*赋予创建角色的权限*/  
  27. ALTER ROLE  
  28. postgres=# \du  
  29.                List of roles  
  30.    Role name    |  Attributes  | Member of  
  31. ----------------+--------------+-----------  
  32.  pg_test_user_3 | Create DB    | {}  
  33.  pg_test_user_4 | Create role  | {}  
  34.                 : Create DB  
  35.                 : Cannot login  
  36.  postgres       | Superuser    | {}  
  37.                 : Create role  
  38.                 : Create DB  
  39.   
  40. postgres=# ALTER ROLE pg_test_user_4 WITH PASSWORD '654321';/*修改密码*/  
  41. ALTER ROLE  
  42. postgres=# ALTER ROLE pg_test_user_4 VALID UNTIL 'JUL 7 14:00:00 2012 +8'; /*设置角色的有效期*  
  43. ALTER ROLE  

4.查看角色表中的信息:

  1. postgres=# SELECT * FROM pg_roles;  
  2.     rolname     | rolsuper | rolinherit | rolcreaterole | rolcreatedb | rolcatupdate | rolcanlogin | rolconnlimit | rolpassword |     rolvaliduntil      | rol  
  3. config |  oid  
  4. ----------------+----------+------------+---------------+-------------+--------------+-------------+--------------+-------------+------------------------+----  
  5. -------+-------  
  6.  postgres       | t        | t          | t             | t           | t            | t           |           -1 | ********    |                        |  
  7.        |    10  
  8.  pg_test_user_3 | f        | t          | f             | t           | f            | t           |           -1 | ********    |                        |  
  9.        | 16390  
  10.  pg_test_user_4 | f        | t          | t             | t           | f            | f           |           -1 | ********    | 2012-07-07 14:00:00+08 |  
  11.        | 16391  
  12. (3 rows)  
  13.   
  14. postgres=#  

5.ALTER ROLE语句简介:

  1. ALTER ROLE  
  2. 名称  
  3. ALTER ROLE -- 修改一个数据库角色  
  4. 语法  
  5. ALTER ROLE name [ [ WITH ] option [ ... ] ]  
  6.   
  7. 这里的 option 可以是:  
  8.       
  9.       SUPERUSER | NOSUPERUSER  
  10.     | CREATEDB | NOCREATEDB  
  11.     | CREATEROLE | NOCREATEROLE  
  12.     | CREATEUSER | NOCREATEUSER  
  13.     | INHERIT | NOINHERIT  
  14.     | LOGIN | NOLOGIN  
  15.     | CONNECTION LIMIT connlimit  
  16.     | [ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password'  
  17.     | VALID UNTIL 'timestamp'   
  18.   
  19. ALTER ROLE name RENAME TO newname  
  20.   
  21. ALTER ROLE name SET configuration_parameter { TO | = } { value | DEFAULT }  
  22. ALTER ROLE name RESET configuration_parameter描述  
  23. ALTER ROLE 修改一个数据库角色的属性。  

相关内容