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

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...