{"id":20784,"date":"2024-01-02T05:53:26","date_gmt":"2024-01-02T05:53:26","guid":{"rendered":"https:\/\/server.hk\/blog\/20784\/"},"modified":"2024-11-14T10:38:44","modified_gmt":"2024-11-14T02:38:44","slug":"postgresql-command-create-foreign-data-wrapper","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/20784\/","title":{"rendered":"PostgreSQL \u547d\u4ee4\uff1aCREATE FOREIGN DATA WRAPPER"},"content":{"rendered":"<h1 id=\"postgresql-command-create-foreign-data-wrapper-iKzUlducCh\">PostgreSQL \u547d\u4ee4\uff1aCREATE FOREIGN DATA WRAPPER<\/h1>\n<p>PostgreSQL \u662f\u4e00\u500b\u5f37\u5927\u7684\u958b\u6e90\u95dc\u806f\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff0c\u63d0\u4f9b\u5ee3\u6cdb\u7684\u529f\u80fd\u548c\u80fd\u529b\u3002\u5176\u4e2d\u4e00\u500b\u986f\u8457\u7684\u7279\u6027\u662f\u80fd\u5920\u4f7f\u7528\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff08foreign data wrappers\uff09\uff0c\u9019\u4f7f\u5f97 PostgreSQL \u80fd\u5920\u8a2a\u554f\u5b58\u5132\u5728\u5916\u90e8\u7cfb\u7d71\u4e2d\u7684\u6578\u64da\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u63a2\u8a0e PostgreSQL \u547d\u4ee4 <code>CREATE FOREIGN DATA WRAPPER<\/code>\uff0c\u4e26\u4e86\u89e3\u5b83\u5982\u4f55\u7528\u65bc\u5c07\u5916\u90e8\u6578\u64da\u6e90\u6574\u5408\u5230\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<h2 id=\"understanding-foreign-data-wrappers-iKzUlducCh\">\u7406\u89e3\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668<\/h2>\n<p>\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff08FDW\uff09\u662f PostgreSQL \u4e2d\u7684\u4e00\u500b\u64f4\u5c55\uff0c\u5141\u8a31\u6578\u64da\u5eab\u8207\u5916\u90e8\u6578\u64da\u6e90\u4e92\u52d5\u3002\u5b83\u5145\u7576 PostgreSQL \u4f3a\u670d\u5668\u8207\u5916\u90e8\u7cfb\u7d71\u4e4b\u9593\u7684\u6a4b\u6a11\uff0c\u5be6\u73fe\u4f86\u81ea\u5404\u7a2e\u4f86\u6e90\u7684\u6578\u64da\u7121\u7e2b\u6574\u5408\u3002<\/p>\n<p>PostgreSQL \u652f\u6301\u591a\u7a2e\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff0c\u5305\u62ec\u7528\u65bc\u8a2a\u554f\u9060\u7a0b PostgreSQL \u6578\u64da\u5eab\u3001MySQL \u6578\u64da\u5eab\u3001Oracle \u6578\u64da\u5eab\uff0c\u751a\u81f3\u662f\u7db2\u7d61\u670d\u52d9\u7684\u5305\u88dd\u5668\u3002\u6bcf\u500b\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u63d0\u4f9b\u4e00\u7d44\u7279\u5b9a\u65bc\u5176\u9023\u63a5\u7684\u5916\u90e8\u6578\u64da\u6e90\u7684\u51fd\u6578\u548c\u9078\u9805\u3002<\/p>\n<h2 id=\"creating-a-foreign-data-wrapper-iKzUlducCh\">\u5275\u5efa\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668<\/h2>\n<p><code>CREATE FOREIGN DATA WRAPPER<\/code> \u547d\u4ee4\u7528\u65bc\u5728 PostgreSQL \u4e2d\u5b9a\u7fa9\u4e00\u500b\u65b0\u7684\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u8a9e\u6cd5\uff1a<\/p>\n<pre><code>CREATE FOREIGN DATA WRAPPER wrapper_name\r\n  [ HANDLER handler_function ]\r\n  [ VALIDATOR validator_function ]\r\n  [ OPTIONS (option 'value', ...) ]\r\n<\/code><\/pre>\n<p>\u8b93\u6211\u5011\u4f86\u5206\u89e3\u9019\u500b\u547d\u4ee4\u7684\u4e0d\u540c\u7d44\u4ef6\uff1a<\/p>\n<ul>\n<li><code>wrapper_name<\/code>\uff1a\u9019\u6307\u5b9a\u4e86\u60a8\u60f3\u8981\u5275\u5efa\u7684\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u7684\u540d\u7a31\u3002<\/li>\n<li><code>HANDLER handler_function<\/code>\uff1a\u9019\u662f\u4e00\u500b\u53ef\u9078\u53c3\u6578\uff0c\u6307\u5b9a\u8655\u7406\u51fd\u6578\u7684\u540d\u7a31\u3002\u8655\u7406\u51fd\u6578\u8ca0\u8cac\u5c0d\u5916\u90e8\u6578\u64da\u6e90\u57f7\u884c\u5be6\u969b\u67e5\u8a62\u3002<\/li>\n<li><code>VALIDATOR validator_function<\/code>\uff1a\u9019\u662f\u4e00\u500b\u53ef\u9078\u53c3\u6578\uff0c\u6307\u5b9a\u9a57\u8b49\u51fd\u6578\u7684\u540d\u7a31\u3002\u9a57\u8b49\u51fd\u6578\u7528\u65bc\u9a57\u8b49\u70ba\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u6307\u5b9a\u7684\u9078\u9805\u3002<\/li>\n<li><code>OPTIONS (option 'value', ...)<\/code>\uff1a\u9019\u662f\u4e00\u500b\u53ef\u9078\u53c3\u6578\uff0c\u5141\u8a31\u60a8\u70ba\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u6307\u5b9a\u5176\u4ed6\u9078\u9805\u3002\u9078\u9805\u53ca\u5176\u503c\u53d6\u6c7a\u65bc\u6240\u4f7f\u7528\u7684\u7279\u5b9a\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u3002<\/li>\n<\/ul>\n<h2 id=\"example-creating-a-foreign-data-wrapper-for-a-mysql-database-iKzUlducCh\">\u793a\u4f8b\uff1a\u70ba MySQL \u6578\u64da\u5eab\u5275\u5efa\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668<\/h2>\n<p>\u5047\u8a2d\u6211\u5011\u60f3\u8981\u5275\u5efa\u4e00\u500b\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u4f86\u8a2a\u554f MySQL \u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>mysql_fdw<\/code> \u64f4\u5c55\uff0c\u8a72\u64f4\u5c55\u63d0\u4f9b\u6240\u9700\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u6211\u5011\u5982\u4f55\u5275\u5efa\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff1a<\/p>\n<pre><code>CREATE EXTENSION mysql_fdw;\r\n\r\nCREATE SERVER mysql_server\r\n  FOREIGN DATA WRAPPER mysql_fdw\r\n  OPTIONS (host 'localhost', port '3306');\r\n\r\nCREATE USER MAPPING FOR current_user\r\n  SERVER mysql_server\r\n  OPTIONS (username 'mysql_user', password 'mysql_password');\r\n\r\nCREATE FOREIGN TABLE employees (\r\n  id INT,\r\n  name VARCHAR,\r\n  salary DECIMAL\r\n)\r\nSERVER mysql_server\r\nOPTIONS (dbname 'employees', table_name 'employees');\r\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa <code>mysql_fdw<\/code> \u64f4\u5c55\uff0c\u8a72\u64f4\u5c55\u63d0\u4f9b\u4e86\u8207 MySQL \u6578\u64da\u5eab\u4ea4\u4e92\u6240\u9700\u7684\u529f\u80fd\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code>CREATE SERVER<\/code> \u547d\u4ee4\u5275\u5efa\u4e00\u500b\u4f3a\u670d\u5668\u5c0d\u8c61\uff0c\u6307\u5b9a\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\u548c\u9023\u63a5\u9078\u9805\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u4f7f\u7528 <code>CREATE USER MAPPING<\/code> \u547d\u4ee4\u5275\u5efa\u7528\u6236\u6620\u5c04\uff0c\u5c07\u7576\u524d\u7684 PostgreSQL \u7528\u6236\u6620\u5c04\u5230 MySQL \u6578\u64da\u5eab\u4e2d\u7684\u7528\u6236\u3002\u9019\u4f7f\u5f97 PostgreSQL \u80fd\u5920\u9032\u884c\u8eab\u4efd\u9a57\u8b49\u4e26\u8a2a\u554f MySQL \u6578\u64da\u6e90\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code>CREATE FOREIGN TABLE<\/code> \u547d\u4ee4\u5275\u5efa\u4e00\u500b\u5916\u90e8\u8868\uff0c\u6307\u5b9a\u8868\u7684\u7d50\u69cb\u548c\u5176\u6240\u5c6c\u7684\u4f3a\u670d\u5668\u3002\u9019\u500b\u8868\u73fe\u5728\u53ef\u4ee5\u50cf PostgreSQL \u4e2d\u7684\u4efb\u4f55\u5176\u4ed6\u8868\u4e00\u6a23\u9032\u884c\u67e5\u8a62\uff0c\u67e5\u8a62\u5c07\u91dd\u5c0d MySQL \u6578\u64da\u5eab\u57f7\u884c\u3002<\/p>\n<h2 id=\"summary-iKzUlducCh\">\u7e3d\u7d50<\/h2>\n<p>PostgreSQL \u4e2d\u7684 <code>CREATE FOREIGN DATA WRAPPER<\/code> \u547d\u4ee4\u5141\u8a31\u60a8\u5b9a\u7fa9\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff0c\u4f7f\u6578\u64da\u5eab\u80fd\u5920\u8207\u5916\u90e8\u6578\u64da\u6e90\u4e92\u52d5\u3002\u901a\u904e\u5275\u5efa\u5916\u90e8\u6578\u64da\u5305\u88dd\u5668\uff0c\u60a8\u53ef\u4ee5\u7121\u7e2b\u5730\u5c07\u4f86\u81ea\u5404\u7a2e\u4f86\u6e90\u7684\u6578\u64da\u6574\u5408\u5230\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 VPS \u8a17\u7ba1\u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684 PostgreSQL \u6578\u64da\u5eab\uff0c\u8003\u616e\u4f7f\u7528 Server.HK\u3002Server.HK \u63d0\u4f9b\u5353\u8d8a\u7684\u6027\u80fd\u548c\u512a\u79c0\u7684\u652f\u6301\uff0c\u63d0\u4f9b\u591a\u7a2e VPS \u8a17\u7ba1\u8a08\u5283\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u4eca\u5929\u5c31\u63a2\u7d22 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\uff0c\u9ad4\u9a57\u5728\u53ef\u9760\u7684\u8a17\u7ba1\u74b0\u5883\u4e2d\u4f7f\u7528 PostgreSQL \u7684\u529b\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PostgreSQL \u547d\u4ee4\uff1aCR&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-20784","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=20784"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20784\/revisions"}],"predecessor-version":[{"id":24315,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/20784\/revisions\/24315"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=20784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=20784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=20784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}