外国服务器的权限被拒绝

Sha*_*aun 12 postgresql postgresql-fdw

我正在尝试设置一个具有有限权限的用户,该用户能够创建外部表。我有两个数据库,hr_dbaccounting_db. 我已经创建了一个hr_user用户hr_db和一个accounting_user用户accounting_db。我只希望accounting_user用户对某些hr_db表(例如表)具有选择权限users。为此,作为超级用户,我转到hr_db数据库并运行:

GRANT CONNECT ON DATABASE hr_db TO accounting_user;
GRANT SELECT ON people TO accounting_user;
Run Code Online (Sandbox Code Playgroud)

我设置了一个连接hr_dbaccounting_db使用外国数据包装:

CREATE SERVER hr_db FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'hr_db', port '5432');
Run Code Online (Sandbox Code Playgroud)

然后我为accounting_user用户添加了一个映射:

CREATE USER MAPPING FOR accounting_user SERVER hr_db
OPTIONS (user 'accounting_user', password 'secretpassword');
Run Code Online (Sandbox Code Playgroud)

密码accounting_user与我用于从命令行登录的密码相同。这工作正常:

psql -U accounting_user -W hr_db
[enter accounting_user password]
SELECT * FROM people LIMIT 10;
Run Code Online (Sandbox Code Playgroud)

我可以accounting_db作为accounting_user用户在数据库中创建一个常规表:

psql -U accounting_user -W accounting_db
[enter accounting_user password]
CREATE TABLE test (person_id integer NOT NULL);
DROP TABLE test;
Run Code Online (Sandbox Code Playgroud)

但是如果我尝试创建一个外部表:

CREATE FOREIGN TABLE hr_people (person_id integer NOT NULL)
SERVER hr_db OPTIONS (table_name 'people');
ERROR:  permission denied for foreign server hr_db
Run Code Online (Sandbox Code Playgroud)

作为超级用户,我可以创建hr_people外部表并且accounting_user可以访问它。所以外部数据连接hr_db似乎是正确的。accounting_user为了能够创建和删除外部表,我还需要提供什么?

小智 12

为外部服务器授予权限:

GRANT USAGE ON FOREIGN SERVER hr_db TO accounting_user;
Run Code Online (Sandbox Code Playgroud)

官方页面https://www.postgresql.org/docs/9.6/static/contrib-dblink-connect.html上的示例中提供了更多详细信息