gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 lines
9.4KB

  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>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="ecpg-sql-allocate-descriptor.html" title="ALLOCATE DESCRIPTOR" /><link rel="next" href="ecpg-sql-deallocate-descriptor.html" title="DEALLOCATE DESCRIPTOR" /></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">CONNECT</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-sql-allocate-descriptor.html" title="ALLOCATE DESCRIPTOR">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg-sql-commands.html" title="35.14. Embedded SQL Commands">Up</a></td><th width="60%" align="center">35.14. Embedded SQL Commands</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="ecpg-sql-deallocate-descriptor.html" title="DEALLOCATE DESCRIPTOR">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="ECPG-SQL-CONNECT"><div class="titlepage"></div><div class="refnamediv"><h2>CONNECT</h2><p>CONNECT — establish a database connection</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. CONNECT TO <em class="replaceable"><code>connection_target</code></em> [ AS <em class="replaceable"><code>connection_name</code></em> ] [ USER <em class="replaceable"><code>connection_user</code></em> ]
  4. CONNECT TO DEFAULT
  5. CONNECT <em class="replaceable"><code>connection_user</code></em>
  6. DATABASE <em class="replaceable"><code>connection_target</code></em>
  7. </pre></div><div class="refsect1" id="id-1.7.5.20.4.3"><h2>Description</h2><p>
  8. The <code class="command">CONNECT</code> command establishes a connection
  9. between the client and the PostgreSQL server.
  10. </p></div><div class="refsect1" id="id-1.7.5.20.4.4"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>connection_target</code></em></span></dt><dd><p>
  11. <em class="replaceable"><code>connection_target</code></em>
  12. specifies the target server of the connection on one of
  13. several forms.
  14. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">[ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">@</code><em class="replaceable"><code>host</code></em> ] [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ]</span></dt><dd><p>
  15. Connect over TCP/IP
  16. </p></dd><dt><span class="term"><code class="literal">unix:postgresql://</code><em class="replaceable"><code>host</code></em> [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ] <code class="literal">/</code> [ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">?</code><em class="replaceable"><code>connection_option</code></em> ]</span></dt><dd><p>
  17. Connect over Unix-domain sockets
  18. </p></dd><dt><span class="term"><code class="literal">tcp:postgresql://</code><em class="replaceable"><code>host</code></em> [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ] <code class="literal">/</code> [ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">?</code><em class="replaceable"><code>connection_option</code></em> ]</span></dt><dd><p>
  19. Connect over TCP/IP
  20. </p></dd><dt><span class="term">SQL string constant</span></dt><dd><p>
  21. containing a value in one of the above forms
  22. </p></dd><dt><span class="term">host variable</span></dt><dd><p>
  23. host variable of type <code class="type">char[]</code>
  24. or <code class="type">VARCHAR[]</code> containing a value in one of the
  25. above forms
  26. </p></dd></dl></div><p>
  27. </p></dd><dt><span class="term"><em class="replaceable"><code>connection_name</code></em></span></dt><dd><p>
  28. An optional identifier for the connection, so that it can be
  29. referred to in other commands. This can be an SQL identifier
  30. or a host variable.
  31. </p></dd><dt><span class="term"><em class="replaceable"><code>connection_user</code></em></span></dt><dd><p>
  32. The user name for the database connection.
  33. </p><p>
  34. This parameter can also specify user name and password, using one the forms
  35. <code class="literal"><em class="replaceable"><code>user_name</code></em>/<em class="replaceable"><code>password</code></em></code>,
  36. <code class="literal"><em class="replaceable"><code>user_name</code></em> IDENTIFIED BY <em class="replaceable"><code>password</code></em></code>, or
  37. <code class="literal"><em class="replaceable"><code>user_name</code></em> USING <em class="replaceable"><code>password</code></em></code>.
  38. </p><p>
  39. User name and password can be SQL identifiers, string
  40. constants, or host variables.
  41. </p></dd><dt><span class="term"><code class="literal">DEFAULT</code></span></dt><dd><p>
  42. Use all default connection parameters, as defined by libpq.
  43. </p></dd></dl></div></div><div class="refsect1" id="id-1.7.5.20.4.5"><h2>Examples</h2><p>
  44. Here a several variants for specifying connection parameters:
  45. </p><pre class="programlisting">
  46. EXEC SQL CONNECT TO "connectdb" AS main;
  47. EXEC SQL CONNECT TO "connectdb" AS second;
  48. EXEC SQL CONNECT TO "unix:postgresql://200.46.204.71/connectdb" AS main USER connectuser;
  49. EXEC SQL CONNECT TO "unix:postgresql://localhost/connectdb" AS main USER connectuser;
  50. EXEC SQL CONNECT TO 'connectdb' AS main;
  51. EXEC SQL CONNECT TO 'unix:postgresql://localhost/connectdb' AS main USER :user;
  52. EXEC SQL CONNECT TO :db AS :id;
  53. EXEC SQL CONNECT TO :db USER connectuser USING :pw;
  54. EXEC SQL CONNECT TO @localhost AS main USER connectdb;
  55. EXEC SQL CONNECT TO REGRESSDB1 as main;
  56. EXEC SQL CONNECT TO AS main USER connectdb;
  57. EXEC SQL CONNECT TO connectdb AS :id;
  58. EXEC SQL CONNECT TO connectdb AS main USER connectuser/connectdb;
  59. EXEC SQL CONNECT TO connectdb AS main;
  60. EXEC SQL CONNECT TO connectdb@localhost AS main;
  61. EXEC SQL CONNECT TO tcp:postgresql://localhost/ USER connectdb;
  62. EXEC SQL CONNECT TO tcp:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY connectpw;
  63. EXEC SQL CONNECT TO tcp:postgresql://localhost:20/connectdb USER connectuser IDENTIFIED BY connectpw;
  64. EXEC SQL CONNECT TO unix:postgresql://localhost/ AS main USER connectdb;
  65. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb AS main USER connectuser;
  66. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY "connectpw";
  67. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser USING "connectpw";
  68. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb?connect_timeout=14 USER connectuser;
  69. </pre><p>
  70. </p><p>
  71. Here is an example program that illustrates the use of host
  72. variables to specify connection parameters:
  73. </p><pre class="programlisting">
  74. int
  75. main(void)
  76. {
  77. EXEC SQL BEGIN DECLARE SECTION;
  78. char *dbname = "testdb"; /* database name */
  79. char *user = "testuser"; /* connection user name */
  80. char *connection = "tcp:postgresql://localhost:5432/testdb";
  81. /* connection string */
  82. char ver[256]; /* buffer to store the version string */
  83. EXEC SQL END DECLARE SECTION;
  84. ECPGdebug(1, stderr);
  85. EXEC SQL CONNECT TO :dbname USER :user;
  86. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  87. EXEC SQL SELECT version() INTO :ver;
  88. EXEC SQL DISCONNECT;
  89. printf("version: %s\n", ver);
  90. EXEC SQL CONNECT TO :connection USER :user;
  91. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  92. EXEC SQL SELECT version() INTO :ver;
  93. EXEC SQL DISCONNECT;
  94. printf("version: %s\n", ver);
  95. return 0;
  96. }
  97. </pre><p>
  98. </p></div><div class="refsect1" id="id-1.7.5.20.4.6"><h2>Compatibility</h2><p>
  99. <code class="command">CONNECT</code> is specified in the SQL standard, but
  100. the format of the connection parameters is
  101. implementation-specific.
  102. </p></div><div class="refsect1" id="id-1.7.5.20.4.7"><h2>See Also</h2><span class="simplelist"><a class="xref" href="ecpg-sql-disconnect.html" title="DISCONNECT">DISCONNECT</a>, <a class="xref" href="ecpg-sql-set-connection.html" title="SET CONNECTION">SET CONNECTION</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-sql-allocate-descriptor.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg-sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-sql-deallocate-descriptor.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALLOCATE DESCRIPTOR </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> DEALLOCATE DESCRIPTOR</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1