4、ERROR: permission denied for foreign-data wrapper postgres_fdw 需要给予权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
f001=>create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '10.0.0.196', port '26000',dbname 'testdb'); ERROR: permission denied forforeign-data wrapper postgres_fdw DETAIL: N/A f001=> f001=> \c f001 omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "omm". f001=# GRANT USAGE ONFOREIGN DATA WRAPPER postgres_fdw TO u_f001; GRANT f001=# \c f001 u_f001 Password foruser u_f001: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "u_f001". f001=>create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '10.0.0.196', port '26000',dbname 'testdb'); CREATE SERVER f001=>
00准备工作
在远端数据库创建用于数据库远程连接的用户,并创建数据库和表:
1 2 3 4 5 6
=== 远端数据库 == create database testdb; \c testdb createuser testdblink password 'Enmotech@123'; grantallon database testdb to testdblink ; createtable testdblink.testdata asselect generate_series(1,100) as id ;
f001=# \c f001 u_f001 Password foruser u_f001: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "u_f001". f001=> \conninfo You are connected to database "f001" asuser "u_f001" via socket in "/opt/mogdb/tmp" at port "26000". f001=>create extension postgres_fdw; ERROR: permission denied tocreate extension "postgres_fdw" HINT: Must be system admin tocreate this extension. f001=> \c f001 omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "omm". f001=# create extension postgres_fdw; CREATE EXTENSION
注意:Must be system admin to create this extension.
02创建远程数据库链接(create server),指定对端数据库的物理信息
1 2
===本地数据库=== create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '10.0.0.196', port '26000',dbname 'testdb');
f001=>select*from testdata@testDBLink_mogdb1; ERROR: remote table "testdata" does not exist f001=>select*from testdblink.testdata@testDBLink_mogdb1; ERROR: permission denied for relation (null) DETAIL: N/A f001=> \c f001 omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "omm". f001=# select*from testdblink.testdata@testDBLink_mogdb1; id ----- 1 2 3
在这里不能直接通过上面的语句访问,会报错:ERROR: remote table “testdata” does not exist
f001=>GRANT USAGE ONFOREIGN DATA WRAPPER postgres_fdw TO u_f001; ERROR: permission denied forforeign-data wrapper postgres_fdw DETAIL: N/A f001=> \c f001 omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "omm". f001=# GRANT USAGE ONFOREIGN DATA WRAPPER postgres_fdw TO u_f001; GRANT f001=# \c f001 u_f001 Password foruser u_f001: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "u_f001". f001=>create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '10.0.0.196', port '26000',dbname 'testdb'); CREATE SERVER f001=>createuser mapping for public server testDBLink_mogdb1 options(user'testdblink',password 'Enmotech@123'); CREATEUSER MAPPING f001=>select*from testdata@testDBLink_mogdb1; ERROR: remote table "testdata" does not exist f001=>select*from testdblink.testdata@testDBLink_mogdb1; ERROR: permission denied for relation (null) DETAIL: N/A f001=> \c f001 omm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "f001" asuser "omm". f001=# select*from testdblink.testdata@testDBLink_mogdb1; id ----- 1 2 3
1
select count(1) from testdblink.testdata@testDBLink_mogdb1;