PostgreSQL使用者角色及其屬性介紹

來源:互聯網
上載者:User

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=#  
postgres=# CREATE ROLE pg_test_user_3 CREATEDB; /*具有建立資料庫的屬性*/CREATE ROLEpostgres=# \du List of roles Role name | Attributes | Member of----------------+--------------+----------- pg_test_user_1 | Cannot login | {} pg_test_user_2 | | {} pg_test_user_3 | Create DB | {} : Cannot login postgres | Superuser | {} : Create role : Create DBpostgres=# CREATE ROLE pg_test_user_4 CREATEDB PASSWORD '123456'; /*具有建立資料庫及帶有密碼登陸的屬性 */ CREATE ROLEpostgres=# \du List of roles Role name | Attributes | Member of----------------+--------------+----------- pg_test_user_1 | Cannot login | {} pg_test_user_2 | | {} pg_test_user_3 | Create DB | {} : Cannot login pg_test_user_4 | Create DB | {} : Cannot login postgres | Superuser | {} : Create role : Create DBpostgres=#

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 修改一個資料庫角色的屬性。  

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.