微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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了。

原文地址:https://www.jb51.cc/postgresql/196923.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐