gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

106 rindas
7.6KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>dblink_connect</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="dblink.html" title="F.10. dblink" /><link rel="next" href="contrib-dblink-connect-u.html" title="dblink_connect_u" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">dblink_connect</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="dblink.html" title="F.10. dblink">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="dblink.html" title="F.10. dblink">Up</a></td><th width="60%" align="center">F.10. dblink</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="contrib-dblink-connect-u.html" title="dblink_connect_u">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="CONTRIB-DBLINK-CONNECT"><div class="titlepage"></div><a id="id-1.11.7.19.5.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">dblink_connect</span></h2><p>dblink_connect — opens a persistent connection to a remote database</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. dblink_connect(text connstr) returns text
  4. dblink_connect(text connname, text connstr) returns text
  5. </pre></div><div class="refsect1" id="id-1.11.7.19.5.5"><h2>Description</h2><p>
  6. <code class="function">dblink_connect()</code> establishes a connection to a remote
  7. <span class="productname">PostgreSQL</span> database. The server and database to
  8. be contacted are identified through a standard <span class="application">libpq</span>
  9. connection string. Optionally, a name can be assigned to the
  10. connection. Multiple named connections can be open at once, but
  11. only one unnamed connection is permitted at a time. The connection
  12. will persist until closed or until the database session is ended.
  13. </p><p>
  14. The connection string may also be the name of an existing foreign
  15. server. It is recommended to use the foreign-data wrapper
  16. <code class="literal">dblink_fdw</code> when defining the foreign
  17. server. See the example below, as well as
  18. <a class="xref" href="sql-createserver.html" title="CREATE SERVER"><span class="refentrytitle">CREATE SERVER</span></a> and
  19. <a class="xref" href="sql-createusermapping.html" title="CREATE USER MAPPING"><span class="refentrytitle">CREATE USER MAPPING</span></a>.
  20. </p></div><div class="refsect1" id="id-1.11.7.19.5.6"><h2>Arguments</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="parameter"><code>connname</code></em></span></dt><dd><p>
  21. The name to use for this connection; if omitted, an unnamed
  22. connection is opened, replacing any existing unnamed connection.
  23. </p></dd><dt><span class="term"><em class="parameter"><code>connstr</code></em></span></dt><dd><p><span class="application">libpq</span>-style connection info string, for example
  24. <code class="literal">hostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres
  25. password=mypasswd options=-csearch_path=</code>.
  26. For details see <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a>.
  27. Alternatively, the name of a foreign server.
  28. </p></dd></dl></div></div><div class="refsect1" id="id-1.11.7.19.5.7"><h2>Return Value</h2><p>
  29. Returns status, which is always <code class="literal">OK</code> (since any error
  30. causes the function to throw an error instead of returning).
  31. </p></div><div class="refsect1" id="id-1.11.7.19.5.8"><h2>Notes</h2><p>
  32. If untrusted users have access to a database that has not adopted a
  33. <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
  34. begin each session by removing publicly-writable schemas from
  35. <code class="varname">search_path</code>. One could, for example,
  36. add <code class="literal">options=-csearch_path=</code> to
  37. <em class="parameter"><code>connstr</code></em>. This consideration is not specific
  38. to <code class="filename">dblink</code>; it applies to every interface for
  39. executing arbitrary SQL commands.
  40. </p><p>
  41. Only superusers may use <code class="function">dblink_connect</code> to create
  42. non-password-authenticated connections. If non-superusers need this
  43. capability, use <code class="function">dblink_connect_u</code> instead.
  44. </p><p>
  45. It is unwise to choose connection names that contain equal signs,
  46. as this opens a risk of confusion with connection info strings
  47. in other <code class="filename">dblink</code> functions.
  48. </p></div><div class="refsect1" id="id-1.11.7.19.5.9"><h2>Examples</h2><pre class="screen">
  49. SELECT dblink_connect('dbname=postgres options=-csearch_path=');
  50. dblink_connect
  51. ----------------
  52. OK
  53. (1 row)
  54. SELECT dblink_connect('myconn', 'dbname=postgres options=-csearch_path=');
  55. dblink_connect
  56. ----------------
  57. OK
  58. (1 row)
  59. -- FOREIGN DATA WRAPPER functionality
  60. -- Note: local connection must require password authentication for this to work properly
  61. -- Otherwise, you will receive the following error from dblink_connect():
  62. -- ----------------------------------------------------------------------
  63. -- ERROR: password is required
  64. -- DETAIL: Non-superuser cannot connect if the server does not request a password.
  65. -- HINT: Target server's authentication method must be changed.
  66. CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw OPTIONS (hostaddr '127.0.0.1', dbname 'contrib_regression');
  67. CREATE USER regress_dblink_user WITH PASSWORD 'secret';
  68. CREATE USER MAPPING FOR regress_dblink_user SERVER fdtest OPTIONS (user 'regress_dblink_user', password 'secret');
  69. GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
  70. GRANT SELECT ON TABLE foo TO regress_dblink_user;
  71. \set ORIGINAL_USER :USER
  72. \c - regress_dblink_user
  73. SELECT dblink_connect('myconn', 'fdtest');
  74. dblink_connect
  75. ----------------
  76. OK
  77. (1 row)
  78. SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
  79. a | b | c
  80. ----+---+---------------
  81. 0 | a | {a0,b0,c0}
  82. 1 | b | {a1,b1,c1}
  83. 2 | c | {a2,b2,c2}
  84. 3 | d | {a3,b3,c3}
  85. 4 | e | {a4,b4,c4}
  86. 5 | f | {a5,b5,c5}
  87. 6 | g | {a6,b6,c6}
  88. 7 | h | {a7,b7,c7}
  89. 8 | i | {a8,b8,c8}
  90. 9 | j | {a9,b9,c9}
  91. 10 | k | {a10,b10,c10}
  92. (11 rows)
  93. \c - :ORIGINAL_USER
  94. REVOKE USAGE ON FOREIGN SERVER fdtest FROM regress_dblink_user;
  95. REVOKE SELECT ON TABLE foo FROM regress_dblink_user;
  96. DROP USER MAPPING FOR regress_dblink_user SERVER fdtest;
  97. DROP USER regress_dblink_user;
  98. DROP SERVER fdtest;
  99. </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="dblink.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="dblink.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="contrib-dblink-connect-u.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">F.10. dblink </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> dblink_connect_u</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1