在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了。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|