postgresql db_user_namespace parameter

在PostgreSQL中,通过db_user_namespace参数可以实现用户+数据库的认证。默认情况下这个参数是关闭的,关闭状态下创建的用户属于全局用户。

 

下面是自己的操作记录

mysql@pttest4 data]$ psql
Welcome to psql 8.2.16, the PostgreSQL interactive terminal.

Type:  /copyright for distribution terms
       /h for help with SQL commands
       /? for help with psql commands
       /g or terminate with semicolon to execute query
       /q to quit

mysql=# show db_user_namespace;
 db_user_namespace
-------------------
 off

(1 row)

 

这个参数默认是OFF的,在POSTGRESQL.CONF文件里把它设置为ON,然后重新加载配置文件

[mysql@pttest4 data]$ pg_ctl -D `pwd` reload
server signaled

 

创建角色aa

mysql=# create role "aa@hivedb" nosuperuser nocreatedb nocreaterole noinherit login;
CREATE ROLE

 

连接数据库试试

[mysql@pttest4 data]$ psql -d postgres -U aa
psql: FATAL:  role "aa@postgres" does not exist

由于我们在创建角色aa的时候是和hivedb这个DATABASE绑定的,而不是POSTGRES,因此会提示"aa@postgres"不存在

 

[mysql@pttest4 data]$ psql -d hivedb -U aa
Welcome to psql 8.2.16, the PostgreSQL interactive terminal.

Type:  /copyright for distribution terms
       /h for help with SQL commands
       /? for help with psql commands
       /g or terminate with semicolon to execute query
       /q to quit

hivedb=>

这样就可以连接上了。

 

但是目前这只是一个过渡的功能,因为这个参数关闭后,这个用户又会变成全局用户。创建的per database认证用户实质上就是名字改变了,在关闭db_user_namespace 后和全局用户没其他区别。下面我们把db_user_namespace重新置为OFF,试试

[mysql@pttest4 data]$ psql -d hivedb -U aa
psql: FATAL:  role "aa" does not exist

提示角色aa并不存在,为什么呢?刚才不是明明创建了吗?其实前面创建的是一个叫"aa@hivedb"的角色。

mysql=# /du
                               List of roles
 Role name | Superuser | Create role | Create DB | Connections | Member of
-----------+-----------+-------------+-----------+-------------+-----------
 aa@hivedb | no        | no          | no        | no limit    |
 mysql     | yes       | yes         | yes       | no limit    |
 test      | no        | no          | no        | no limit    |
(3 rows)

 

[mysql@pttest4 data]$ psql -d hivedb -U aa@hivedb
Welcome to psql 8.2.16, the PostgreSQL interactive terminal.

Type:  /copyright for distribution terms
       /h for help with SQL commands
       /? for help with psql commands
       /g or terminate with semicolon to execute query
       /q to quit

hivedb=> /q
[mysql@pttest4 data]$ psql -d mysql -U aa@hivedb
Welcome to psql 8.2.16, the PostgreSQL interactive terminal.

Type:  /copyright for distribution terms
       /h for help with SQL commands
       /? for help with psql commands
       /g or terminate with semicolon to execute query
       /q to quit

mysql=>

 

我们指定"aa@hivedb"作为用户名,就可以像一个全局的ROLE一样连接各个DATABASE了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值