postgresql db_user_namespace parameter

前端之家收集整理的这篇文章主要介绍了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.f2er.com/postgresql/196923.html

猜你在找的Postgre SQL相关文章