1. 程式人生 > >Postgres使用者建立資料庫的許可權

Postgres使用者建立資料庫的許可權

使用者建立資料庫的許可權

(1)檢查確認超級使用者highgo擁有建立資料庫的許可權,普通使用者a沒有建立資料庫的許可權

highgo=#\du

                             List of roles

 Role name |                   Attributes                   | Member of

-----------+------------------------------------------------+-----------

 a        |                                               | {}

 highgo   | Superuser, Create role, Create DB, Replication | {}

highgo=#select current_user;

 current_user

--------------

 highgo

(1row)

highgo=#create database highgo_t;

CREATEDATABASE

highgo=#\c highgo a

Youare now connected to database "highgo" as user "a".

highgo=>create database a_t;

錯誤:  建立資料庫許可權不夠

highgo=>

(2)賦予普通使用者a建立資料庫的許可權,但並未賦予超級使用者許可權,此時使用者a可建立資料庫

highgo=#alter user a createdb ;

ALTERROLE

highgo=#\du

                             List of roles

 Role name |                   Attributes                   | Member of

-----------+------------------------------------------------+-----------

 a        | Create DB                                      | {}

 highgo   | Superuser, Create role, Create DB, Replication | {}

highgo=#\c highgo a

Youare now connected to database "highgo" as user "a".

highgo=>create database a_t;

CREATEDATABASE

highgo=>\l

                              List of databases

   Name   | Owner  | Encoding |  Collate  |   Ctype    | Access privileges

-----------+--------+----------+------------+------------+-------------------

 a_t      | a      | UTF8     | zh_CN.utf8 | zh_CN.utf8 |

 highgo   | highgo | UTF8     | zh_CN.utf8 |zh_CN.utf8 | =Tc/highgo       +

           |        |          |            |            | highgo=CTc/highgo

 highgo_t | highgo | UTF8     | zh_CN.utf8 |zh_CN.utf8 |

 template0 | highgo | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +

           |        |          |            |            | highgo=CTc/highgo

 template1 | highgo | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +

           |        |          |            |            | highgo=CTc/highgo

(5rows)

(3)賦予使用者a超級使用者的許可權,但並未單獨賦予建立資料庫的許可權,此時使用者a可建立資料庫

highgo=#alter user a nocreatedb ;

ALTERROLE

highgo=#\du

                             List of roles

 Role name |                   Attributes                   | Member of

-----------+------------------------------------------------+-----------

 a        |                                               | {}

 highgo   | Superuser, Create role, Create DB, Replication | {}

highgo=#alter user a superuser ;

ALTERROLE

highgo=#\du

                             List of roles

 Role name |                   Attributes                   | Member of

-----------+------------------------------------------------+-----------

 a         | Superuser                                      | {}

 highgo   | Superuser, Create role, Create DB, Replication | {}

highgo=#\c highgo a

Youare now connected to database "highgo" as user "a".

highgo=#create database a_tb;

CREATEDATABASE

highgo=#\l

                              List of databases

   Name   | Owner  | Encoding |  Collate  |   Ctype    | Access privileges

-----------+--------+----------+------------+------------+-------------------

 a_t      | a      | UTF8     | zh_CN.utf8 | zh_CN.utf8 |

 a_tb     | a      | UTF8     | zh_CN.utf8 | zh_CN.utf8 |

 highgo   | highgo | UTF8     | zh_CN.utf8 |zh_CN.utf8 | =Tc/highgo       +

           |        |          |            |            | highgo=CTc/highgo

 highgo_t | highgo | UTF8     | zh_CN.utf8 | zh_CN.utf8 |

 template0 | highgo | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +

           |        |          |            |            | highgo=CTc/highgo

 template1 | highgo | UTF8     | zh_CN.utf8 | zh_CN.utf8 | =c/highgo        +

           |        |          |            |            | highgo=CTc/highgo

(6rows)