gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

939 satır
67KB

  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>33.1. Database Connection Control Functions</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="libpq.html" title="Chapter 33. libpq - C Library" /><link rel="next" href="libpq-status.html" title="33.2. Connection Status Functions" /></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">33.1. Database Connection Control Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="libpq.html" title="Chapter 33. libpq - C Library">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="libpq.html" title="Chapter 33. libpq - C Library">Up</a></td><th width="60%" align="center">Chapter 33. <span xmlns="http://www.w3.org/1999/xhtml" class="application">libpq</span> - C Library</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="libpq-status.html" title="33.2. Connection Status Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LIBPQ-CONNECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.1. Database Connection Control Functions</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="libpq-connect.html#LIBPQ-CONNSTRING">33.1.1. Connection Strings</a></span></dt><dt><span class="sect2"><a href="libpq-connect.html#LIBPQ-PARAMKEYWORDS">33.1.2. Parameter Key Words</a></span></dt></dl></div><p>
  3. The following functions deal with making a connection to a
  4. <span class="productname">PostgreSQL</span> backend server. An
  5. application program can have several backend connections open at
  6. one time. (One reason to do that is to access more than one
  7. database.) Each connection is represented by a
  8. <code class="structname">PGconn</code><a id="id-1.7.3.8.2.3" class="indexterm"></a> object, which
  9. is obtained from the function <code class="function">PQconnectdb</code>,
  10. <code class="function">PQconnectdbParams</code>, or
  11. <code class="function">PQsetdbLogin</code>. Note that these functions will always
  12. return a non-null object pointer, unless perhaps there is too
  13. little memory even to allocate the <code class="structname">PGconn</code> object.
  14. The <code class="function">PQstatus</code> function should be called to check
  15. the return value for a successful connection before queries are sent
  16. via the connection object.
  17. </p><div class="warning"><h3 class="title">Warning</h3><p>
  18. If untrusted users have access to a database that has not adopted a
  19. <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
  20. begin each session by removing publicly-writable schemas from
  21. <code class="varname">search_path</code>. One can set parameter key
  22. word <code class="literal">options</code> to
  23. value <code class="literal">-csearch_path=</code>. Alternately, one can
  24. issue <code class="literal">PQexec(<em class="replaceable"><code>conn</code></em>, "SELECT
  25. pg_catalog.set_config('search_path', '', false)")</code> after
  26. connecting. This consideration is not specific
  27. to <span class="application">libpq</span>; it applies to every interface for
  28. executing arbitrary SQL commands.
  29. </p></div><p>
  30. </p><div class="warning"><h3 class="title">Warning</h3><p>
  31. On Unix, forking a process with open libpq connections can lead to
  32. unpredictable results because the parent and child processes share
  33. the same sockets and operating system resources. For this reason,
  34. such usage is not recommended, though doing an <code class="function">exec</code> from
  35. the child process to load a new executable is safe.
  36. </p></div><p>
  37. </p><div class="note"><h3 class="title">Note</h3><p>
  38. On Windows, there is a way to improve performance if a single
  39. database connection is repeatedly started and shutdown. Internally,
  40. libpq calls <code class="function">WSAStartup()</code> and <code class="function">WSACleanup()</code> for connection startup
  41. and shutdown, respectively. <code class="function">WSAStartup()</code> increments an internal
  42. Windows library reference count which is decremented by <code class="function">WSACleanup()</code>.
  43. When the reference count is just one, calling <code class="function">WSACleanup()</code> frees
  44. all resources and all DLLs are unloaded. This is an expensive
  45. operation. To avoid this, an application can manually call
  46. <code class="function">WSAStartup()</code> so resources will not be freed when the last database
  47. connection is closed.
  48. </p></div><p>
  49. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQCONNECTDBPARAMS"><span class="term"><code class="function">PQconnectdbParams</code><a id="id-1.7.3.8.2.12.1.1.2" class="indexterm"></a></span></dt><dd><p>
  50. Makes a new connection to the database server.
  51. </p><pre class="synopsis">
  52. PGconn *PQconnectdbParams(const char * const *keywords,
  53. const char * const *values,
  54. int expand_dbname);
  55. </pre><p>
  56. </p><p>
  57. This function opens a new database connection using the parameters taken
  58. from two <code class="symbol">NULL</code>-terminated arrays. The first,
  59. <code class="literal">keywords</code>, is defined as an array of strings, each one
  60. being a key word. The second, <code class="literal">values</code>, gives the value
  61. for each key word. Unlike <code class="function">PQsetdbLogin</code> below, the parameter
  62. set can be extended without changing the function signature, so use of
  63. this function (or its nonblocking analogs <code class="function">PQconnectStartParams</code>
  64. and <code class="function">PQconnectPoll</code>) is preferred for new application
  65. programming.
  66. </p><p>
  67. The currently recognized parameter key words are listed in
  68. <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a>.
  69. </p><p>
  70. When <code class="literal">expand_dbname</code> is non-zero, the
  71. <em class="parameter"><code>dbname</code></em> key word value is allowed to be recognized
  72. as a connection string. Only the first occurrence of
  73. <em class="parameter"><code>dbname</code></em> is expanded this way, any subsequent
  74. <em class="parameter"><code>dbname</code></em> value is processed as plain database name. More
  75. details on the possible connection string formats appear in
  76. <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a>.
  77. </p><p>
  78. The passed arrays can be empty to use all default parameters, or can
  79. contain one or more parameter settings. They should be matched in length.
  80. Processing will stop at the first <code class="symbol">NULL</code> element
  81. in the <code class="literal">keywords</code> array.
  82. </p><p>
  83. If any parameter is <code class="symbol">NULL</code> or an empty string, the corresponding
  84. environment variable (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>) is checked.
  85. If the environment variable is not set either, then the indicated
  86. built-in defaults are used.
  87. </p><p>
  88. In general key words are processed from the beginning of these arrays in index
  89. order. The effect of this is that when key words are repeated, the last processed
  90. value is retained. Therefore, through careful placement of the
  91. <em class="parameter"><code>dbname</code></em> key word, it is possible to determine what may
  92. be overridden by a <em class="parameter"><code>conninfo</code></em> string, and what may not.
  93. </p></dd><dt id="LIBPQ-PQCONNECTDB"><span class="term"><code class="function">PQconnectdb</code><a id="id-1.7.3.8.2.12.2.1.2" class="indexterm"></a></span></dt><dd><p>
  94. Makes a new connection to the database server.
  95. </p><pre class="synopsis">
  96. PGconn *PQconnectdb(const char *conninfo);
  97. </pre><p>
  98. </p><p>
  99. This function opens a new database connection using the parameters taken
  100. from the string <code class="literal">conninfo</code>.
  101. </p><p>
  102. The passed string can be empty to use all default parameters, or it can
  103. contain one or more parameter settings separated by whitespace,
  104. or it can contain a <acronym class="acronym">URI</acronym>.
  105. See <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a> for details.
  106. </p></dd><dt id="LIBPQ-PQSETDBLOGIN"><span class="term"><code class="function">PQsetdbLogin</code><a id="id-1.7.3.8.2.12.3.1.2" class="indexterm"></a></span></dt><dd><p>
  107. Makes a new connection to the database server.
  108. </p><pre class="synopsis">
  109. PGconn *PQsetdbLogin(const char *pghost,
  110. const char *pgport,
  111. const char *pgoptions,
  112. const char *pgtty,
  113. const char *dbName,
  114. const char *login,
  115. const char *pwd);
  116. </pre><p>
  117. </p><p>
  118. This is the predecessor of <code class="function">PQconnectdb</code> with a fixed
  119. set of parameters. It has the same functionality except that the
  120. missing parameters will always take on default values. Write <code class="symbol">NULL</code> or an
  121. empty string for any one of the fixed parameters that is to be defaulted.
  122. </p><p>
  123. If the <em class="parameter"><code>dbName</code></em> contains
  124. an <code class="symbol">=</code> sign or has a valid connection <acronym class="acronym">URI</acronym> prefix, it
  125. is taken as a <em class="parameter"><code>conninfo</code></em> string in exactly the same way as
  126. if it had been passed to <code class="function">PQconnectdb</code>, and the remaining
  127. parameters are then applied as specified for <code class="function">PQconnectdbParams</code>.
  128. </p></dd><dt id="LIBPQ-PQSETDB"><span class="term"><code class="function">PQsetdb</code><a id="id-1.7.3.8.2.12.4.1.2" class="indexterm"></a></span></dt><dd><p>
  129. Makes a new connection to the database server.
  130. </p><pre class="synopsis">
  131. PGconn *PQsetdb(char *pghost,
  132. char *pgport,
  133. char *pgoptions,
  134. char *pgtty,
  135. char *dbName);
  136. </pre><p>
  137. </p><p>
  138. This is a macro that calls <code class="function">PQsetdbLogin</code> with null pointers
  139. for the <em class="parameter"><code>login</code></em> and <em class="parameter"><code>pwd</code></em> parameters. It is provided
  140. for backward compatibility with very old programs.
  141. </p></dd><dt id="LIBPQ-PQCONNECTSTARTPARAMS"><span class="term"><code class="function">PQconnectStartParams</code><a id="id-1.7.3.8.2.12.5.1.2" class="indexterm"></a><br /></span><span class="term"><code class="function">PQconnectStart</code><a id="id-1.7.3.8.2.12.5.2.2" class="indexterm"></a><br /></span><span class="term"><code class="function">PQconnectPoll</code><a id="id-1.7.3.8.2.12.5.3.2" class="indexterm"></a></span></dt><dd><p>
  142. <a id="id-1.7.3.8.2.12.5.4.1.1" class="indexterm"></a>
  143. Make a connection to the database server in a nonblocking manner.
  144. </p><pre class="synopsis">
  145. PGconn *PQconnectStartParams(const char * const *keywords,
  146. const char * const *values,
  147. int expand_dbname);
  148. PGconn *PQconnectStart(const char *conninfo);
  149. PostgresPollingStatusType PQconnectPoll(PGconn *conn);
  150. </pre><p>
  151. </p><p>
  152. These three functions are used to open a connection to a database server such
  153. that your application's thread of execution is not blocked on remote I/O
  154. whilst doing so. The point of this approach is that the waits for I/O to
  155. complete can occur in the application's main loop, rather than down inside
  156. <code class="function">PQconnectdbParams</code> or <code class="function">PQconnectdb</code>, and so the
  157. application can manage this operation in parallel with other activities.
  158. </p><p>
  159. With <code class="function">PQconnectStartParams</code>, the database connection is made
  160. using the parameters taken from the <code class="literal">keywords</code> and
  161. <code class="literal">values</code> arrays, and controlled by <code class="literal">expand_dbname</code>,
  162. as described above for <code class="function">PQconnectdbParams</code>.
  163. </p><p>
  164. With <code class="function">PQconnectStart</code>, the database connection is made
  165. using the parameters taken from the string <code class="literal">conninfo</code> as
  166. described above for <code class="function">PQconnectdb</code>.
  167. </p><p>
  168. Neither <code class="function">PQconnectStartParams</code> nor <code class="function">PQconnectStart</code>
  169. nor <code class="function">PQconnectPoll</code> will block, so long as a number of
  170. restrictions are met:
  171. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  172. The <code class="literal">hostaddr</code> parameter must be used appropriately
  173. to prevent DNS queries from being made. See the documentation of
  174. this parameter in <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a> for details.
  175. </p></li><li class="listitem"><p>
  176. If you call <code class="function">PQtrace</code>, ensure that the stream object
  177. into which you trace will not block.
  178. </p></li><li class="listitem"><p>
  179. You must ensure that the socket is in the appropriate state
  180. before calling <code class="function">PQconnectPoll</code>, as described below.
  181. </p></li></ul></div><p>
  182. </p><p>
  183. To begin a nonblocking connection request,
  184. call <code class="function">PQconnectStart</code>
  185. or <code class="function">PQconnectStartParams</code>. If the result is null,
  186. then <span class="application">libpq</span> has been unable to allocate a
  187. new <code class="structname">PGconn</code> structure. Otherwise, a
  188. valid <code class="structname">PGconn</code> pointer is returned (though not
  189. yet representing a valid connection to the database). Next
  190. call <code class="literal">PQstatus(conn)</code>. If the result
  191. is <code class="symbol">CONNECTION_BAD</code>, the connection attempt has already
  192. failed, typically because of invalid connection parameters.
  193. </p><p>
  194. If <code class="function">PQconnectStart</code>
  195. or <code class="function">PQconnectStartParams</code> succeeds, the next stage
  196. is to poll <span class="application">libpq</span> so that it can proceed with
  197. the connection sequence.
  198. Use <code class="function">PQsocket(conn)</code> to obtain the descriptor of the
  199. socket underlying the database connection.
  200. (Caution: do not assume that the socket remains the same
  201. across <code class="function">PQconnectPoll</code> calls.)
  202. Loop thus: If <code class="function">PQconnectPoll(conn)</code> last returned
  203. <code class="symbol">PGRES_POLLING_READING</code>, wait until the socket is ready to
  204. read (as indicated by <code class="function">select()</code>, <code class="function">poll()</code>, or
  205. similar system function).
  206. Then call <code class="function">PQconnectPoll(conn)</code> again.
  207. Conversely, if <code class="function">PQconnectPoll(conn)</code> last returned
  208. <code class="symbol">PGRES_POLLING_WRITING</code>, wait until the socket is ready
  209. to write, then call <code class="function">PQconnectPoll(conn)</code> again.
  210. On the first iteration, i.e. if you have yet to call
  211. <code class="function">PQconnectPoll</code>, behave as if it last returned
  212. <code class="symbol">PGRES_POLLING_WRITING</code>. Continue this loop until
  213. <code class="function">PQconnectPoll(conn)</code> returns
  214. <code class="symbol">PGRES_POLLING_FAILED</code>, indicating the connection procedure
  215. has failed, or <code class="symbol">PGRES_POLLING_OK</code>, indicating the connection
  216. has been successfully made.
  217. </p><p>
  218. At any time during connection, the status of the connection can be
  219. checked by calling <code class="function">PQstatus</code>. If this call returns <code class="symbol">CONNECTION_BAD</code>, then the
  220. connection procedure has failed; if the call returns <code class="function">CONNECTION_OK</code>, then the
  221. connection is ready. Both of these states are equally detectable
  222. from the return value of <code class="function">PQconnectPoll</code>, described above. Other states might also occur
  223. during (and only during) an asynchronous connection procedure. These
  224. indicate the current stage of the connection procedure and might be useful
  225. to provide feedback to the user for example. These statuses are:
  226. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-CONNECTION-STARTED"><span class="term"><code class="symbol">CONNECTION_STARTED</code></span></dt><dd><p>
  227. Waiting for connection to be made.
  228. </p></dd><dt id="LIBPQ-CONNECTION-MADE"><span class="term"><code class="symbol">CONNECTION_MADE</code></span></dt><dd><p>
  229. Connection OK; waiting to send.
  230. </p></dd><dt id="LIBPQ-CONNECTION-AWAITING-RESPONSE"><span class="term"><code class="symbol">CONNECTION_AWAITING_RESPONSE</code></span></dt><dd><p>
  231. Waiting for a response from the server.
  232. </p></dd><dt id="LIBPQ-CONNECTION-AUTH-OK"><span class="term"><code class="symbol">CONNECTION_AUTH_OK</code></span></dt><dd><p>
  233. Received authentication; waiting for backend start-up to finish.
  234. </p></dd><dt id="LIBPQ-CONNECTION-SSL-STARTUP"><span class="term"><code class="symbol">CONNECTION_SSL_STARTUP</code></span></dt><dd><p>
  235. Negotiating SSL encryption.
  236. </p></dd><dt id="LIBPQ-CONNECTION-SETENV"><span class="term"><code class="symbol">CONNECTION_SETENV</code></span></dt><dd><p>
  237. Negotiating environment-driven parameter settings.
  238. </p></dd><dt id="LIBPQ-CONNECTION-CHECK-WRITABLE"><span class="term"><code class="symbol">CONNECTION_CHECK_WRITABLE</code></span></dt><dd><p>
  239. Checking if connection is able to handle write transactions.
  240. </p></dd><dt id="LIBPQ-CONNECTION-CONSUME"><span class="term"><code class="symbol">CONNECTION_CONSUME</code></span></dt><dd><p>
  241. Consuming any remaining response messages on connection.
  242. </p></dd></dl></div><p>
  243. Note that, although these constants will remain (in order to maintain
  244. compatibility), an application should never rely upon these occurring in a
  245. particular order, or at all, or on the status always being one of these
  246. documented values. An application might do something like this:
  247. </p><pre class="programlisting">
  248. switch(PQstatus(conn))
  249. {
  250. case CONNECTION_STARTED:
  251. feedback = "Connecting...";
  252. break;
  253. case CONNECTION_MADE:
  254. feedback = "Connected to server...";
  255. break;
  256. .
  257. .
  258. .
  259. default:
  260. feedback = "Connecting...";
  261. }
  262. </pre><p>
  263. </p><p>
  264. The <code class="literal">connect_timeout</code> connection parameter is ignored
  265. when using <code class="function">PQconnectPoll</code>; it is the application's
  266. responsibility to decide whether an excessive amount of time has elapsed.
  267. Otherwise, <code class="function">PQconnectStart</code> followed by a
  268. <code class="function">PQconnectPoll</code> loop is equivalent to
  269. <code class="function">PQconnectdb</code>.
  270. </p><p>
  271. Note that when <code class="function">PQconnectStart</code>
  272. or <code class="function">PQconnectStartParams</code> returns a non-null
  273. pointer, you must call <code class="function">PQfinish</code> when you are
  274. finished with it, in order to dispose of the structure and any
  275. associated memory blocks. This must be done even if the connection
  276. attempt fails or is abandoned.
  277. </p></dd><dt id="LIBPQ-PQCONNDEFAULTS"><span class="term"><code class="function">PQconndefaults</code><a id="id-1.7.3.8.2.12.6.1.2" class="indexterm"></a></span></dt><dd><p>
  278. Returns the default connection options.
  279. </p><pre class="synopsis">
  280. PQconninfoOption *PQconndefaults(void);
  281. typedef struct
  282. {
  283. char *keyword; /* The keyword of the option */
  284. char *envvar; /* Fallback environment variable name */
  285. char *compiled; /* Fallback compiled in default value */
  286. char *val; /* Option's current value, or NULL */
  287. char *label; /* Label for field in connect dialog */
  288. char *dispchar; /* Indicates how to display this field
  289. in a connect dialog. Values are:
  290. "" Display entered value as is
  291. "*" Password field - hide value
  292. "D" Debug option - don't show by default */
  293. int dispsize; /* Field size in characters for dialog */
  294. } PQconninfoOption;
  295. </pre><p>
  296. </p><p>
  297. Returns a connection options array. This can be used to determine
  298. all possible <code class="function">PQconnectdb</code> options and their
  299. current default values. The return value points to an array of
  300. <code class="structname">PQconninfoOption</code> structures, which ends
  301. with an entry having a null <code class="structfield">keyword</code> pointer. The
  302. null pointer is returned if memory could not be allocated. Note that
  303. the current default values (<code class="structfield">val</code> fields)
  304. will depend on environment variables and other context. A
  305. missing or invalid service file will be silently ignored. Callers
  306. must treat the connection options data as read-only.
  307. </p><p>
  308. After processing the options array, free it by passing it to
  309. <code class="function">PQconninfoFree</code>. If this is not done, a small amount of memory
  310. is leaked for each call to <code class="function">PQconndefaults</code>.
  311. </p></dd><dt id="LIBPQ-PQCONNINFO"><span class="term"><code class="function">PQconninfo</code><a id="id-1.7.3.8.2.12.7.1.2" class="indexterm"></a></span></dt><dd><p>
  312. Returns the connection options used by a live connection.
  313. </p><pre class="synopsis">
  314. PQconninfoOption *PQconninfo(PGconn *conn);
  315. </pre><p>
  316. </p><p>
  317. Returns a connection options array. This can be used to determine
  318. all possible <code class="function">PQconnectdb</code> options and the
  319. values that were used to connect to the server. The return
  320. value points to an array of <code class="structname">PQconninfoOption</code>
  321. structures, which ends with an entry having a null <code class="structfield">keyword</code>
  322. pointer. All notes above for <code class="function">PQconndefaults</code> also
  323. apply to the result of <code class="function">PQconninfo</code>.
  324. </p></dd><dt id="LIBPQ-PQCONNINFOPARSE"><span class="term"><code class="function">PQconninfoParse</code><a id="id-1.7.3.8.2.12.8.1.2" class="indexterm"></a></span></dt><dd><p>
  325. Returns parsed connection options from the provided connection string.
  326. </p><pre class="synopsis">
  327. PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
  328. </pre><p>
  329. </p><p>
  330. Parses a connection string and returns the resulting options as an
  331. array; or returns <code class="symbol">NULL</code> if there is a problem with the connection
  332. string. This function can be used to extract
  333. the <code class="function">PQconnectdb</code> options in the provided
  334. connection string. The return value points to an array of
  335. <code class="structname">PQconninfoOption</code> structures, which ends
  336. with an entry having a null <code class="structfield">keyword</code> pointer.
  337. </p><p>
  338. All legal options will be present in the result array, but the
  339. <code class="literal">PQconninfoOption</code> for any option not present
  340. in the connection string will have <code class="literal">val</code> set to
  341. <code class="literal">NULL</code>; default values are not inserted.
  342. </p><p>
  343. If <code class="literal">errmsg</code> is not <code class="symbol">NULL</code>, then <code class="literal">*errmsg</code> is set
  344. to <code class="symbol">NULL</code> on success, else to a <code class="function">malloc</code>'d error string explaining
  345. the problem. (It is also possible for <code class="literal">*errmsg</code> to be
  346. set to <code class="symbol">NULL</code> and the function to return <code class="symbol">NULL</code>;
  347. this indicates an out-of-memory condition.)
  348. </p><p>
  349. After processing the options array, free it by passing it to
  350. <code class="function">PQconninfoFree</code>. If this is not done, some memory
  351. is leaked for each call to <code class="function">PQconninfoParse</code>.
  352. Conversely, if an error occurs and <code class="literal">errmsg</code> is not <code class="symbol">NULL</code>,
  353. be sure to free the error string using <code class="function">PQfreemem</code>.
  354. </p></dd><dt id="LIBPQ-PQFINISH"><span class="term"><code class="function">PQfinish</code><a id="id-1.7.3.8.2.12.9.1.2" class="indexterm"></a></span></dt><dd><p>
  355. Closes the connection to the server. Also frees
  356. memory used by the <code class="structname">PGconn</code> object.
  357. </p><pre class="synopsis">
  358. void PQfinish(PGconn *conn);
  359. </pre><p>
  360. </p><p>
  361. Note that even if the server connection attempt fails (as
  362. indicated by <code class="function">PQstatus</code>), the application should call <code class="function">PQfinish</code>
  363. to free the memory used by the <code class="structname">PGconn</code> object.
  364. The <code class="structname">PGconn</code> pointer must not be used again after
  365. <code class="function">PQfinish</code> has been called.
  366. </p></dd><dt id="LIBPQ-PQRESET"><span class="term"><code class="function">PQreset</code><a id="id-1.7.3.8.2.12.10.1.2" class="indexterm"></a></span></dt><dd><p>
  367. Resets the communication channel to the server.
  368. </p><pre class="synopsis">
  369. void PQreset(PGconn *conn);
  370. </pre><p>
  371. </p><p>
  372. This function will close the connection
  373. to the server and attempt to reestablish a new
  374. connection to the same server, using all the same
  375. parameters previously used. This might be useful for
  376. error recovery if a working connection is lost.
  377. </p></dd><dt id="LIBPQ-PQRESETSTART"><span class="term"><code class="function">PQresetStart</code><a id="id-1.7.3.8.2.12.11.1.2" class="indexterm"></a><br /></span><span class="term"><code class="function">PQresetPoll</code><a id="id-1.7.3.8.2.12.11.2.2" class="indexterm"></a></span></dt><dd><p>
  378. Reset the communication channel to the server, in a nonblocking manner.
  379. </p><pre class="synopsis">
  380. int PQresetStart(PGconn *conn);
  381. PostgresPollingStatusType PQresetPoll(PGconn *conn);
  382. </pre><p>
  383. </p><p>
  384. These functions will close the connection to the server and attempt to
  385. reestablish a new connection to the same server, using all the same
  386. parameters previously used. This can be useful for error recovery if a
  387. working connection is lost. They differ from <code class="function">PQreset</code> (above) in that they
  388. act in a nonblocking manner. These functions suffer from the same
  389. restrictions as <code class="function">PQconnectStartParams</code>, <code class="function">PQconnectStart</code>
  390. and <code class="function">PQconnectPoll</code>.
  391. </p><p>
  392. To initiate a connection reset, call
  393. <code class="function">PQresetStart</code>. If it returns 0, the reset has
  394. failed. If it returns 1, poll the reset using
  395. <code class="function">PQresetPoll</code> in exactly the same way as you
  396. would create the connection using <code class="function">PQconnectPoll</code>.
  397. </p></dd><dt id="LIBPQ-PQPINGPARAMS"><span class="term"><code class="function">PQpingParams</code><a id="id-1.7.3.8.2.12.12.1.2" class="indexterm"></a></span></dt><dd><p>
  398. <code class="function">PQpingParams</code> reports the status of the
  399. server. It accepts connection parameters identical to those of
  400. <code class="function">PQconnectdbParams</code>, described above. It is not
  401. necessary to supply correct user name, password, or database name
  402. values to obtain the server status; however, if incorrect values
  403. are provided, the server will log a failed connection attempt.
  404. </p><pre class="synopsis">
  405. PGPing PQpingParams(const char * const *keywords,
  406. const char * const *values,
  407. int expand_dbname);
  408. </pre><p>
  409. The function returns one of the following values:
  410. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-PQPINGPARAMS-PQPING-OK"><span class="term"><code class="literal">PQPING_OK</code></span></dt><dd><p>
  411. The server is running and appears to be accepting connections.
  412. </p></dd><dt id="LIBPQ-PQPINGPARAMS-PQPING-REJECT"><span class="term"><code class="literal">PQPING_REJECT</code></span></dt><dd><p>
  413. The server is running but is in a state that disallows connections
  414. (startup, shutdown, or crash recovery).
  415. </p></dd><dt id="LIBPQ-PQPINGPARAMS-PQPING-NO-RESPONSE"><span class="term"><code class="literal">PQPING_NO_RESPONSE</code></span></dt><dd><p>
  416. The server could not be contacted. This might indicate that the
  417. server is not running, or that there is something wrong with the
  418. given connection parameters (for example, wrong port number), or
  419. that there is a network connectivity problem (for example, a
  420. firewall blocking the connection request).
  421. </p></dd><dt id="LIBPQ-PQPINGPARAMS-PQPING-NO-ATTEMPT"><span class="term"><code class="literal">PQPING_NO_ATTEMPT</code></span></dt><dd><p>
  422. No attempt was made to contact the server, because the supplied
  423. parameters were obviously incorrect or there was some client-side
  424. problem (for example, out of memory).
  425. </p></dd></dl></div><p>
  426. </p></dd><dt id="LIBPQ-PQPING"><span class="term"><code class="function">PQping</code><a id="id-1.7.3.8.2.12.13.1.2" class="indexterm"></a></span></dt><dd><p>
  427. <code class="function">PQping</code> reports the status of the
  428. server. It accepts connection parameters identical to those of
  429. <code class="function">PQconnectdb</code>, described above. It is not
  430. necessary to supply correct user name, password, or database name
  431. values to obtain the server status; however, if incorrect values
  432. are provided, the server will log a failed connection attempt.
  433. </p><pre class="synopsis">
  434. PGPing PQping(const char *conninfo);
  435. </pre><p>
  436. </p><p>
  437. The return values are the same as for <code class="function">PQpingParams</code>.
  438. </p></dd></dl></div><p>
  439. </p><div class="sect2" id="LIBPQ-CONNSTRING"><div class="titlepage"><div><div><h3 class="title">33.1.1. Connection Strings</h3></div></div></div><a id="id-1.7.3.8.3.2" class="indexterm"></a><a id="id-1.7.3.8.3.3" class="indexterm"></a><p>
  440. Several <span class="application">libpq</span> functions parse a user-specified string to obtain
  441. connection parameters. There are two accepted formats for these strings:
  442. plain <code class="literal">keyword = value</code> strings
  443. and URIs. URIs generally follow
  444. <a class="ulink" href="https://tools.ietf.org/html/rfc3986" target="_top">RFC
  445. 3986</a>, except that multi-host connection strings are allowed
  446. as further described below.
  447. </p><div class="sect3" id="id-1.7.3.8.3.5"><div class="titlepage"><div><div><h4 class="title">33.1.1.1. Keyword/Value Connection Strings</h4></div></div></div><p>
  448. In the first format, each parameter setting is in the form
  449. <code class="literal">keyword = value</code>. Spaces around the equal sign are
  450. optional. To write an empty value, or a value containing spaces, surround it
  451. with single quotes, e.g., <code class="literal">keyword = 'a value'</code>. Single
  452. quotes and backslashes within
  453. the value must be escaped with a backslash, i.e., <code class="literal">\'</code> and
  454. <code class="literal">\\</code>.
  455. </p><p>
  456. Example:
  457. </p><pre class="programlisting">
  458. host=localhost port=5432 dbname=mydb connect_timeout=10
  459. </pre><p>
  460. </p><p>
  461. The recognized parameter key words are listed in <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a>.
  462. </p></div><div class="sect3" id="id-1.7.3.8.3.6"><div class="titlepage"><div><div><h4 class="title">33.1.1.2. Connection URIs</h4></div></div></div><p>
  463. The general form for a connection <acronym class="acronym">URI</acronym> is:
  464. </p><pre class="synopsis">
  465. postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&amp;...]
  466. </pre><p>
  467. </p><p>
  468. The <acronym class="acronym">URI</acronym> scheme designator can be either
  469. <code class="literal">postgresql://</code> or <code class="literal">postgres://</code>. Each
  470. of the <acronym class="acronym">URI</acronym> parts is optional. The following examples
  471. illustrate valid <acronym class="acronym">URI</acronym> syntax uses:
  472. </p><pre class="programlisting">
  473. postgresql://
  474. postgresql://localhost
  475. postgresql://localhost:5433
  476. postgresql://localhost/mydb
  477. postgresql://user@localhost
  478. postgresql://user:secret@localhost
  479. postgresql://other@localhost/otherdb?connect_timeout=10&amp;application_name=myapp
  480. postgresql://host1:123,host2:456/somedb?target_session_attrs=any&amp;application_name=myapp
  481. </pre><p>
  482. Components of the hierarchical part of the <acronym class="acronym">URI</acronym> can also
  483. be given as parameters. For example:
  484. </p><pre class="programlisting">
  485. postgresql:///mydb?host=localhost&amp;port=5433
  486. </pre><p>
  487. </p><p>
  488. Percent-encoding may be used to include symbols with special meaning in any
  489. of the <acronym class="acronym">URI</acronym> parts, e.g. replace <code class="literal">=</code> with
  490. <code class="literal">%3D</code>.
  491. </p><p>
  492. Any connection parameters not corresponding to key words listed in <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a> are ignored and a warning message about them
  493. is sent to <code class="filename">stderr</code>.
  494. </p><p>
  495. For improved compatibility with JDBC connection <acronym class="acronym">URI</acronym>s,
  496. instances of parameter <code class="literal">ssl=true</code> are translated into
  497. <code class="literal">sslmode=require</code>.
  498. </p><p>
  499. The host part may be either host name or an IP address. To specify an
  500. IPv6 host address, enclose it in square brackets:
  501. </p><pre class="synopsis">
  502. postgresql://[2001:db8::1234]/database
  503. </pre><p>
  504. </p><p>
  505. The host component is interpreted as described for the parameter <a class="xref" href="libpq-connect.html#LIBPQ-CONNECT-HOST">host</a>. In particular, a Unix-domain socket
  506. connection is chosen if the host part is either empty or starts with a
  507. slash, otherwise a TCP/IP connection is initiated. Note, however, that the
  508. slash is a reserved character in the hierarchical part of the URI. So, to
  509. specify a non-standard Unix-domain socket directory, either omit the host
  510. specification in the URI and specify the host as a parameter, or
  511. percent-encode the path in the host component of the URI:
  512. </p><pre class="programlisting">
  513. postgresql:///dbname?host=/var/lib/postgresql
  514. postgresql://%2Fvar%2Flib%2Fpostgresql/dbname
  515. </pre><p>
  516. </p><p>
  517. It is possible to specify multiple host components, each with an optional
  518. port component, in a single URI. A URI of the form
  519. <code class="literal">postgresql://host1:port1,host2:port2,host3:port3/</code>
  520. is equivalent to a connection string of the form
  521. <code class="literal">host=host1,host2,host3 port=port1,port2,port3</code>. Each
  522. host will be tried in turn until a connection is successfully established.
  523. </p></div><div class="sect3" id="LIBPQ-MULTIPLE-HOSTS"><div class="titlepage"><div><div><h4 class="title">33.1.1.3. Specifying Multiple Hosts</h4></div></div></div><p>
  524. It is possible to specify multiple hosts to connect to, so that they are
  525. tried in the given order. In the Keyword/Value format, the <code class="literal">host</code>,
  526. <code class="literal">hostaddr</code>, and <code class="literal">port</code> options accept a comma-separated
  527. list of values. The same number of elements must be given in each
  528. option that is specified, such
  529. that e.g. the first <code class="literal">hostaddr</code> corresponds to the first host name,
  530. the second <code class="literal">hostaddr</code> corresponds to the second host name, and so
  531. forth. As an exception, if only one <code class="literal">port</code> is specified, it
  532. applies to all the hosts.
  533. </p><p>
  534. In the connection URI format, you can list multiple <code class="literal">host:port</code> pairs
  535. separated by commas, in the <code class="literal">host</code> component of the URI.
  536. </p><p>
  537. In either format, a single host name can translate to multiple network
  538. addresses. A common example of this is a host that has both an IPv4 and
  539. an IPv6 address.
  540. </p><p>
  541. When multiple hosts are specified, or when a single host name is
  542. translated to multiple addresses, all the hosts and addresses will be
  543. tried in order, until one succeeds. If none of the hosts can be reached,
  544. the connection fails. If a connection is established successfully, but
  545. authentication fails, the remaining hosts in the list are not tried.
  546. </p><p>
  547. If a password file is used, you can have different passwords for
  548. different hosts. All the other connection options are the same for every
  549. host in the list; it is not possible to e.g. specify different
  550. usernames for different hosts.
  551. </p></div></div><div class="sect2" id="LIBPQ-PARAMKEYWORDS"><div class="titlepage"><div><div><h3 class="title">33.1.2. Parameter Key Words</h3></div></div></div><p>
  552. The currently recognized parameter key words are:
  553. </p><div class="variablelist"><dl class="variablelist"><dt id="LIBPQ-CONNECT-HOST"><span class="term"><code class="literal">host</code></span></dt><dd><p>
  554. Name of host to connect to.<a id="id-1.7.3.8.4.2.1.1.2.1.1" class="indexterm"></a>
  555. If a host name begins with a slash, it specifies Unix-domain
  556. communication rather than TCP/IP communication; the value is the
  557. name of the directory in which the socket file is stored.
  558. The default behavior when <code class="literal">host</code> is
  559. not specified, or is empty, is to connect to a Unix-domain
  560. socket<a id="id-1.7.3.8.4.2.1.1.2.1.3" class="indexterm"></a> in
  561. <code class="filename">/tmp</code> (or whatever socket directory was specified
  562. when <span class="productname">PostgreSQL</span> was built). On machines without
  563. Unix-domain sockets, the default is to connect to <code class="literal">localhost</code>.
  564. </p><p>
  565. A comma-separated list of host names is also accepted, in which case
  566. each host name in the list is tried in order; an empty item in the
  567. list selects the default behavior as explained above. See
  568. <a class="xref" href="libpq-connect.html#LIBPQ-MULTIPLE-HOSTS" title="33.1.1.3. Specifying Multiple Hosts">Section 33.1.1.3</a> for details.
  569. </p></dd><dt id="LIBPQ-CONNECT-HOSTADDR"><span class="term"><code class="literal">hostaddr</code></span></dt><dd><p>
  570. Numeric IP address of host to connect to. This should be in the
  571. standard IPv4 address format, e.g., <code class="literal">172.28.40.9</code>. If
  572. your machine supports IPv6, you can also use those addresses.
  573. TCP/IP communication is
  574. always used when a nonempty string is specified for this parameter.
  575. </p><p>
  576. Using <code class="literal">hostaddr</code> instead of <code class="literal">host</code> allows the
  577. application to avoid a host name look-up, which might be important
  578. in applications with time constraints. However, a host name is
  579. required for GSSAPI or SSPI authentication
  580. methods, as well as for <code class="literal">verify-full</code> SSL
  581. certificate verification. The following rules are used:
  582. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  583. If <code class="literal">host</code> is specified
  584. without <code class="literal">hostaddr</code>, a host name lookup occurs.
  585. (When using <code class="function">PQconnectPoll</code>, the lookup occurs
  586. when <code class="function">PQconnectPoll</code> first considers this host
  587. name, and it may cause <code class="function">PQconnectPoll</code> to block
  588. for a significant amount of time.)
  589. </p></li><li class="listitem"><p>
  590. If <code class="literal">hostaddr</code> is specified without <code class="literal">host</code>,
  591. the value for <code class="literal">hostaddr</code> gives the server network address.
  592. The connection attempt will fail if the authentication
  593. method requires a host name.
  594. </p></li><li class="listitem"><p>
  595. If both <code class="literal">host</code> and <code class="literal">hostaddr</code> are specified,
  596. the value for <code class="literal">hostaddr</code> gives the server network address.
  597. The value for <code class="literal">host</code> is ignored unless the
  598. authentication method requires it, in which case it will be
  599. used as the host name.
  600. </p></li></ul></div><p>
  601. Note that authentication is likely to fail if <code class="literal">host</code>
  602. is not the name of the server at network address <code class="literal">hostaddr</code>.
  603. Also, when both <code class="literal">host</code> and <code class="literal">hostaddr</code>
  604. are specified, <code class="literal">host</code>
  605. is used to identify the connection in a password file (see
  606. <a class="xref" href="libpq-pgpass.html" title="33.15. The Password File">Section 33.15</a>).
  607. </p><p>
  608. A comma-separated list of <code class="literal">hostaddr</code> values is also
  609. accepted, in which case each host in the list is tried in order.
  610. An empty item in the list causes the corresponding host name to be
  611. used, or the default host name if that is empty as well. See
  612. <a class="xref" href="libpq-connect.html#LIBPQ-MULTIPLE-HOSTS" title="33.1.1.3. Specifying Multiple Hosts">Section 33.1.1.3</a> for details.
  613. </p><p>
  614. Without either a host name or host address,
  615. <span class="application">libpq</span> will connect using a
  616. local Unix-domain socket; or on machines without Unix-domain
  617. sockets, it will attempt to connect to <code class="literal">localhost</code>.
  618. </p></dd><dt id="LIBPQ-CONNECT-PORT"><span class="term"><code class="literal">port</code></span></dt><dd><p>
  619. Port number to connect to at the server host, or socket file
  620. name extension for Unix-domain
  621. connections.<a id="id-1.7.3.8.4.2.1.3.2.1.1" class="indexterm"></a>
  622. If multiple hosts were given in the <code class="literal">host</code> or
  623. <code class="literal">hostaddr</code> parameters, this parameter may specify a
  624. comma-separated list of ports of the same length as the host list, or
  625. it may specify a single port number to be used for all hosts.
  626. An empty string, or an empty item in a comma-separated list,
  627. specifies the default port number established
  628. when <span class="productname">PostgreSQL</span> was built.
  629. </p></dd><dt id="LIBPQ-CONNECT-DBNAME"><span class="term"><code class="literal">dbname</code></span></dt><dd><p>
  630. The database name. Defaults to be the same as the user name.
  631. In certain contexts, the value is checked for extended
  632. formats; see <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a> for more details on
  633. those.
  634. </p></dd><dt id="LIBPQ-CONNECT-USER"><span class="term"><code class="literal">user</code></span></dt><dd><p>
  635. <span class="productname">PostgreSQL</span> user name to connect as.
  636. Defaults to be the same as the operating system name of the user
  637. running the application.
  638. </p></dd><dt id="LIBPQ-CONNECT-PASSWORD"><span class="term"><code class="literal">password</code></span></dt><dd><p>
  639. Password to be used if the server demands password authentication.
  640. </p></dd><dt id="LIBPQ-CONNECT-PASSFILE"><span class="term"><code class="literal">passfile</code></span></dt><dd><p>
  641. Specifies the name of the file used to store passwords
  642. (see <a class="xref" href="libpq-pgpass.html" title="33.15. The Password File">Section 33.15</a>).
  643. Defaults to <code class="filename">~/.pgpass</code>, or
  644. <code class="filename">%APPDATA%\postgresql\pgpass.conf</code> on Microsoft Windows.
  645. (No error is reported if this file does not exist.)
  646. </p></dd><dt id="LIBPQ-CONNECT-CONNECT-TIMEOUT"><span class="term"><code class="literal">connect_timeout</code></span></dt><dd><p>
  647. Maximum wait for connection, in seconds (write as a decimal integer,
  648. e.g. <code class="literal">10</code>). Zero, negative, or not specified means
  649. wait indefinitely. The minimum allowed timeout is 2 seconds, therefore
  650. a value of <code class="literal">1</code> is interpreted as <code class="literal">2</code>.
  651. This timeout applies separately to each host name or IP address.
  652. For example, if you specify two hosts and <code class="literal">connect_timeout</code>
  653. is 5, each host will time out if no connection is made within 5
  654. seconds, so the total time spent waiting for a connection might be
  655. up to 10 seconds.
  656. </p></dd><dt id="LIBPQ-CONNECT-CLIENT-ENCODING"><span class="term"><code class="literal">client_encoding</code></span></dt><dd><p>
  657. This sets the <code class="varname">client_encoding</code>
  658. configuration parameter for this connection. In addition to
  659. the values accepted by the corresponding server option, you
  660. can use <code class="literal">auto</code> to determine the right
  661. encoding from the current locale in the client
  662. (<code class="envar">LC_CTYPE</code> environment variable on Unix
  663. systems).
  664. </p></dd><dt id="LIBPQ-CONNECT-OPTIONS"><span class="term"><code class="literal">options</code></span></dt><dd><p>
  665. Specifies command-line options to send to the server at connection
  666. start. For example, setting this to <code class="literal">-c geqo=off</code> sets the
  667. session's value of the <code class="varname">geqo</code> parameter to
  668. <code class="literal">off</code>. Spaces within this string are considered to
  669. separate command-line arguments, unless escaped with a backslash
  670. (<code class="literal">\</code>); write <code class="literal">\\</code> to represent a literal
  671. backslash. For a detailed discussion of the available
  672. options, consult <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a>.
  673. </p></dd><dt id="LIBPQ-CONNECT-APPLICATION-NAME"><span class="term"><code class="literal">application_name</code></span></dt><dd><p>
  674. Specifies a value for the <a class="xref" href="runtime-config-logging.html#GUC-APPLICATION-NAME">application_name</a>
  675. configuration parameter.
  676. </p></dd><dt id="LIBPQ-CONNECT-FALLBACK-APPLICATION-NAME"><span class="term"><code class="literal">fallback_application_name</code></span></dt><dd><p>
  677. Specifies a fallback value for the <a class="xref" href="runtime-config-logging.html#GUC-APPLICATION-NAME">application_name</a> configuration parameter.
  678. This value will be used if no value has been given for
  679. <code class="literal">application_name</code> via a connection parameter or the
  680. <code class="envar">PGAPPNAME</code> environment variable. Specifying
  681. a fallback name is useful in generic utility programs that
  682. wish to set a default application name but allow it to be
  683. overridden by the user.
  684. </p></dd><dt id="LIBPQ-KEEPALIVES"><span class="term"><code class="literal">keepalives</code></span></dt><dd><p>
  685. Controls whether client-side TCP keepalives are used. The default
  686. value is 1, meaning on, but you can change this to 0, meaning off,
  687. if keepalives are not wanted. This parameter is ignored for
  688. connections made via a Unix-domain socket.
  689. </p></dd><dt id="LIBPQ-KEEPALIVES-IDLE"><span class="term"><code class="literal">keepalives_idle</code></span></dt><dd><p>
  690. Controls the number of seconds of inactivity after which TCP should
  691. send a keepalive message to the server. A value of zero uses the
  692. system default. This parameter is ignored for connections made via a
  693. Unix-domain socket, or if keepalives are disabled.
  694. It is only supported on systems where <code class="symbol">TCP_KEEPIDLE</code> or
  695. an equivalent socket option is available, and on Windows; on other
  696. systems, it has no effect.
  697. </p></dd><dt id="LIBPQ-KEEPALIVES-INTERVAL"><span class="term"><code class="literal">keepalives_interval</code></span></dt><dd><p>
  698. Controls the number of seconds after which a TCP keepalive message
  699. that is not acknowledged by the server should be retransmitted. A
  700. value of zero uses the system default. This parameter is ignored for
  701. connections made via a Unix-domain socket, or if keepalives are disabled.
  702. It is only supported on systems where <code class="symbol">TCP_KEEPINTVL</code> or
  703. an equivalent socket option is available, and on Windows; on other
  704. systems, it has no effect.
  705. </p></dd><dt id="LIBPQ-KEEPALIVES-COUNT"><span class="term"><code class="literal">keepalives_count</code></span></dt><dd><p>
  706. Controls the number of TCP keepalives that can be lost before the
  707. client's connection to the server is considered dead. A value of
  708. zero uses the system default. This parameter is ignored for
  709. connections made via a Unix-domain socket, or if keepalives are disabled.
  710. It is only supported on systems where <code class="symbol">TCP_KEEPCNT</code> or
  711. an equivalent socket option is available; on other systems, it has no
  712. effect.
  713. </p></dd><dt id="LIBPQ-TCP-USER-TIMEOUT"><span class="term"><code class="literal">tcp_user_timeout</code></span></dt><dd><p>
  714. Controls the number of milliseconds that transmitted data may
  715. remain unacknowledged before a connection is forcibly closed.
  716. A value of zero uses the system default. This parameter is
  717. ignored for connections made via a Unix-domain socket.
  718. It is only supported on systems where <code class="symbol">TCP_USER_TIMEOUT</code>
  719. is available; on other systems, it has no effect.
  720. </p></dd><dt id="LIBPQ-CONNECT-TTY"><span class="term"><code class="literal">tty</code></span></dt><dd><p>
  721. Ignored (formerly, this specified where to send server debug output).
  722. </p></dd><dt id="LIBPQ-CONNECT-REPLICATION"><span class="term"><code class="literal">replication</code></span></dt><dd><p>
  723. This option determines whether the connection should use the
  724. replication protocol instead of the normal protocol. This is what
  725. PostgreSQL replication connections as well as tools such as
  726. <span class="application">pg_basebackup</span> use internally, but it can
  727. also be used by third-party applications. For a description of the
  728. replication protocol, consult <a class="xref" href="protocol-replication.html" title="52.4. Streaming Replication Protocol">Section 52.4</a>.
  729. </p><p>
  730. The following values, which are case-insensitive, are supported:
  731. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">
  732. <code class="literal">true</code>, <code class="literal">on</code>,
  733. <code class="literal">yes</code>, <code class="literal">1</code>
  734. </span></dt><dd><p>
  735. The connection goes into physical replication mode.
  736. </p></dd><dt><span class="term"><code class="literal">database</code></span></dt><dd><p>
  737. The connection goes into logical replication mode, connecting to
  738. the database specified in the <code class="literal">dbname</code> parameter.
  739. </p></dd><dt><span class="term">
  740. <code class="literal">false</code>, <code class="literal">off</code>,
  741. <code class="literal">no</code>, <code class="literal">0</code>
  742. </span></dt><dd><p>
  743. The connection is a regular one, which is the default behavior.
  744. </p></dd></dl></div><p>
  745. </p><p>
  746. In physical or logical replication mode, only the simple query protocol
  747. can be used.
  748. </p></dd><dt id="LIBPQ-CONNECT-GSSENCMODE"><span class="term"><code class="literal">gssencmode</code></span></dt><dd><p>
  749. This option determines whether or with what priority a secure
  750. <acronym class="acronym">GSS</acronym> TCP/IP connection will be negotiated with the
  751. server. There are three modes:
  752. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">disable</code></span></dt><dd><p>
  753. only try a non-<acronym class="acronym">GSSAPI</acronym>-encrypted connection
  754. </p></dd><dt><span class="term"><code class="literal">prefer</code> (default)</span></dt><dd><p>
  755. if there are <acronym class="acronym">GSSAPI</acronym> credentials present (i.e.,
  756. in a credentials cache), first try
  757. a <acronym class="acronym">GSSAPI</acronym>-encrypted connection; if that fails or
  758. there are no credentials, try a
  759. non-<acronym class="acronym">GSSAPI</acronym>-encrypted connection. This is the
  760. default when <span class="productname">PostgreSQL</span> has been
  761. compiled with <acronym class="acronym">GSSAPI</acronym> support.
  762. </p></dd><dt><span class="term"><code class="literal">require</code></span></dt><dd><p>
  763. only try a <acronym class="acronym">GSSAPI</acronym>-encrypted connection
  764. </p></dd></dl></div><p>
  765. </p><p>
  766. <code class="literal">gssencmode</code> is ignored for Unix domain socket
  767. communication. If <span class="productname">PostgreSQL</span> is compiled
  768. without GSSAPI support, using the <code class="literal">require</code> option
  769. will cause an error, while <code class="literal">prefer</code> will be accepted
  770. but <span class="application">libpq</span> will not actually attempt
  771. a <acronym class="acronym">GSSAPI</acronym>-encrypted
  772. connection.<a id="id-1.7.3.8.4.2.1.20.2.2.7" class="indexterm"></a>
  773. </p></dd><dt id="LIBPQ-CONNECT-SSLMODE"><span class="term"><code class="literal">sslmode</code></span></dt><dd><p>
  774. This option determines whether or with what priority a secure
  775. <acronym class="acronym">SSL</acronym> TCP/IP connection will be negotiated with the
  776. server. There are six modes:
  777. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">disable</code></span></dt><dd><p>
  778. only try a non-<acronym class="acronym">SSL</acronym> connection
  779. </p></dd><dt><span class="term"><code class="literal">allow</code></span></dt><dd><p>
  780. first try a non-<acronym class="acronym">SSL</acronym> connection; if that
  781. fails, try an <acronym class="acronym">SSL</acronym> connection
  782. </p></dd><dt><span class="term"><code class="literal">prefer</code> (default)</span></dt><dd><p>
  783. first try an <acronym class="acronym">SSL</acronym> connection; if that fails,
  784. try a non-<acronym class="acronym">SSL</acronym> connection
  785. </p></dd><dt><span class="term"><code class="literal">require</code></span></dt><dd><p>
  786. only try an <acronym class="acronym">SSL</acronym> connection. If a root CA
  787. file is present, verify the certificate in the same way as
  788. if <code class="literal">verify-ca</code> was specified
  789. </p></dd><dt><span class="term"><code class="literal">verify-ca</code></span></dt><dd><p>
  790. only try an <acronym class="acronym">SSL</acronym> connection, and verify that
  791. the server certificate is issued by a trusted
  792. certificate authority (<acronym class="acronym">CA</acronym>)
  793. </p></dd><dt><span class="term"><code class="literal">verify-full</code></span></dt><dd><p>
  794. only try an <acronym class="acronym">SSL</acronym> connection, verify that the
  795. server certificate is issued by a
  796. trusted <acronym class="acronym">CA</acronym> and that the requested server host name
  797. matches that in the certificate
  798. </p></dd></dl></div><p>
  799. See <a class="xref" href="libpq-ssl.html" title="33.18. SSL Support">Section 33.18</a> for a detailed description of how
  800. these options work.
  801. </p><p>
  802. <code class="literal">sslmode</code> is ignored for Unix domain socket
  803. communication.
  804. If <span class="productname">PostgreSQL</span> is compiled without SSL support,
  805. using options <code class="literal">require</code>, <code class="literal">verify-ca</code>, or
  806. <code class="literal">verify-full</code> will cause an error, while
  807. options <code class="literal">allow</code> and <code class="literal">prefer</code> will be
  808. accepted but <span class="application">libpq</span> will not actually attempt
  809. an <acronym class="acronym">SSL</acronym>
  810. connection.<a id="id-1.7.3.8.4.2.1.21.2.2.10" class="indexterm"></a>
  811. </p></dd><dt id="LIBPQ-CONNECT-REQUIRESSL"><span class="term"><code class="literal">requiressl</code></span></dt><dd><p>
  812. This option is deprecated in favor of the <code class="literal">sslmode</code>
  813. setting.
  814. </p><p>
  815. If set to 1, an <acronym class="acronym">SSL</acronym> connection to the server
  816. is required (this is equivalent to <code class="literal">sslmode</code>
  817. <code class="literal">require</code>). <span class="application">libpq</span> will then refuse
  818. to connect if the server does not accept an
  819. <acronym class="acronym">SSL</acronym> connection. If set to 0 (default),
  820. <span class="application">libpq</span> will negotiate the connection type with
  821. the server (equivalent to <code class="literal">sslmode</code>
  822. <code class="literal">prefer</code>). This option is only available if
  823. <span class="productname">PostgreSQL</span> is compiled with SSL support.
  824. </p></dd><dt id="LIBPQ-CONNECT-SSLCOMPRESSION"><span class="term"><code class="literal">sslcompression</code></span></dt><dd><p>
  825. If set to 1, data sent over SSL connections will be compressed. If
  826. set to 0, compression will be disabled. The default is 0. This
  827. parameter is ignored if a connection without SSL is made.
  828. </p><p>
  829. SSL compression is nowadays considered insecure and its use is no
  830. longer recommended. <span class="productname">OpenSSL</span> 1.1.0 disables
  831. compression by default, and many operating system distributions
  832. disable it in prior versions as well, so setting this parameter to on
  833. will not have any effect if the server does not accept compression.
  834. On the other hand, <span class="productname">OpenSSL</span> before 1.0.0
  835. does not support disabling compression, so this parameter is ignored
  836. with those versions, and whether compression is used depends on the
  837. server.
  838. </p><p>
  839. If security is not a primary concern, compression can improve
  840. throughput if the network is the bottleneck. Disabling compression
  841. can improve response time and throughput if CPU performance is the
  842. limiting factor.
  843. </p></dd><dt id="LIBPQ-CONNECT-SSLCERT"><span class="term"><code class="literal">sslcert</code></span></dt><dd><p>
  844. This parameter specifies the file name of the client SSL
  845. certificate, replacing the default
  846. <code class="filename">~/.postgresql/postgresql.crt</code>.
  847. This parameter is ignored if an SSL connection is not made.
  848. </p></dd><dt id="LIBPQ-CONNECT-SSLKEY"><span class="term"><code class="literal">sslkey</code></span></dt><dd><p>
  849. This parameter specifies the location for the secret key used for
  850. the client certificate. It can either specify a file name that will
  851. be used instead of the default
  852. <code class="filename">~/.postgresql/postgresql.key</code>, or it can specify a key
  853. obtained from an external <span class="quote">“<span class="quote">engine</span>”</span> (engines are
  854. <span class="productname">OpenSSL</span> loadable modules). An external engine
  855. specification should consist of a colon-separated engine name and
  856. an engine-specific key identifier. This parameter is ignored if an
  857. SSL connection is not made.
  858. </p></dd><dt id="LIBPQ-CONNECT-SSLROOTCERT"><span class="term"><code class="literal">sslrootcert</code></span></dt><dd><p>
  859. This parameter specifies the name of a file containing SSL
  860. certificate authority (<acronym class="acronym">CA</acronym>) certificate(s).
  861. If the file exists, the server's certificate will be verified
  862. to be signed by one of these authorities. The default is
  863. <code class="filename">~/.postgresql/root.crt</code>.
  864. </p></dd><dt id="LIBPQ-CONNECT-SSLCRL"><span class="term"><code class="literal">sslcrl</code></span></dt><dd><p>
  865. This parameter specifies the file name of the SSL certificate
  866. revocation list (CRL). Certificates listed in this file, if it
  867. exists, will be rejected while attempting to authenticate the
  868. server's certificate. The default is
  869. <code class="filename">~/.postgresql/root.crl</code>.
  870. </p></dd><dt id="LIBPQ-CONNECT-REQUIREPEER"><span class="term"><code class="literal">requirepeer</code></span></dt><dd><p>
  871. This parameter specifies the operating-system user name of the
  872. server, for example <code class="literal">requirepeer=postgres</code>.
  873. When making a Unix-domain socket connection, if this
  874. parameter is set, the client checks at the beginning of the
  875. connection that the server process is running under the specified
  876. user name; if it is not, the connection is aborted with an error.
  877. This parameter can be used to provide server authentication similar
  878. to that available with SSL certificates on TCP/IP connections.
  879. (Note that if the Unix-domain socket is in
  880. <code class="filename">/tmp</code> or another publicly writable location,
  881. any user could start a server listening there. Use this parameter
  882. to ensure that you are connected to a server run by a trusted user.)
  883. This option is only supported on platforms for which the
  884. <code class="literal">peer</code> authentication method is implemented; see
  885. <a class="xref" href="auth-peer.html" title="20.9. Peer Authentication">Section 20.9</a>.
  886. </p></dd><dt id="LIBPQ-CONNECT-KRBSRVNAME"><span class="term"><code class="literal">krbsrvname</code></span></dt><dd><p>
  887. Kerberos service name to use when authenticating with GSSAPI.
  888. This must match the service name specified in the server
  889. configuration for Kerberos authentication to succeed. (See also
  890. <a class="xref" href="gssapi-auth.html" title="20.6. GSSAPI Authentication">Section 20.6</a>.)
  891. </p></dd><dt id="LIBPQ-CONNECT-GSSLIB"><span class="term"><code class="literal">gsslib</code></span></dt><dd><p>
  892. GSS library to use for GSSAPI authentication.
  893. Currently this is disregarded except on Windows builds that include
  894. both GSSAPI and SSPI support. In that case, set
  895. this to <code class="literal">gssapi</code> to cause libpq to use the GSSAPI
  896. library for authentication instead of the default SSPI.
  897. </p></dd><dt id="LIBPQ-CONNECT-SERVICE"><span class="term"><code class="literal">service</code></span></dt><dd><p>
  898. Service name to use for additional parameters. It specifies a service
  899. name in <code class="filename">pg_service.conf</code> that holds additional connection parameters.
  900. This allows applications to specify only a service name so connection parameters
  901. can be centrally maintained. See <a class="xref" href="libpq-pgservice.html" title="33.16. The Connection Service File">Section 33.16</a>.
  902. </p></dd><dt id="LIBPQ-CONNECT-TARGET-SESSION-ATTRS"><span class="term"><code class="literal">target_session_attrs</code></span></dt><dd><p>
  903. If this parameter is set to <code class="literal">read-write</code>, only a
  904. connection in which read-write transactions are accepted by default
  905. is considered acceptable. The query
  906. <code class="literal">SHOW transaction_read_only</code> will be sent upon any
  907. successful connection; if it returns <code class="literal">on</code>, the connection
  908. will be closed. If multiple hosts were specified in the connection
  909. string, any remaining servers will be tried just as if the connection
  910. attempt had failed. The default value of this parameter,
  911. <code class="literal">any</code>, regards all connections as acceptable.
  912. </p></dd></dl></div><p>
  913. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="libpq.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="libpq.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="libpq-status.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 33. <span class="application">libpq</span> - C Library </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 33.2. Connection Status Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1