Postgres用户创建数据库的权限

本文详细介绍了在Postgres中管理用户创建数据库权限的过程。首先展示了超级用户highgo具有创建数据库的权限,而普通用户a则没有。接着通过`alter user`命令赋予了用户a创建数据库的权限,并验证了用户a可以成功创建数据库。最后,虽然收回了用户a的创建数据库权限,但将其设为超级用户后,a依然能够创建数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用户创建数据库的权限

(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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值