=== 远端数据库 MogDB=# create database testdb; CREATE DATABASE MogDB=# \c testdb Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb" asuser "fomm". testdb=# createuser testdblink password 'Enmotech@123'; NOTICE: The encrypted password contains MD5 ciphertext, which isnot secure. CREATE ROLE testdb=# grantallon database testdb to testdblink testdb-# \c testdb testdblink Password foruser testdblink: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "testdb" asuser "testdblink". testdb=>createtable testdblink.testdata asselect generate_series(1,100) as id ; INSERT0100
==本地数据库 MogDB=# create database liups; CREATE DATABASE MogDB=# \c liups Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "liups" asuser "fomm". liups=# createuser liups password 'Password123'; NOTICE: The encrypted password contains MD5 ciphertext, which isnot secure. CREATE ROLE liups=# grantallon database liups to liups ; GRANT liups=#
pg_hba.conf 配置允许远程登录,这里在 配置文件新增如下:
1
host all all 0.0.0.0/0 md5
验证远端数据库可以远程登录
1 2 3 4 5 6 7 8 9
[fomm@cnwww ~]$ gsql -h172.20.62.166 -Utestdblink testdb Password for user testdblink: gsql ((MogDB 5.0.8 build 2ede45c9) compiled at 2024-06-3012:43:48 commit 0 last mr 1804 ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help.
testdb=> \conninfo You are connected to database "testdb" as user "testdblink" on host "172.20.62.166" at port "27000". testdb=>
liups=>create extension postgres_fdw; ERROR: permission denied tocreate extension "postgres_fdw" HINT: Must be system admin tocreate this extension. liups=> \c liups fomm Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "liups" asuser "fomm". liups=# create extension postgres_fdw; CREATE EXTENSION liups=# \dx List of installed extensions Name | Version | Schema | Description -----------------+---------+------------+------------------------------------------------- dist_fdw |1.0| pg_catalog |foreign-data wrapper for distfs access file_fdw |1.0| pg_catalog |foreign-data wrapper for flat file access hstore |1.1| pg_catalog | data type for storing sets of (key, value) pairs log_fdw |1.0| pg_catalog |Foreign Data Wrapper for accessing logging data plpgsql |1.0| pg_catalog | PL/pgSQL procedural language postgres_fdw |1.0| public |foreign-data wrapper for remote PostgreSQL servers security_plugin |1.0| pg_catalog | provides security functionality tidrangescan |1.0| pg_catalog | example implementation for custom-scan-provider interface (8rows) liups=#
创建远程数据库链接(create server),指定对端数据库的物理信息
1 2
===本地数据库=== create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '172.20.62.166', port '27000',dbname 'testdb');
1 2 3 4 5
liups=> create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '172.20.62.166', port '27000',dbname 'testdb'); ERROR: permission denied for foreign-data wrapper postgres_fdw DETAIL: N/A liups=> Connection to cn.liups.com closed by remote host. Connection to cn.liups.com closed.
注意事项:创建远程数据库链接(create server)可以通过管理员进行创建,或者授予 :GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO liups; 权限来创建。
1 2 3 4 5 6 7 8 9
liups=# GRANT USAGE ONFOREIGN DATA WRAPPER postgres_fdw TO liups; GRANT liups=# \c liups liups Password foruser liups: Non-SSL connection (SSL connection is recommended when requiring high-security) You are now connected to database "liups" asuser "liups". liups=>create server testDBLink_mogdb1 foreign data wrapper postgres_fdw options(host '172.20.62.166', port '27000',dbname 'testdb'); CREATE SERVER liups=>
目前测试给 USAGE 权限即可,当然如果给 ALL PRIVILEGES 那更可以。
1
GRANTALL PRIVILEGES onFOREIGN DATA WRAPPER postgres_fdw TO testuser;
创建用户映射关系(create user mapping),指定对端数据库的认证信息
注意,在做这一步之前,如果之前没有创建过针对usermapping的密码保护,则需要执行以下语句
1 2 3 4 5 6 7 8 9 10 11
[root@cnwww~]# su - fomm Last login: Wed Jul 310:04:55 CST 2024on pts/0 [fomm@cnwww~]$ ls -l $GAUSSHOME/bin/user* ls: cannot access /fopt/mogdb/app/bin/user*: No such file or directory [fomm@cnwww~]$ gs_guc generate -o usermapping -S default-D $GAUSSHOME/bin The gs_guc run with the following arguments: [gs_guc -o usermapping -S *******-D /fopt/mogdb/app/bin generate ]. gs_guc generate -S *** [fomm@cnwww~]$ ls -l $GAUSSHOME/bin/user* -rw------- 1 fomm fomm 56 Jul 3 10:07 /fopt/mogdb/app/bin/usermapping.key.cipher -rw------- 1 fomm fomm 24 Jul 3 10:07 /fopt/mogdb/app/bin/usermapping.key.rand [fomm@cnwww~]$