gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

2687 líneas
195KB

  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>psql</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="app-pgrestore.html" title="pg_restore" /><link rel="next" href="app-reindexdb.html" title="reindexdb" /></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"><span xmlns="http://www.w3.org/1999/xhtml" class="application">psql</span></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-pgrestore.html" title="pg_restore">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</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="app-reindexdb.html" title="reindexdb">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PSQL"><div class="titlepage"></div><a id="id-1.9.4.18.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle"><span class="application">psql</span></span></h2><p><span class="application">psql</span> —
  3. <span class="productname">PostgreSQL</span> interactive terminal
  4. </p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.18.4.1"><code class="command">psql</code> [<em class="replaceable"><code>option</code></em>...] [<em class="replaceable"><code>dbname</code></em>
  5. [<em class="replaceable"><code>username</code></em>]]</p></div></div><div class="refsect1" id="id-1.9.4.18.5"><h2>Description</h2><p>
  6. <span class="application">psql</span> is a terminal-based front-end to
  7. <span class="productname">PostgreSQL</span>. It enables you to type in
  8. queries interactively, issue them to
  9. <span class="productname">PostgreSQL</span>, and see the query results.
  10. Alternatively, input can be from a file or from command line
  11. arguments. In addition, <span class="application">psql</span> provides a
  12. number of meta-commands and various shell-like features to
  13. facilitate writing scripts and automating a wide variety of tasks.
  14. </p></div><div class="refsect1" id="R1-APP-PSQL-3"><h2>Options</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-a</code><br /></span><span class="term"><code class="option">--echo-all</code></span></dt><dd><p>
  15. Print all nonempty input lines to standard output as they are read.
  16. (This does not apply to lines read interactively.) This is
  17. equivalent to setting the variable <code class="varname">ECHO</code> to
  18. <code class="literal">all</code>.
  19. </p></dd><dt><span class="term"><code class="option">-A</code><br /></span><span class="term"><code class="option">--no-align</code></span></dt><dd><p>
  20. Switches to unaligned output mode. (The default output mode is
  21. <code class="literal">aligned</code>.) This is equivalent to
  22. <code class="command">\pset format unaligned</code>.
  23. </p></dd><dt><span class="term"><code class="option">-b</code><br /></span><span class="term"><code class="option">--echo-errors</code></span></dt><dd><p>
  24. Print failed SQL commands to standard error output. This is
  25. equivalent to setting the variable <code class="varname">ECHO</code> to
  26. <code class="literal">errors</code>.
  27. </p></dd><dt><span class="term"><code class="option">-c <em class="replaceable"><code>command</code></em></code><br /></span><span class="term"><code class="option">--command=<em class="replaceable"><code>command</code></em></code></span></dt><dd><p>
  28. Specifies that <span class="application">psql</span> is to execute the given
  29. command string, <em class="replaceable"><code>command</code></em>.
  30. This option can be repeated and combined in any order with
  31. the <code class="option">-f</code> option. When either <code class="option">-c</code>
  32. or <code class="option">-f</code> is specified, <span class="application">psql</span>
  33. does not read commands from standard input; instead it terminates
  34. after processing all the <code class="option">-c</code> and <code class="option">-f</code>
  35. options in sequence.
  36. </p><p>
  37. <em class="replaceable"><code>command</code></em> must be either
  38. a command string that is completely parsable by the server (i.e.,
  39. it contains no <span class="application">psql</span>-specific features),
  40. or a single backslash command. Thus you cannot mix
  41. <acronym class="acronym">SQL</acronym> and <span class="application">psql</span>
  42. meta-commands within a <code class="option">-c</code> option. To achieve that,
  43. you could use repeated <code class="option">-c</code> options or pipe the string
  44. into <span class="application">psql</span>, for example:
  45. </p><pre class="programlisting">
  46. psql -c '\x' -c 'SELECT * FROM foo;'
  47. </pre><p>
  48. or
  49. </p><pre class="programlisting">
  50. echo '\x \\ SELECT * FROM foo;' | psql
  51. </pre><p>
  52. (<code class="literal">\\</code> is the separator meta-command.)
  53. </p><p>
  54. Each <acronym class="acronym">SQL</acronym> command string passed
  55. to <code class="option">-c</code> is sent to the server as a single request.
  56. Because of this, the server executes it as a single transaction even
  57. if the string contains multiple <acronym class="acronym">SQL</acronym> commands,
  58. unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
  59. commands included in the string to divide it into multiple
  60. transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="52.2.2.1. Multiple Statements in a Simple Query">Section 52.2.2.1</a>
  61. for more details about how the server handles multi-query strings.)
  62. Also, <span class="application">psql</span> only prints the
  63. result of the last <acronym class="acronym">SQL</acronym> command in the string.
  64. This is different from the behavior when the same string is read from
  65. a file or fed to <span class="application">psql</span>'s standard input,
  66. because then <span class="application">psql</span> sends
  67. each <acronym class="acronym">SQL</acronym> command separately.
  68. </p><p>
  69. Because of this behavior, putting more than one SQL command in a
  70. single <code class="option">-c</code> string often has unexpected results.
  71. It's better to use repeated <code class="option">-c</code> commands or feed
  72. multiple commands to <span class="application">psql</span>'s standard input,
  73. either using <span class="application">echo</span> as illustrated above, or
  74. via a shell here-document, for example:
  75. </p><pre class="programlisting">
  76. psql &lt;&lt;EOF
  77. \x
  78. SELECT * FROM foo;
  79. EOF
  80. </pre><p>
  81. </p></dd><dt><span class="term"><code class="option">--csv</code></span></dt><dd><p>
  82. Switches to <acronym class="acronym">CSV</acronym> (Comma-Separated Values) output
  83. mode. This is equivalent to <code class="command">\pset format csv</code>.
  84. </p></dd><dt><span class="term"><code class="option">-d <em class="replaceable"><code>dbname</code></em></code><br /></span><span class="term"><code class="option">--dbname=<em class="replaceable"><code>dbname</code></em></code></span></dt><dd><p>
  85. Specifies the name of the database to connect to. This is
  86. equivalent to specifying <em class="replaceable"><code>dbname</code></em> as the first non-option
  87. argument on the command line.
  88. </p><p>
  89. If this parameter contains an <code class="symbol">=</code> sign or starts
  90. with a valid <acronym class="acronym">URI</acronym> prefix
  91. (<code class="literal">postgresql://</code>
  92. or <code class="literal">postgres://</code>), it is treated as a
  93. <em class="parameter"><code>conninfo</code></em> string. See <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a> for more information.
  94. </p></dd><dt><span class="term"><code class="option">-e</code><br /></span><span class="term"><code class="option">--echo-queries</code></span></dt><dd><p>
  95. Copy all SQL commands sent to the server to standard output as well.
  96. This is equivalent
  97. to setting the variable <code class="varname">ECHO</code> to
  98. <code class="literal">queries</code>.
  99. </p></dd><dt><span class="term"><code class="option">-E</code><br /></span><span class="term"><code class="option">--echo-hidden</code></span></dt><dd><p>
  100. Echo the actual queries generated by <code class="command">\d</code> and other backslash
  101. commands. You can use this to study <span class="application">psql</span>'s
  102. internal operations. This is equivalent to
  103. setting the variable <code class="varname">ECHO_HIDDEN</code> to <code class="literal">on</code>.
  104. </p></dd><dt><span class="term"><code class="option">-f <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--file=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
  105. Read commands from the
  106. file <em class="replaceable"><code>filename</code></em>,
  107. rather than standard input.
  108. This option can be repeated and combined in any order with
  109. the <code class="option">-c</code> option. When either <code class="option">-c</code>
  110. or <code class="option">-f</code> is specified, <span class="application">psql</span>
  111. does not read commands from standard input; instead it terminates
  112. after processing all the <code class="option">-c</code> and <code class="option">-f</code>
  113. options in sequence.
  114. Except for that, this option is largely equivalent to the
  115. meta-command <code class="command">\i</code>.
  116. </p><p>
  117. If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
  118. (hyphen), then standard input is read until an EOF indication
  119. or <code class="command">\q</code> meta-command. This can be used to intersperse
  120. interactive input with input from files. Note however that Readline
  121. is not used in this case (much as if <code class="option">-n</code> had been
  122. specified).
  123. </p><p>
  124. Using this option is subtly different from writing <code class="literal">psql
  125. &lt; <em class="replaceable"><code>filename</code></em></code>. In general,
  126. both will do what you expect, but using <code class="literal">-f</code>
  127. enables some nice features such as error messages with line
  128. numbers. There is also a slight chance that using this option will
  129. reduce the start-up overhead. On the other hand, the variant using
  130. the shell's input redirection is (in theory) guaranteed to yield
  131. exactly the same output you would have received had you entered
  132. everything by hand.
  133. </p></dd><dt><span class="term"><code class="option">-F <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--field-separator=<em class="replaceable"><code>separator</code></em></code></span></dt><dd><p>
  134. Use <em class="replaceable"><code>separator</code></em> as the
  135. field separator for unaligned output. This is equivalent to
  136. <code class="command">\pset fieldsep</code> or <code class="command">\f</code>.
  137. </p></dd><dt><span class="term"><code class="option">-h <em class="replaceable"><code>hostname</code></em></code><br /></span><span class="term"><code class="option">--host=<em class="replaceable"><code>hostname</code></em></code></span></dt><dd><p>
  138. Specifies the host name of the machine on which the
  139. server is running. If the value begins
  140. with a slash, it is used as the directory for the Unix-domain
  141. socket.
  142. </p></dd><dt><span class="term"><code class="option">-H</code><br /></span><span class="term"><code class="option">--html</code></span></dt><dd><p>
  143. Switches to <acronym class="acronym">HTML</acronym> output mode. This is
  144. equivalent to <code class="command">\pset format html</code> or the
  145. <code class="command">\H</code> command.
  146. </p></dd><dt><span class="term"><code class="option">-l</code><br /></span><span class="term"><code class="option">--list</code></span></dt><dd><p>
  147. List all available databases, then exit. Other non-connection
  148. options are ignored. This is similar to the meta-command
  149. <code class="command">\list</code>.
  150. </p><p>
  151. When this option is used, <span class="application">psql</span> will connect
  152. to the database <code class="literal">postgres</code>, unless a different database
  153. is named on the command line (option <code class="option">-d</code> or non-option
  154. argument, possibly via a service entry, but not via an environment
  155. variable).
  156. </p></dd><dt><span class="term"><code class="option">-L <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--log-file=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
  157. Write all query output into file <em class="replaceable"><code>filename</code></em>, in addition to the
  158. normal output destination.
  159. </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-readline</code></span></dt><dd><p>
  160. Do not use <span class="application">Readline</span> for line editing and do
  161. not use the command history.
  162. This can be useful to turn off tab expansion when cutting and pasting.
  163. </p></dd><dt><span class="term"><code class="option">-o <em class="replaceable"><code>filename</code></em></code><br /></span><span class="term"><code class="option">--output=<em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
  164. Put all query output into file <em class="replaceable"><code>filename</code></em>. This is equivalent to
  165. the command <code class="command">\o</code>.
  166. </p></dd><dt><span class="term"><code class="option">-p <em class="replaceable"><code>port</code></em></code><br /></span><span class="term"><code class="option">--port=<em class="replaceable"><code>port</code></em></code></span></dt><dd><p>
  167. Specifies the TCP port or the local Unix-domain
  168. socket file extension on which the server is listening for
  169. connections. Defaults to the value of the <code class="envar">PGPORT</code>
  170. environment variable or, if not set, to the port specified at
  171. compile time, usually 5432.
  172. </p></dd><dt><span class="term"><code class="option">-P <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--pset=<em class="replaceable"><code>assignment</code></em></code></span></dt><dd><p>
  173. Specifies printing options, in the style of
  174. <code class="command">\pset</code>. Note that here you
  175. have to separate name and value with an equal sign instead of a
  176. space. For example, to set the output format to <span class="application">LaTeX</span>, you could write
  177. <code class="literal">-P format=latex</code>.
  178. </p></dd><dt><span class="term"><code class="option">-q</code><br /></span><span class="term"><code class="option">--quiet</code></span></dt><dd><p>
  179. Specifies that <span class="application">psql</span> should do its work
  180. quietly. By default, it prints welcome messages and various
  181. informational output. If this option is used, none of this
  182. happens. This is useful with the <code class="option">-c</code> option.
  183. This is equivalent to setting the variable <code class="varname">QUIET</code>
  184. to <code class="literal">on</code>.
  185. </p></dd><dt><span class="term"><code class="option">-R <em class="replaceable"><code>separator</code></em></code><br /></span><span class="term"><code class="option">--record-separator=<em class="replaceable"><code>separator</code></em></code></span></dt><dd><p>
  186. Use <em class="replaceable"><code>separator</code></em> as the
  187. record separator for unaligned output. This is equivalent to
  188. <code class="command">\pset recordsep</code>.
  189. </p></dd><dt><span class="term"><code class="option">-s</code><br /></span><span class="term"><code class="option">--single-step</code></span></dt><dd><p>
  190. Run in single-step mode. That means the user is prompted before
  191. each command is sent to the server, with the option to cancel
  192. execution as well. Use this to debug scripts.
  193. </p></dd><dt><span class="term"><code class="option">-S</code><br /></span><span class="term"><code class="option">--single-line</code></span></dt><dd><p>
  194. Runs in single-line mode where a newline terminates an SQL command, as a
  195. semicolon does.
  196. </p><div class="note"><h3 class="title">Note</h3><p>
  197. This mode is provided for those who insist on it, but you are not
  198. necessarily encouraged to use it. In particular, if you mix
  199. <acronym class="acronym">SQL</acronym> and meta-commands on a line the order of
  200. execution might not always be clear to the inexperienced user.
  201. </p></div></dd><dt><span class="term"><code class="option">-t</code><br /></span><span class="term"><code class="option">--tuples-only</code></span></dt><dd><p>
  202. Turn off printing of column names and result row count footers,
  203. etc. This is equivalent to <code class="command">\t</code> or
  204. <code class="command">\pset tuples_only</code>.
  205. </p></dd><dt><span class="term"><code class="option">-T <em class="replaceable"><code>table_options</code></em></code><br /></span><span class="term"><code class="option">--table-attr=<em class="replaceable"><code>table_options</code></em></code></span></dt><dd><p>
  206. Specifies options to be placed within the
  207. <acronym class="acronym">HTML</acronym> <code class="sgmltag-element">table</code> tag. See
  208. <code class="command">\pset tableattr</code> for details.
  209. </p></dd><dt><span class="term"><code class="option">-U <em class="replaceable"><code>username</code></em></code><br /></span><span class="term"><code class="option">--username=<em class="replaceable"><code>username</code></em></code></span></dt><dd><p>
  210. Connect to the database as the user <em class="replaceable"><code>username</code></em> instead of the default.
  211. (You must have permission to do so, of course.)
  212. </p></dd><dt><span class="term"><code class="option">-v <em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--set=<em class="replaceable"><code>assignment</code></em></code><br /></span><span class="term"><code class="option">--variable=<em class="replaceable"><code>assignment</code></em></code></span></dt><dd><p>
  213. Perform a variable assignment, like the <code class="command">\set</code>
  214. meta-command. Note that you must separate name and value, if
  215. any, by an equal sign on the command line. To unset a variable,
  216. leave off the equal sign. To set a variable with an empty value,
  217. use the equal sign but leave off the value. These assignments are
  218. done during command line processing, so variables that reflect
  219. connection state will get overwritten later.
  220. </p></dd><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
  221. Print the <span class="application">psql</span> version and exit.
  222. </p></dd><dt><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span></dt><dd><p>
  223. Never issue a password prompt. If the server requires password
  224. authentication and a password is not available by other means
  225. such as a <code class="filename">.pgpass</code> file, the connection
  226. attempt will fail. This option can be useful in batch jobs and
  227. scripts where no user is present to enter a password.
  228. </p><p>
  229. Note that this option will remain set for the entire session,
  230. and so it affects uses of the meta-command
  231. <code class="command">\connect</code> as well as the initial connection attempt.
  232. </p></dd><dt><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span></dt><dd><p>
  233. Force <span class="application">psql</span> to prompt for a
  234. password before connecting to a database.
  235. </p><p>
  236. This option is never essential, since <span class="application">psql</span>
  237. will automatically prompt for a password if the server demands
  238. password authentication. However, <span class="application">psql</span>
  239. will waste a connection attempt finding out that the server wants a
  240. password. In some cases it is worth typing <code class="option">-W</code> to avoid
  241. the extra connection attempt.
  242. </p><p>
  243. Note that this option will remain set for the entire session,
  244. and so it affects uses of the meta-command
  245. <code class="command">\connect</code> as well as the initial connection attempt.
  246. </p></dd><dt><span class="term"><code class="option">-x</code><br /></span><span class="term"><code class="option">--expanded</code></span></dt><dd><p>
  247. Turn on the expanded table formatting mode. This is equivalent to
  248. <code class="command">\x</code> or <code class="command">\pset expanded</code>.
  249. </p></dd><dt><span class="term"><code class="option">-X,</code><br /></span><span class="term"><code class="option">--no-psqlrc</code></span></dt><dd><p>
  250. Do not read the start-up file (neither the system-wide
  251. <code class="filename">psqlrc</code> file nor the user's
  252. <code class="filename">~/.psqlrc</code> file).
  253. </p></dd><dt><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--field-separator-zero</code></span></dt><dd><p>
  254. Set the field separator for unaligned output to a zero byte. This is
  255. equivalent to <code class="command">\pset fieldsep_zero</code>.
  256. </p></dd><dt><span class="term"><code class="option">-0</code><br /></span><span class="term"><code class="option">--record-separator-zero</code></span></dt><dd><p>
  257. Set the record separator for unaligned output to a zero byte. This is
  258. useful for interfacing, for example, with <code class="literal">xargs -0</code>.
  259. This is equivalent to <code class="command">\pset recordsep_zero</code>.
  260. </p></dd><dt><span class="term"><code class="option">-1</code><br /></span><span class="term"><code class="option">--single-transaction</code></span></dt><dd><p>
  261. This option can only be used in combination with one or more
  262. <code class="option">-c</code> and/or <code class="option">-f</code> options. It causes
  263. <span class="application">psql</span> to issue a <code class="command">BEGIN</code> command
  264. before the first such option and a <code class="command">COMMIT</code> command after
  265. the last one, thereby wrapping all the commands into a single
  266. transaction. This ensures that either all the commands complete
  267. successfully, or no changes are applied.
  268. </p><p>
  269. If the commands themselves
  270. contain <code class="command">BEGIN</code>, <code class="command">COMMIT</code>,
  271. or <code class="command">ROLLBACK</code>, this option will not have the desired
  272. effects. Also, if an individual command cannot be executed inside a
  273. transaction block, specifying this option will cause the whole
  274. transaction to fail.
  275. </p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help[=<em class="replaceable"><code>topic</code></em>]</code></span></dt><dd><p>
  276. Show help about <span class="application">psql</span> and exit. The optional
  277. <em class="replaceable"><code>topic</code></em> parameter (defaulting
  278. to <code class="literal">options</code>) selects which part of <span class="application">psql</span> is
  279. explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
  280. backslash commands; <code class="literal">options</code> describes the command-line
  281. options that can be passed to <span class="application">psql</span>;
  282. and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
  283. variables.
  284. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.18.7"><h2>Exit Status</h2><p>
  285. <span class="application">psql</span> returns 0 to the shell if it
  286. finished normally, 1 if a fatal error of its own occurs (e.g. out of memory,
  287. file not found), 2 if the connection to the server went bad
  288. and the session was not interactive, and 3 if an error occurred in a
  289. script and the variable <code class="varname">ON_ERROR_STOP</code> was set.
  290. </p></div><div class="refsect1" id="id-1.9.4.18.8"><h2>Usage</h2><div class="refsect2" id="R2-APP-PSQL-CONNECTING"><h3>Connecting to a Database</h3><p>
  291. <span class="application">psql</span> is a regular
  292. <span class="productname">PostgreSQL</span> client application. In order
  293. to connect to a database you need to know the name of your target
  294. database, the host name and port number of the server, and what user
  295. name you want to connect as. <span class="application">psql</span> can be
  296. told about those parameters via command line options, namely
  297. <code class="option">-d</code>, <code class="option">-h</code>, <code class="option">-p</code>, and
  298. <code class="option">-U</code> respectively. If an argument is found that does
  299. not belong to any option it will be interpreted as the database name
  300. (or the user name, if the database name is already given). Not all
  301. of these options are required; there are useful defaults. If you omit the host
  302. name, <span class="application">psql</span> will connect via a Unix-domain socket
  303. to a server on the local host, or via TCP/IP to <code class="literal">localhost</code> on
  304. machines that don't have Unix-domain sockets. The default port number is
  305. determined at compile time.
  306. Since the database server uses the same default, you will not have
  307. to specify the port in most cases. The default user name is your
  308. operating-system user name, as is the default database name.
  309. Note that you cannot
  310. just connect to any database under any user name. Your database
  311. administrator should have informed you about your access rights.
  312. </p><p>
  313. When the defaults aren't quite right, you can save yourself
  314. some typing by setting the environment variables
  315. <code class="envar">PGDATABASE</code>, <code class="envar">PGHOST</code>,
  316. <code class="envar">PGPORT</code> and/or <code class="envar">PGUSER</code> to appropriate
  317. values. (For additional environment variables, see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>.) It is also convenient to have a
  318. <code class="filename">~/.pgpass</code> file to avoid regularly having to type in
  319. passwords. See <a class="xref" href="libpq-pgpass.html" title="33.15. The Password File">Section 33.15</a> for more information.
  320. </p><p>
  321. An alternative way to specify connection parameters is in a
  322. <em class="parameter"><code>conninfo</code></em> string or
  323. a <acronym class="acronym">URI</acronym>, which is used instead of a database
  324. name. This mechanism give you very wide control over the
  325. connection. For example:
  326. </p><pre class="programlisting">
  327. $ <strong class="userinput"><code>psql "service=myservice sslmode=require"</code></strong>
  328. $ <strong class="userinput"><code>psql postgresql://dbmaster:5433/mydb?sslmode=require</code></strong>
  329. </pre><p>
  330. This way you can also use <acronym class="acronym">LDAP</acronym> for connection
  331. parameter lookup as described in <a class="xref" href="libpq-ldap.html" title="33.17. LDAP Lookup of Connection Parameters">Section 33.17</a>.
  332. See <a class="xref" href="libpq-connect.html#LIBPQ-PARAMKEYWORDS" title="33.1.2. Parameter Key Words">Section 33.1.2</a> for more information on all the
  333. available connection options.
  334. </p><p>
  335. If the connection could not be made for any reason (e.g., insufficient
  336. privileges, server is not running on the targeted host, etc.),
  337. <span class="application">psql</span> will return an error and terminate.
  338. </p><p>
  339. If both standard input and standard output are a
  340. terminal, then <span class="application">psql</span> sets the client
  341. encoding to <span class="quote">“<span class="quote">auto</span>”</span>, which will detect the
  342. appropriate client encoding from the locale settings
  343. (<code class="envar">LC_CTYPE</code> environment variable on Unix systems).
  344. If this doesn't work out as expected, the client encoding can be
  345. overridden using the environment
  346. variable <code class="envar">PGCLIENTENCODING</code>.
  347. </p></div><div class="refsect2" id="R2-APP-PSQL-4"><h3>Entering SQL Commands</h3><p>
  348. In normal operation, <span class="application">psql</span> provides a
  349. prompt with the name of the database to which
  350. <span class="application">psql</span> is currently connected, followed by
  351. the string <code class="literal">=&gt;</code>. For example:
  352. </p><pre class="programlisting">
  353. $ <strong class="userinput"><code>psql testdb</code></strong>
  354. psql (12.4)
  355. Type "help" for help.
  356. testdb=&gt;
  357. </pre><p>
  358. </p><p>
  359. At the prompt, the user can type in <acronym class="acronym">SQL</acronym> commands.
  360. Ordinarily, input lines are sent to the server when a
  361. command-terminating semicolon is reached. An end of line does not
  362. terminate a command. Thus commands can be spread over several lines for
  363. clarity. If the command was sent and executed without error, the results
  364. of the command are displayed on the screen.
  365. </p><p>
  366. If untrusted users have access to a database that has not adopted a
  367. <a class="link" href="ddl-schemas.html#DDL-SCHEMAS-PATTERNS" title="5.9.6. Usage Patterns">secure schema usage pattern</a>,
  368. begin your session by removing publicly-writable schemas
  369. from <code class="varname">search_path</code>. One can
  370. add <code class="literal">options=-csearch_path=</code> to the connection string or
  371. issue <code class="literal">SELECT pg_catalog.set_config('search_path', '',
  372. false)</code> before other SQL commands. This consideration is not
  373. specific to <span class="application">psql</span>; it applies to every interface
  374. for executing arbitrary SQL commands.
  375. </p><p>
  376. Whenever a command is executed, <span class="application">psql</span> also polls
  377. for asynchronous notification events generated by
  378. <a class="xref" href="sql-listen.html" title="LISTEN"><span class="refentrytitle">LISTEN</span></a> and
  379. <a class="xref" href="sql-notify.html" title="NOTIFY"><span class="refentrytitle">NOTIFY</span></a>.
  380. </p><p>
  381. While C-style block comments are passed to the server for
  382. processing and removal, SQL-standard comments are removed by
  383. <span class="application">psql</span>.
  384. </p></div><div class="refsect2" id="APP-PSQL-META-COMMANDS"><h3>Meta-Commands</h3><p>
  385. Anything you enter in <span class="application">psql</span> that begins
  386. with an unquoted backslash is a <span class="application">psql</span>
  387. meta-command that is processed by <span class="application">psql</span>
  388. itself. These commands make
  389. <span class="application">psql</span> more useful for administration or
  390. scripting. Meta-commands are often called slash or backslash commands.
  391. </p><p>
  392. The format of a <span class="application">psql</span> command is the backslash,
  393. followed immediately by a command verb, then any arguments. The arguments
  394. are separated from the command verb and each other by any number of
  395. whitespace characters.
  396. </p><p>
  397. To include whitespace in an argument you can quote it with
  398. single quotes. To include a single quote in an argument,
  399. write two single quotes within single-quoted text.
  400. Anything contained in single quotes is
  401. furthermore subject to C-like substitutions for
  402. <code class="literal">\n</code> (new line), <code class="literal">\t</code> (tab),
  403. <code class="literal">\b</code> (backspace), <code class="literal">\r</code> (carriage return),
  404. <code class="literal">\f</code> (form feed),
  405. <code class="literal">\</code><em class="replaceable"><code>digits</code></em> (octal), and
  406. <code class="literal">\x</code><em class="replaceable"><code>digits</code></em> (hexadecimal).
  407. A backslash preceding any other character within single-quoted text
  408. quotes that single character, whatever it is.
  409. </p><p>
  410. If an unquoted colon (<code class="literal">:</code>) followed by a
  411. <span class="application">psql</span> variable name appears within an argument, it is
  412. replaced by the variable's value, as described in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation"><acronym class="acronym">SQL</acronym> Interpolation</a>.
  413. The forms <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> and
  414. <code class="literal">:"<em class="replaceable"><code>variable_name</code></em>"</code> described there
  415. work as well.
  416. The <code class="literal">:{?<em class="replaceable"><code>variable_name</code></em>}</code> syntax allows
  417. testing whether a variable is defined. It is substituted by
  418. TRUE or FALSE.
  419. Escaping the colon with a backslash protects it from substitution.
  420. </p><p>
  421. Within an argument, text that is enclosed in backquotes
  422. (<code class="literal">`</code>) is taken as a command line that is passed to the
  423. shell. The output of the command (with any trailing newline removed)
  424. replaces the backquoted text. Within the text enclosed in backquotes,
  425. no special quoting or other processing occurs, except that appearances
  426. of <code class="literal">:<em class="replaceable"><code>variable_name</code></em></code> where
  427. <em class="replaceable"><code>variable_name</code></em> is a <span class="application">psql</span> variable name
  428. are replaced by the variable's value. Also, appearances of
  429. <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> are replaced by the
  430. variable's value suitably quoted to become a single shell command
  431. argument. (The latter form is almost always preferable, unless you are
  432. very sure of what is in the variable.) Because carriage return and line
  433. feed characters cannot be safely quoted on all platforms, the
  434. <code class="literal">:'<em class="replaceable"><code>variable_name</code></em>'</code> form prints an
  435. error message and does not substitute the variable value when such
  436. characters appear in the value.
  437. </p><p>
  438. Some commands take an <acronym class="acronym">SQL</acronym> identifier (such as a
  439. table name) as argument. These arguments follow the syntax rules
  440. of <acronym class="acronym">SQL</acronym>: Unquoted letters are forced to
  441. lowercase, while double quotes (<code class="literal">"</code>) protect letters
  442. from case conversion and allow incorporation of whitespace into
  443. the identifier. Within double quotes, paired double quotes reduce
  444. to a single double quote in the resulting name. For example,
  445. <code class="literal">FOO"BAR"BAZ</code> is interpreted as <code class="literal">fooBARbaz</code>,
  446. and <code class="literal">"A weird"" name"</code> becomes <code class="literal">A weird"
  447. name</code>.
  448. </p><p>
  449. Parsing for arguments stops at the end of the line, or when another
  450. unquoted backslash is found. An unquoted backslash
  451. is taken as the beginning of a new meta-command. The special
  452. sequence <code class="literal">\\</code> (two backslashes) marks the end of
  453. arguments and continues parsing <acronym class="acronym">SQL</acronym> commands, if
  454. any. That way <acronym class="acronym">SQL</acronym> and
  455. <span class="application">psql</span> commands can be freely mixed on a
  456. line. But in any case, the arguments of a meta-command cannot
  457. continue beyond the end of the line.
  458. </p><p>
  459. Many of the meta-commands act on the <em class="firstterm">current query buffer</em>.
  460. This is simply a buffer holding whatever SQL command text has been typed
  461. but not yet sent to the server for execution. This will include previous
  462. input lines as well as any text appearing before the meta-command on the
  463. same line.
  464. </p><p>
  465. The following meta-commands are defined:
  466. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">\a</code></span></dt><dd><p>
  467. If the current table output format is unaligned, it is switched to aligned.
  468. If it is not unaligned, it is set to unaligned. This command is
  469. kept for backwards compatibility. See <code class="command">\pset</code> for a
  470. more general solution.
  471. </p></dd><dt><span class="term"><code class="literal">\c</code> or <code class="literal">\connect [ -reuse-previous=<em class="replaceable"><code>on|off</code></em> ] [ <em class="replaceable"><code>dbname</code></em> [ <em class="replaceable"><code>username</code></em> ] [ <em class="replaceable"><code>host</code></em> ] [ <em class="replaceable"><code>port</code></em> ] | <em class="replaceable"><code>conninfo</code></em> ]</code></span></dt><dd><p>
  472. Establishes a new connection to a <span class="productname">PostgreSQL</span>
  473. server. The connection parameters to use can be specified either
  474. using a positional syntax, or using <em class="replaceable"><code>conninfo</code></em> connection
  475. strings as detailed in <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a>.
  476. </p><p>
  477. Where the command omits database name, user, host, or port, the new
  478. connection can reuse values from the previous connection. By default,
  479. values from the previous connection are reused except when processing
  480. a <em class="replaceable"><code>conninfo</code></em> string. Passing a first argument
  481. of <code class="literal">-reuse-previous=on</code>
  482. or <code class="literal">-reuse-previous=off</code> overrides that default.
  483. When the command neither specifies nor reuses a particular parameter,
  484. the <span class="application">libpq</span> default is used. Specifying any
  485. of <em class="replaceable"><code>dbname</code></em>,
  486. <em class="replaceable"><code>username</code></em>,
  487. <em class="replaceable"><code>host</code></em> or
  488. <em class="replaceable"><code>port</code></em>
  489. as <code class="literal">-</code> is equivalent to omitting that parameter.
  490. If <code class="literal">hostaddr</code> was specified in the original
  491. connection's <code class="structname">conninfo</code>, that address is reused
  492. for the new connection (disregarding any other host specification).
  493. </p><p>
  494. If the new connection is successfully made, the previous
  495. connection is closed.
  496. If the connection attempt failed (wrong user name, access
  497. denied, etc.), the previous connection will only be kept if
  498. <span class="application">psql</span> is in interactive mode. When
  499. executing a non-interactive script, processing will
  500. immediately stop with an error. This distinction was chosen as
  501. a user convenience against typos on the one hand, and a safety
  502. mechanism that scripts are not accidentally acting on the
  503. wrong database on the other hand.
  504. </p><p>
  505. Examples:
  506. </p><pre class="programlisting">
  507. =&gt; \c mydb myuser host.dom 6432
  508. =&gt; \c service=foo
  509. =&gt; \c "host=localhost port=5432 dbname=mydb connect_timeout=10 sslmode=disable"
  510. =&gt; \c postgresql://tom@localhost/mydb?application_name=myapp
  511. </pre></dd><dt><span class="term"><code class="literal">\C [ <em class="replaceable"><code>title</code></em> ]</code></span></dt><dd><p>
  512. Sets the title of any tables being printed as the result of a
  513. query or unset any such title. This command is equivalent to
  514. <code class="literal">\pset title <em class="replaceable"><code>title</code></em></code>. (The name of
  515. this command derives from <span class="quote">“<span class="quote">caption</span>”</span>, as it was
  516. previously only used to set the caption in an
  517. <acronym class="acronym">HTML</acronym> table.)
  518. </p></dd><dt><span class="term"><code class="literal">\cd [ <em class="replaceable"><code>directory</code></em> ]</code></span></dt><dd><p>
  519. Changes the current working directory to
  520. <em class="replaceable"><code>directory</code></em>. Without argument, changes
  521. to the current user's home directory.
  522. </p><div class="tip"><h3 class="title">Tip</h3><p>
  523. To print your current working directory, use <code class="literal">\! pwd</code>.
  524. </p></div></dd><dt><span class="term"><code class="literal">\conninfo</code></span></dt><dd><p>
  525. Outputs information about the current database connection.
  526. </p></dd><dt id="APP-PSQL-META-COMMANDS-COPY"><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] }
  527. <code class="literal">from</code>
  528. { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdin | pstdin }
  529. [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]
  530. [ where <em class="replaceable"><code>condition</code></em> ]</code><br /></span><span class="term"><code class="literal">\copy { <em class="replaceable"><code>table</code></em> [ ( <em class="replaceable"><code>column_list</code></em> ) ] | ( <em class="replaceable"><code>query</code></em> ) }
  531. <code class="literal">to</code>
  532. { <em class="replaceable"><code>'filename'</code></em> | program <em class="replaceable"><code>'command'</code></em> | stdout | pstdout }
  533. [ [ with ] ( <em class="replaceable"><code>option</code></em> [, ...] ) ]</code></span></dt><dd><p>
  534. Performs a frontend (client) copy. This is an operation that
  535. runs an <acronym class="acronym">SQL</acronym> <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>
  536. command, but instead of the server
  537. reading or writing the specified file,
  538. <span class="application">psql</span> reads or writes the file and
  539. routes the data between the server and the local file system.
  540. This means that file accessibility and privileges are those of
  541. the local user, not the server, and no SQL superuser
  542. privileges are required.
  543. </p><p>
  544. When <code class="literal">program</code> is specified,
  545. <em class="replaceable"><code>command</code></em> is
  546. executed by <span class="application">psql</span> and the data passed from
  547. or to <em class="replaceable"><code>command</code></em> is
  548. routed between the server and the client.
  549. Again, the execution privileges are those of
  550. the local user, not the server, and no SQL superuser
  551. privileges are required.
  552. </p><p>
  553. For <code class="literal">\copy ... from stdin</code>, data rows are read from the same
  554. source that issued the command, continuing until <code class="literal">\.</code>
  555. is read or the stream reaches <acronym class="acronym">EOF</acronym>. This option is useful
  556. for populating tables in-line within a SQL script file.
  557. For <code class="literal">\copy ... to stdout</code>, output is sent to the same place
  558. as <span class="application">psql</span> command output, and
  559. the <code class="literal">COPY <em class="replaceable"><code>count</code></em></code> command status is
  560. not printed (since it might be confused with a data row).
  561. To read/write <span class="application">psql</span>'s standard input or
  562. output regardless of the current command source or <code class="literal">\o</code>
  563. option, write <code class="literal">from pstdin</code> or <code class="literal">to pstdout</code>.
  564. </p><p>
  565. The syntax of this command is similar to that of the
  566. <acronym class="acronym">SQL</acronym> <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>
  567. command. All options other than the data source/destination are
  568. as specified for <a class="xref" href="sql-copy.html" title="COPY"><span class="refentrytitle">COPY</span></a>.
  569. Because of this, special parsing rules apply to the <code class="command">\copy</code>
  570. meta-command. Unlike most other meta-commands, the entire remainder
  571. of the line is always taken to be the arguments of <code class="command">\copy</code>,
  572. and neither variable interpolation nor backquote expansion are
  573. performed in the arguments.
  574. </p><div class="tip"><h3 class="title">Tip</h3><p>
  575. Another way to obtain the same result as <code class="literal">\copy
  576. ... to</code> is to use the <acronym class="acronym">SQL</acronym> <code class="literal">COPY
  577. ... TO STDOUT</code> command and terminate it
  578. with <code class="literal">\g <em class="replaceable"><code>filename</code></em></code>
  579. or <code class="literal">\g |<em class="replaceable"><code>program</code></em></code>.
  580. Unlike <code class="literal">\copy</code>, this method allows the command to
  581. span multiple lines; also, variable interpolation and backquote
  582. expansion can be used.
  583. </p></div><div class="tip"><h3 class="title">Tip</h3><p>
  584. These operations are not as efficient as the <acronym class="acronym">SQL</acronym>
  585. <code class="command">COPY</code> command with a file or program data source or
  586. destination, because all data must pass through the client/server
  587. connection. For large amounts of data the <acronym class="acronym">SQL</acronym>
  588. command might be preferable.
  589. </p></div></dd><dt><span class="term"><code class="literal">\copyright</code></span></dt><dd><p>
  590. Shows the copyright and distribution terms of
  591. <span class="productname">PostgreSQL</span>.
  592. </p></dd><dt id="APP-PSQL-META-COMMANDS-CROSSTABVIEW"><span class="term"><code class="literal">\crosstabview [
  593. <em class="replaceable"><code>colV</code></em>
  594. [ <em class="replaceable"><code>colH</code></em>
  595. [ <em class="replaceable"><code>colD</code></em>
  596. [ <em class="replaceable"><code>sortcolH</code></em>
  597. ] ] ] ] </code></span></dt><dd><p>
  598. Executes the current query buffer (like <code class="literal">\g</code>) and
  599. shows the results in a crosstab grid.
  600. The query must return at least three columns.
  601. The output column identified by <em class="replaceable"><code>colV</code></em>
  602. becomes a vertical header and the output column identified by
  603. <em class="replaceable"><code>colH</code></em>
  604. becomes a horizontal header.
  605. <em class="replaceable"><code>colD</code></em> identifies
  606. the output column to display within the grid.
  607. <em class="replaceable"><code>sortcolH</code></em> identifies
  608. an optional sort column for the horizontal header.
  609. </p><p>
  610. Each column specification can be a column number (starting at 1) or
  611. a column name. The usual SQL case folding and quoting rules apply to
  612. column names. If omitted,
  613. <em class="replaceable"><code>colV</code></em> is taken as column 1
  614. and <em class="replaceable"><code>colH</code></em> as column 2.
  615. <em class="replaceable"><code>colH</code></em> must differ from
  616. <em class="replaceable"><code>colV</code></em>.
  617. If <em class="replaceable"><code>colD</code></em> is not
  618. specified, then there must be exactly three columns in the query
  619. result, and the column that is neither
  620. <em class="replaceable"><code>colV</code></em> nor
  621. <em class="replaceable"><code>colH</code></em>
  622. is taken to be <em class="replaceable"><code>colD</code></em>.
  623. </p><p>
  624. The vertical header, displayed as the leftmost column, contains the
  625. values found in column <em class="replaceable"><code>colV</code></em>, in the
  626. same order as in the query results, but with duplicates removed.
  627. </p><p>
  628. The horizontal header, displayed as the first row, contains the values
  629. found in column <em class="replaceable"><code>colH</code></em>,
  630. with duplicates removed. By default, these appear in the same order
  631. as in the query results. But if the
  632. optional <em class="replaceable"><code>sortcolH</code></em> argument is given,
  633. it identifies a column whose values must be integer numbers, and the
  634. values from <em class="replaceable"><code>colH</code></em> will
  635. appear in the horizontal header sorted according to the
  636. corresponding <em class="replaceable"><code>sortcolH</code></em> values.
  637. </p><p>
  638. Inside the crosstab grid, for each distinct value <code class="literal">x</code>
  639. of <em class="replaceable"><code>colH</code></em> and each distinct
  640. value <code class="literal">y</code>
  641. of <em class="replaceable"><code>colV</code></em>, the cell located
  642. at the intersection <code class="literal">(x,y)</code> contains the value of
  643. the <code class="literal">colD</code> column in the query result row for which
  644. the value of <em class="replaceable"><code>colH</code></em>
  645. is <code class="literal">x</code> and the value
  646. of <em class="replaceable"><code>colV</code></em>
  647. is <code class="literal">y</code>. If there is no such row, the cell is empty. If
  648. there are multiple such rows, an error is reported.
  649. </p></dd><dt><span class="term"><code class="literal">\d[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  650. For each relation (table, view, materialized view, index, sequence,
  651. or foreign table)
  652. or composite type matching the
  653. <em class="replaceable"><code>pattern</code></em>, show all
  654. columns, their types, the tablespace (if not the default) and any
  655. special attributes such as <code class="literal">NOT NULL</code> or defaults.
  656. Associated indexes, constraints, rules, and triggers are
  657. also shown. For foreign tables, the associated foreign
  658. server is shown as well.
  659. (<span class="quote">“<span class="quote">Matching the pattern</span>”</span> is defined in
  660. <a class="xref" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns">Patterns</a>
  661. below.)
  662. </p><p>
  663. For some types of relation, <code class="literal">\d</code> shows additional information
  664. for each column: column values for sequences, indexed expressions for
  665. indexes, and foreign data wrapper options for foreign tables.
  666. </p><p>
  667. The command form <code class="literal">\d+</code> is identical, except that
  668. more information is displayed: any comments associated with the
  669. columns of the table are shown, as is the presence of OIDs in the
  670. table, the view definition if the relation is a view, a non-default
  671. <a class="link" href="sql-altertable.html#SQL-CREATETABLE-REPLICA-IDENTITY">replica
  672. identity</a> setting.
  673. </p><p>
  674. By default, only user-created objects are shown; supply a
  675. pattern or the <code class="literal">S</code> modifier to include system
  676. objects.
  677. </p><div class="note"><h3 class="title">Note</h3><p>
  678. If <code class="command">\d</code> is used without a
  679. <em class="replaceable"><code>pattern</code></em> argument, it is
  680. equivalent to <code class="command">\dtvmsE</code> which will show a list of
  681. all visible tables, views, materialized views, sequences and
  682. foreign tables.
  683. This is purely a convenience measure.
  684. </p></div></dd><dt><span class="term"><code class="literal">\da[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  685. Lists aggregate functions, together with their
  686. return type and the data types they operate on. If <em class="replaceable"><code>pattern</code></em>
  687. is specified, only aggregates whose names match the pattern are shown.
  688. By default, only user-created objects are shown; supply a
  689. pattern or the <code class="literal">S</code> modifier to include system
  690. objects.
  691. </p></dd><dt><span class="term"><code class="literal">\dA[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  692. Lists access methods. If <em class="replaceable"><code>pattern</code></em> is specified, only access
  693. methods whose names match the pattern are shown. If
  694. <code class="literal">+</code> is appended to the command name, each access
  695. method is listed with its associated handler function and description.
  696. </p></dd><dt><span class="term"><code class="literal">\db[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  697. Lists tablespaces. If <em class="replaceable"><code>pattern</code></em>
  698. is specified, only tablespaces whose names match the pattern are shown.
  699. If <code class="literal">+</code> is appended to the command name, each tablespace
  700. is listed with its associated options, on-disk size, permissions and
  701. description.
  702. </p></dd><dt><span class="term"><code class="literal">\dc[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  703. Lists conversions between character-set encodings.
  704. If <em class="replaceable"><code>pattern</code></em>
  705. is specified, only conversions whose names match the pattern are
  706. listed.
  707. By default, only user-created objects are shown; supply a
  708. pattern or the <code class="literal">S</code> modifier to include system
  709. objects.
  710. If <code class="literal">+</code> is appended to the command name, each object
  711. is listed with its associated description.
  712. </p></dd><dt><span class="term"><code class="literal">\dC[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  713. Lists type casts.
  714. If <em class="replaceable"><code>pattern</code></em>
  715. is specified, only casts whose source or target types match the
  716. pattern are listed.
  717. If <code class="literal">+</code> is appended to the command name, each object
  718. is listed with its associated description.
  719. </p></dd><dt><span class="term"><code class="literal">\dd[S] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  720. Shows the descriptions of objects of type <code class="literal">constraint</code>,
  721. <code class="literal">operator class</code>, <code class="literal">operator family</code>,
  722. <code class="literal">rule</code>, and <code class="literal">trigger</code>. All
  723. other comments may be viewed by the respective backslash commands for
  724. those object types.
  725. </p><p><code class="literal">\dd</code> displays descriptions for objects matching the
  726. <em class="replaceable"><code>pattern</code></em>, or of visible
  727. objects of the appropriate type if no argument is given. But in either
  728. case, only objects that have a description are listed.
  729. By default, only user-created objects are shown; supply a
  730. pattern or the <code class="literal">S</code> modifier to include system
  731. objects.
  732. </p><p>
  733. Descriptions for objects can be created with the <a class="xref" href="sql-comment.html" title="COMMENT"><span class="refentrytitle">COMMENT</span></a>
  734. <acronym class="acronym">SQL</acronym> command.
  735. </p></dd><dt><span class="term"><code class="literal">\dD[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  736. Lists domains. If <em class="replaceable"><code>pattern</code></em>
  737. is specified, only domains whose names match the pattern are shown.
  738. By default, only user-created objects are shown; supply a
  739. pattern or the <code class="literal">S</code> modifier to include system
  740. objects.
  741. If <code class="literal">+</code> is appended to the command name, each object
  742. is listed with its associated permissions and description.
  743. </p></dd><dt><span class="term"><code class="literal">\ddp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  744. Lists default access privilege settings. An entry is shown for
  745. each role (and schema, if applicable) for which the default
  746. privilege settings have been changed from the built-in defaults.
  747. If <em class="replaceable"><code>pattern</code></em> is
  748. specified, only entries whose role name or schema name matches
  749. the pattern are listed.
  750. </p><p>
  751. The <a class="xref" href="sql-alterdefaultprivileges.html" title="ALTER DEFAULT PRIVILEGES"><span class="refentrytitle">ALTER DEFAULT PRIVILEGES</span></a> command is used to set
  752. default access privileges. The meaning of the
  753. privilege display is explained in
  754. <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
  755. </p></dd><dt><span class="term"><code class="literal">\dE[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\di[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dm[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\ds[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dt[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code><br /></span><span class="term"><code class="literal">\dv[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  756. In this group of commands, the letters <code class="literal">E</code>,
  757. <code class="literal">i</code>, <code class="literal">m</code>, <code class="literal">s</code>,
  758. <code class="literal">t</code>, and <code class="literal">v</code>
  759. stand for foreign table, index, materialized view, sequence, table, and view,
  760. respectively.
  761. You can specify any or all of
  762. these letters, in any order, to obtain a listing of objects
  763. of these types. For example, <code class="literal">\dit</code> lists indexes
  764. and tables. If <code class="literal">+</code> is
  765. appended to the command name, each object is listed with its
  766. physical size on disk and its associated description, if any.
  767. If <em class="replaceable"><code>pattern</code></em> is
  768. specified, only objects whose names match the pattern are listed.
  769. By default, only user-created objects are shown; supply a
  770. pattern or the <code class="literal">S</code> modifier to include system
  771. objects.
  772. </p></dd><dt><span class="term"><code class="literal">\des[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  773. Lists foreign servers (mnemonic: <span class="quote">“<span class="quote">external
  774. servers</span>”</span>).
  775. If <em class="replaceable"><code>pattern</code></em> is
  776. specified, only those servers whose name matches the pattern
  777. are listed. If the form <code class="literal">\des+</code> is used, a
  778. full description of each server is shown, including the
  779. server's access privileges, type, version, options, and description.
  780. </p></dd><dt><span class="term"><code class="literal">\det[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  781. Lists foreign tables (mnemonic: <span class="quote">“<span class="quote">external tables</span>”</span>).
  782. If <em class="replaceable"><code>pattern</code></em> is
  783. specified, only entries whose table name or schema name matches
  784. the pattern are listed. If the form <code class="literal">\det+</code>
  785. is used, generic options and the foreign table description
  786. are also displayed.
  787. </p></dd><dt><span class="term"><code class="literal">\deu[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  788. Lists user mappings (mnemonic: <span class="quote">“<span class="quote">external
  789. users</span>”</span>).
  790. If <em class="replaceable"><code>pattern</code></em> is
  791. specified, only those mappings whose user names match the
  792. pattern are listed. If the form <code class="literal">\deu+</code> is
  793. used, additional information about each mapping is shown.
  794. </p><div class="caution"><h3 class="title">Caution</h3><p>
  795. <code class="literal">\deu+</code> might also display the user name and
  796. password of the remote user, so care should be taken not to
  797. disclose them.
  798. </p></div></dd><dt><span class="term"><code class="literal">\dew[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  799. Lists foreign-data wrappers (mnemonic: <span class="quote">“<span class="quote">external
  800. wrappers</span>”</span>).
  801. If <em class="replaceable"><code>pattern</code></em> is
  802. specified, only those foreign-data wrappers whose name matches
  803. the pattern are listed. If the form <code class="literal">\dew+</code>
  804. is used, the access privileges, options, and description of the
  805. foreign-data wrapper are also shown.
  806. </p></dd><dt><span class="term"><code class="literal">\df[anptwS+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  807. Lists functions, together with their result data types, argument data
  808. types, and function types, which are classified as <span class="quote">“<span class="quote">agg</span>”</span>
  809. (aggregate), <span class="quote">“<span class="quote">normal</span>”</span>, <span class="quote">“<span class="quote">procedure</span>”</span>, <span class="quote">“<span class="quote">trigger</span>”</span>, or <span class="quote">“<span class="quote">window</span>”</span>.
  810. To display only functions
  811. of specific type(s), add the corresponding letters <code class="literal">a</code>,
  812. <code class="literal">n</code>, <code class="literal">p</code>, <code class="literal">t</code>, or <code class="literal">w</code> to the command.
  813. If <em class="replaceable"><code>pattern</code></em> is specified, only
  814. functions whose names match the pattern are shown.
  815. By default, only user-created
  816. objects are shown; supply a pattern or the <code class="literal">S</code>
  817. modifier to include system objects.
  818. If the form <code class="literal">\df+</code> is used, additional information
  819. about each function is shown, including volatility,
  820. parallel safety, owner, security classification, access privileges,
  821. language, source code and description.
  822. </p><div class="tip"><h3 class="title">Tip</h3><p>
  823. To look up functions taking arguments or returning values of a specific
  824. data type, use your pager's search capability to scroll through the
  825. <code class="literal">\df</code> output.
  826. </p></div></dd><dt><span class="term"><code class="literal">\dF[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  827. Lists text search configurations.
  828. If <em class="replaceable"><code>pattern</code></em> is specified,
  829. only configurations whose names match the pattern are shown.
  830. If the form <code class="literal">\dF+</code> is used, a full description of
  831. each configuration is shown, including the underlying text search
  832. parser and the dictionary list for each parser token type.
  833. </p></dd><dt><span class="term"><code class="literal">\dFd[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  834. Lists text search dictionaries.
  835. If <em class="replaceable"><code>pattern</code></em> is specified,
  836. only dictionaries whose names match the pattern are shown.
  837. If the form <code class="literal">\dFd+</code> is used, additional information
  838. is shown about each selected dictionary, including the underlying
  839. text search template and the option values.
  840. </p></dd><dt><span class="term"><code class="literal">\dFp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  841. Lists text search parsers.
  842. If <em class="replaceable"><code>pattern</code></em> is specified,
  843. only parsers whose names match the pattern are shown.
  844. If the form <code class="literal">\dFp+</code> is used, a full description of
  845. each parser is shown, including the underlying functions and the
  846. list of recognized token types.
  847. </p></dd><dt><span class="term"><code class="literal">\dFt[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  848. Lists text search templates.
  849. If <em class="replaceable"><code>pattern</code></em> is specified,
  850. only templates whose names match the pattern are shown.
  851. If the form <code class="literal">\dFt+</code> is used, additional information
  852. is shown about each template, including the underlying function names.
  853. </p></dd><dt><span class="term"><code class="literal">\dg[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  854. Lists database roles.
  855. (Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
  856. unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
  857. <code class="literal">\du</code>.)
  858. By default, only user-created roles are shown; supply the
  859. <code class="literal">S</code> modifier to include system roles.
  860. If <em class="replaceable"><code>pattern</code></em> is specified,
  861. only those roles whose names match the pattern are listed.
  862. If the form <code class="literal">\dg+</code> is used, additional information
  863. is shown about each role; currently this adds the comment for each
  864. role.
  865. </p></dd><dt><span class="term"><code class="literal">\dl</code></span></dt><dd><p>
  866. This is an alias for <code class="command">\lo_list</code>, which shows a
  867. list of large objects.
  868. </p></dd><dt><span class="term"><code class="literal">\dL[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  869. Lists procedural languages. If <em class="replaceable"><code>pattern</code></em>
  870. is specified, only languages whose names match the pattern are listed.
  871. By default, only user-created languages
  872. are shown; supply the <code class="literal">S</code> modifier to include system
  873. objects. If <code class="literal">+</code> is appended to the command name, each
  874. language is listed with its call handler, validator, access privileges,
  875. and whether it is a system object.
  876. </p></dd><dt><span class="term"><code class="literal">\dn[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  877. Lists schemas (namespaces). If <em class="replaceable"><code>pattern</code></em>
  878. is specified, only schemas whose names match the pattern are listed.
  879. By default, only user-created objects are shown; supply a
  880. pattern or the <code class="literal">S</code> modifier to include system objects.
  881. If <code class="literal">+</code> is appended to the command name, each object
  882. is listed with its associated permissions and description, if any.
  883. </p></dd><dt><span class="term"><code class="literal">\do[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  884. Lists operators with their operand and result types.
  885. If <em class="replaceable"><code>pattern</code></em> is
  886. specified, only operators whose names match the pattern are listed.
  887. By default, only user-created objects are shown; supply a
  888. pattern or the <code class="literal">S</code> modifier to include system
  889. objects.
  890. If <code class="literal">+</code> is appended to the command name,
  891. additional information about each operator is shown, currently just
  892. the name of the underlying function.
  893. </p></dd><dt><span class="term"><code class="literal">\dO[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  894. Lists collations.
  895. If <em class="replaceable"><code>pattern</code></em> is
  896. specified, only collations whose names match the pattern are
  897. listed. By default, only user-created objects are shown;
  898. supply a pattern or the <code class="literal">S</code> modifier to
  899. include system objects. If <code class="literal">+</code> is appended
  900. to the command name, each collation is listed with its associated
  901. description, if any.
  902. Note that only collations usable with the current database's encoding
  903. are shown, so the results may vary in different databases of the
  904. same installation.
  905. </p></dd><dt><span class="term"><code class="literal">\dp [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  906. Lists tables, views and sequences with their
  907. associated access privileges.
  908. If <em class="replaceable"><code>pattern</code></em> is
  909. specified, only tables, views and sequences whose names match the
  910. pattern are listed.
  911. </p><p>
  912. The <a class="xref" href="sql-grant.html" title="GRANT"><span class="refentrytitle">GRANT</span></a> and
  913. <a class="xref" href="sql-revoke.html" title="REVOKE"><span class="refentrytitle">REVOKE</span></a>
  914. commands are used to set access privileges. The meaning of the
  915. privilege display is explained in
  916. <a class="xref" href="ddl-priv.html" title="5.7. Privileges">Section 5.7</a>.
  917. </p></dd><dt><span class="term"><code class="literal">\dP[itn+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  918. Lists partitioned relations.
  919. If <em class="replaceable"><code>pattern</code></em>
  920. is specified, only entries whose name matches the pattern are listed.
  921. The modifiers <code class="literal">t</code> (tables) and <code class="literal">i</code>
  922. (indexes) can be appended to the command, filtering the kind of
  923. relations to list. By default, partitioned tables and indexes are
  924. listed.
  925. </p><p>
  926. If the modifier <code class="literal">n</code> (<span class="quote">“<span class="quote">nested</span>”</span>) is used,
  927. or a pattern is specified, then non-root partitioned relations are
  928. included, and a column is shown displaying the parent of each
  929. partitioned relation.
  930. </p><p>
  931. If <code class="literal">+</code> is appended to the command name, the sum of the
  932. sizes of each relation's partitions is also displayed, along with the
  933. relation's description.
  934. If <code class="literal">n</code> is combined with <code class="literal">+</code>, two
  935. sizes are shown: one including the total size of directly-attached
  936. leaf partitions, and another showing the total size of all partitions,
  937. including indirectly attached sub-partitions.
  938. </p></dd><dt><span class="term"><code class="literal">\drds [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>role-pattern</code></em></a> [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>database-pattern</code></em></a> ] ]</code></span></dt><dd><p>
  939. Lists defined configuration settings. These settings can be
  940. role-specific, database-specific, or both.
  941. <em class="replaceable"><code>role-pattern</code></em> and
  942. <em class="replaceable"><code>database-pattern</code></em> are used to select
  943. specific roles and databases to list, respectively. If omitted, or if
  944. <code class="literal">*</code> is specified, all settings are listed, including those
  945. not role-specific or database-specific, respectively.
  946. </p><p>
  947. The <a class="xref" href="sql-alterrole.html" title="ALTER ROLE"><span class="refentrytitle">ALTER ROLE</span></a> and
  948. <a class="xref" href="sql-alterdatabase.html" title="ALTER DATABASE"><span class="refentrytitle">ALTER DATABASE</span></a>
  949. commands are used to define per-role and per-database configuration
  950. settings.
  951. </p></dd><dt><span class="term"><code class="literal">\dRp[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  952. Lists replication publications.
  953. If <em class="replaceable"><code>pattern</code></em> is
  954. specified, only those publications whose names match the pattern are
  955. listed.
  956. If <code class="literal">+</code> is appended to the command name, the tables
  957. associated with each publication are shown as well.
  958. </p></dd><dt><span class="term"><code class="literal">\dRs[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  959. Lists replication subscriptions.
  960. If <em class="replaceable"><code>pattern</code></em> is
  961. specified, only those subscriptions whose names match the pattern are
  962. listed.
  963. If <code class="literal">+</code> is appended to the command name, additional
  964. properties of the subscriptions are shown.
  965. </p></dd><dt><span class="term"><code class="literal">\dT[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  966. Lists data types.
  967. If <em class="replaceable"><code>pattern</code></em> is
  968. specified, only types whose names match the pattern are listed.
  969. If <code class="literal">+</code> is appended to the command name, each type is
  970. listed with its internal name and size, its allowed values
  971. if it is an <code class="type">enum</code> type, and its associated permissions.
  972. By default, only user-created objects are shown; supply a
  973. pattern or the <code class="literal">S</code> modifier to include system
  974. objects.
  975. </p></dd><dt><span class="term"><code class="literal">\du[S+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  976. Lists database roles.
  977. (Since the concepts of <span class="quote">“<span class="quote">users</span>”</span> and <span class="quote">“<span class="quote">groups</span>”</span> have been
  978. unified into <span class="quote">“<span class="quote">roles</span>”</span>, this command is now equivalent to
  979. <code class="literal">\dg</code>.)
  980. By default, only user-created roles are shown; supply the
  981. <code class="literal">S</code> modifier to include system roles.
  982. If <em class="replaceable"><code>pattern</code></em> is specified,
  983. only those roles whose names match the pattern are listed.
  984. If the form <code class="literal">\du+</code> is used, additional information
  985. is shown about each role; currently this adds the comment for each
  986. role.
  987. </p></dd><dt><span class="term"><code class="literal">\dx[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  988. Lists installed extensions.
  989. If <em class="replaceable"><code>pattern</code></em>
  990. is specified, only those extensions whose names match the pattern
  991. are listed.
  992. If the form <code class="literal">\dx+</code> is used, all the objects belonging
  993. to each matching extension are listed.
  994. </p></dd><dt><span class="term"><code class="literal">\dy[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  995. Lists event triggers.
  996. If <em class="replaceable"><code>pattern</code></em>
  997. is specified, only those event triggers whose names match the pattern
  998. are listed.
  999. If <code class="literal">+</code> is appended to the command name, each object
  1000. is listed with its associated description.
  1001. </p></dd><dt><span class="term"><code class="literal">\e</code> or <code class="literal">\edit</code> <code class="literal"> [<span class="optional"> <em class="replaceable"><code>filename</code></em> </span>] [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </code></span></dt><dd><p>
  1002. If <em class="replaceable"><code>filename</code></em> is
  1003. specified, the file is edited; after the editor exits, the file's
  1004. content is copied into the current query buffer. If no <em class="replaceable"><code>filename</code></em> is given, the current query
  1005. buffer is copied to a temporary file which is then edited in the same
  1006. fashion. Or, if the current query buffer is empty, the most recently
  1007. executed query is copied to a temporary file and edited in the same
  1008. fashion.
  1009. </p><p>
  1010. The new contents of the query buffer are then re-parsed according to
  1011. the normal rules of <span class="application">psql</span>, treating the
  1012. whole buffer as a single line. Any complete queries are immediately
  1013. executed; that is, if the query buffer contains or ends with a
  1014. semicolon, everything up to that point is executed. Whatever remains
  1015. will wait in the query buffer; type semicolon or <code class="literal">\g</code> to
  1016. send it, or <code class="literal">\r</code> to cancel it by clearing the query buffer.
  1017. Treating the buffer as a single line primarily affects meta-commands:
  1018. whatever is in the buffer after a meta-command will be taken as
  1019. argument(s) to the meta-command, even if it spans multiple lines.
  1020. (Thus you cannot make meta-command-using scripts this way.
  1021. Use <code class="command">\i</code> for that.)
  1022. </p><p>
  1023. If a line number is specified, <span class="application">psql</span> will
  1024. position the cursor on the specified line of the file or query buffer.
  1025. Note that if a single all-digits argument is given,
  1026. <span class="application">psql</span> assumes it is a line number,
  1027. not a file name.
  1028. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1029. See under <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a> for how to configure and
  1030. customize your editor.
  1031. </p></div></dd><dt><span class="term"><code class="literal">\echo <em class="replaceable"><code>text</code></em> [ ... ]</code></span></dt><dd><p>
  1032. Prints the arguments to the standard output, separated by one
  1033. space and followed by a newline. This can be useful to
  1034. intersperse information in the output of scripts. For example:
  1035. </p><pre class="programlisting">
  1036. =&gt; <strong class="userinput"><code>\echo `date`</code></strong>
  1037. Tue Oct 26 21:40:57 CEST 1999
  1038. </pre><p>
  1039. If the first argument is an unquoted <code class="literal">-n</code> the trailing
  1040. newline is not written.
  1041. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1042. If you use the <code class="command">\o</code> command to redirect your
  1043. query output you might wish to use <code class="command">\qecho</code>
  1044. instead of this command.
  1045. </p></div></dd><dt><span class="term"><code class="literal">\ef [<span class="optional"> <em class="replaceable"><code>function_description</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span></dt><dd><p>
  1046. This command fetches and edits the definition of the named function or procedure,
  1047. in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
  1048. <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
  1049. Editing is done in the same way as for <code class="literal">\edit</code>.
  1050. After the editor exits, the updated command waits in the query buffer;
  1051. type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
  1052. to cancel.
  1053. </p><p>
  1054. The target function can be specified by name alone, or by name
  1055. and arguments, for example <code class="literal">foo(integer, text)</code>.
  1056. The argument types must be given if there is more
  1057. than one function of the same name.
  1058. </p><p>
  1059. If no function is specified, a blank <code class="command">CREATE FUNCTION</code>
  1060. template is presented for editing.
  1061. </p><p>
  1062. If a line number is specified, <span class="application">psql</span> will
  1063. position the cursor on the specified line of the function body.
  1064. (Note that the function body typically does not begin on the first
  1065. line of the file.)
  1066. </p><p>
  1067. Unlike most other meta-commands, the entire remainder of the line is
  1068. always taken to be the argument(s) of <code class="command">\ef</code>, and neither
  1069. variable interpolation nor backquote expansion are performed in the
  1070. arguments.
  1071. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1072. See under <a class="xref" href="app-psql.html#APP-PSQL-ENVIRONMENT" title="Environment">Environment</a> for how to configure and
  1073. customize your editor.
  1074. </p></div></dd><dt><span class="term"><code class="literal">\encoding [ <em class="replaceable"><code>encoding</code></em> ]</code></span></dt><dd><p>
  1075. Sets the client character set encoding. Without an argument, this command
  1076. shows the current encoding.
  1077. </p></dd><dt><span class="term"><code class="literal">\errverbose</code></span></dt><dd><p>
  1078. Repeats the most recent server error message at maximum
  1079. verbosity, as though <code class="varname">VERBOSITY</code> were set
  1080. to <code class="literal">verbose</code> and <code class="varname">SHOW_CONTEXT</code> were
  1081. set to <code class="literal">always</code>.
  1082. </p></dd><dt><span class="term"><code class="literal">\ev [<span class="optional"> <em class="replaceable"><code>view_name</code></em> [<span class="optional"> <em class="replaceable"><code>line_number</code></em> </span>] </span>] </code></span></dt><dd><p>
  1083. This command fetches and edits the definition of the named view,
  1084. in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
  1085. Editing is done in the same way as for <code class="literal">\edit</code>.
  1086. After the editor exits, the updated command waits in the query buffer;
  1087. type semicolon or <code class="literal">\g</code> to send it, or <code class="literal">\r</code>
  1088. to cancel.
  1089. </p><p>
  1090. If no view is specified, a blank <code class="command">CREATE VIEW</code>
  1091. template is presented for editing.
  1092. </p><p>
  1093. If a line number is specified, <span class="application">psql</span> will
  1094. position the cursor on the specified line of the view definition.
  1095. </p><p>
  1096. Unlike most other meta-commands, the entire remainder of the line is
  1097. always taken to be the argument(s) of <code class="command">\ev</code>, and neither
  1098. variable interpolation nor backquote expansion are performed in the
  1099. arguments.
  1100. </p></dd><dt><span class="term"><code class="literal">\f [ <em class="replaceable"><code>string</code></em> ]</code></span></dt><dd><p>
  1101. Sets the field separator for unaligned query output. The default
  1102. is the vertical bar (<code class="literal">|</code>). It is equivalent to
  1103. <code class="command">\pset fieldsep</code>.
  1104. </p></dd><dt><span class="term"><code class="literal">\g [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\g [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
  1105. Sends the current query buffer to the server for execution.
  1106. If an argument is given, the query's output is written to the named
  1107. file or piped to the given shell command, instead of displaying it as
  1108. usual. The file or command is written to only if the query
  1109. successfully returns zero or more tuples, not if the query fails or
  1110. is a non-data-returning SQL command.
  1111. </p><p>
  1112. If the current query buffer is empty, the most recently sent query is
  1113. re-executed instead. Except for that behavior, <code class="literal">\g</code>
  1114. without an argument is essentially equivalent to a semicolon.
  1115. A <code class="literal">\g</code> with argument is a <span class="quote">“<span class="quote">one-shot</span>”</span>
  1116. alternative to the <code class="command">\o</code> command.
  1117. </p><p>
  1118. If the argument begins with <code class="literal">|</code>, then the entire remainder
  1119. of the line is taken to be
  1120. the <em class="replaceable"><code>command</code></em> to execute,
  1121. and neither variable interpolation nor backquote expansion are
  1122. performed in it. The rest of the line is simply passed literally to
  1123. the shell.
  1124. </p></dd><dt><span class="term"><code class="literal">\gdesc</code></span></dt><dd><p>
  1125. Shows the description (that is, the column names and data types)
  1126. of the result of the current query buffer. The query is not
  1127. actually executed; however, if it contains some type of syntax
  1128. error, that error will be reported in the normal way.
  1129. </p><p>
  1130. If the current query buffer is empty, the most recently sent query
  1131. is described instead.
  1132. </p></dd><dt><span class="term"><code class="literal">\gexec</code></span></dt><dd><p>
  1133. Sends the current query buffer to the server, then treats
  1134. each column of each row of the query's output (if any) as a SQL
  1135. statement to be executed. For example, to create an index on each
  1136. column of <code class="structname">my_table</code>:
  1137. </p><pre class="programlisting">
  1138. =&gt; <strong class="userinput"><code>SELECT format('create index on my_table(%I)', attname)</code></strong>
  1139. -&gt; <strong class="userinput"><code>FROM pg_attribute</code></strong>
  1140. -&gt; <strong class="userinput"><code>WHERE attrelid = 'my_table'::regclass AND attnum &gt; 0</code></strong>
  1141. -&gt; <strong class="userinput"><code>ORDER BY attnum</code></strong>
  1142. -&gt; <strong class="userinput"><code>\gexec</code></strong>
  1143. CREATE INDEX
  1144. CREATE INDEX
  1145. CREATE INDEX
  1146. CREATE INDEX
  1147. </pre><p>
  1148. </p><p>
  1149. The generated queries are executed in the order in which the rows
  1150. are returned, and left-to-right within each row if there is more
  1151. than one column. NULL fields are ignored. The generated queries
  1152. are sent literally to the server for processing, so they cannot be
  1153. <span class="application">psql</span> meta-commands nor contain <span class="application">psql</span>
  1154. variable references. If any individual query fails, execution of
  1155. the remaining queries continues
  1156. unless <code class="varname">ON_ERROR_STOP</code> is set. Execution of each
  1157. query is subject to <code class="varname">ECHO</code> processing.
  1158. (Setting <code class="varname">ECHO</code> to <code class="literal">all</code>
  1159. or <code class="literal">queries</code> is often advisable when
  1160. using <code class="command">\gexec</code>.) Query logging, single-step mode,
  1161. timing, and other query execution features apply to each generated
  1162. query as well.
  1163. </p><p>
  1164. If the current query buffer is empty, the most recently sent query
  1165. is re-executed instead.
  1166. </p></dd><dt><span class="term"><code class="literal">\gset [ <em class="replaceable"><code>prefix</code></em> ]</code></span></dt><dd><p>
  1167. Sends the current query buffer to the server and stores the
  1168. query's output into <span class="application">psql</span> variables (see <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>).
  1169. The query to be executed must return exactly one row. Each column of
  1170. the row is stored into a separate variable, named the same as the
  1171. column. For example:
  1172. </p><pre class="programlisting">
  1173. =&gt; <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
  1174. -&gt; <strong class="userinput"><code>\gset</code></strong>
  1175. =&gt; <strong class="userinput"><code>\echo :var1 :var2</code></strong>
  1176. hello 10
  1177. </pre><p>
  1178. </p><p>
  1179. If you specify a <em class="replaceable"><code>prefix</code></em>,
  1180. that string is prepended to the query's column names to create the
  1181. variable names to use:
  1182. </p><pre class="programlisting">
  1183. =&gt; <strong class="userinput"><code>SELECT 'hello' AS var1, 10 AS var2</code></strong>
  1184. -&gt; <strong class="userinput"><code>\gset result_</code></strong>
  1185. =&gt; <strong class="userinput"><code>\echo :result_var1 :result_var2</code></strong>
  1186. hello 10
  1187. </pre><p>
  1188. </p><p>
  1189. If a column result is NULL, the corresponding variable is unset
  1190. rather than being set.
  1191. </p><p>
  1192. If the query fails or does not return one row,
  1193. no variables are changed.
  1194. </p><p>
  1195. If the current query buffer is empty, the most recently sent query
  1196. is re-executed instead.
  1197. </p></dd><dt><span class="term"><code class="literal">\gx [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\gx [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
  1198. <code class="literal">\gx</code> is equivalent to <code class="literal">\g</code>, but
  1199. forces expanded output mode for this query. See <code class="literal">\x</code>.
  1200. </p></dd><dt><span class="term"><code class="literal">\h</code> or <code class="literal">\help</code> <code class="literal">[ <em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
  1201. Gives syntax help on the specified <acronym class="acronym">SQL</acronym>
  1202. command. If <em class="replaceable"><code>command</code></em>
  1203. is not specified, then <span class="application">psql</span> will list
  1204. all the commands for which syntax help is available. If
  1205. <em class="replaceable"><code>command</code></em> is an
  1206. asterisk (<code class="literal">*</code>), then syntax help on all
  1207. <acronym class="acronym">SQL</acronym> commands is shown.
  1208. </p><p>
  1209. Unlike most other meta-commands, the entire remainder of the line is
  1210. always taken to be the argument(s) of <code class="command">\help</code>, and neither
  1211. variable interpolation nor backquote expansion are performed in the
  1212. arguments.
  1213. </p><div class="note"><h3 class="title">Note</h3><p>
  1214. To simplify typing, commands that consists of several words do
  1215. not have to be quoted. Thus it is fine to type <strong class="userinput"><code>\help
  1216. alter table</code></strong>.
  1217. </p></div></dd><dt><span class="term"><code class="literal">\H</code> or <code class="literal">\html</code></span></dt><dd><p>
  1218. Turns on <acronym class="acronym">HTML</acronym> query output format. If the
  1219. <acronym class="acronym">HTML</acronym> format is already on, it is switched
  1220. back to the default aligned text format. This command is for
  1221. compatibility and convenience, but see <code class="command">\pset</code>
  1222. about setting other output options.
  1223. </p></dd><dt><span class="term"><code class="literal">\i</code> or <code class="literal">\include</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
  1224. Reads input from the file <em class="replaceable"><code>filename</code></em> and executes it as
  1225. though it had been typed on the keyboard.
  1226. </p><p>
  1227. If <em class="replaceable"><code>filename</code></em> is <code class="literal">-</code>
  1228. (hyphen), then standard input is read until an EOF indication
  1229. or <code class="command">\q</code> meta-command. This can be used to intersperse
  1230. interactive input with input from files. Note that Readline behavior
  1231. will be used only if it is active at the outermost level.
  1232. </p><div class="note"><h3 class="title">Note</h3><p>
  1233. If you want to see the lines on the screen as they are read you
  1234. must set the variable <code class="varname">ECHO</code> to
  1235. <code class="literal">all</code>.
  1236. </p></div></dd><dt id="PSQL-METACOMMAND-IF"><span class="term"><code class="literal">\if</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\elif</code> <em class="replaceable"><code>expression</code></em><br /></span><span class="term"><code class="literal">\else</code><br /></span><span class="term"><code class="literal">\endif</code></span></dt><dd><p>
  1237. This group of commands implements nestable conditional blocks.
  1238. A conditional block must begin with an <code class="command">\if</code> and end
  1239. with an <code class="command">\endif</code>. In between there may be any number
  1240. of <code class="command">\elif</code> clauses, which may optionally be followed
  1241. by a single <code class="command">\else</code> clause. Ordinary queries and
  1242. other types of backslash commands may (and usually do) appear between
  1243. the commands forming a conditional block.
  1244. </p><p>
  1245. The <code class="command">\if</code> and <code class="command">\elif</code> commands read
  1246. their argument(s) and evaluate them as a boolean expression. If the
  1247. expression yields <code class="literal">true</code> then processing continues
  1248. normally; otherwise, lines are skipped until a
  1249. matching <code class="command">\elif</code>, <code class="command">\else</code>,
  1250. or <code class="command">\endif</code> is reached. Once
  1251. an <code class="command">\if</code> or <code class="command">\elif</code> test has
  1252. succeeded, the arguments of later <code class="command">\elif</code> commands in
  1253. the same block are not evaluated but are treated as false. Lines
  1254. following an <code class="command">\else</code> are processed only if no earlier
  1255. matching <code class="command">\if</code> or <code class="command">\elif</code> succeeded.
  1256. </p><p>
  1257. The <em class="replaceable"><code>expression</code></em> argument
  1258. of an <code class="command">\if</code> or <code class="command">\elif</code> command
  1259. is subject to variable interpolation and backquote expansion, just
  1260. like any other backslash command argument. After that it is evaluated
  1261. like the value of an on/off option variable. So a valid value
  1262. is any unambiguous case-insensitive match for one of:
  1263. <code class="literal">true</code>, <code class="literal">false</code>, <code class="literal">1</code>,
  1264. <code class="literal">0</code>, <code class="literal">on</code>, <code class="literal">off</code>,
  1265. <code class="literal">yes</code>, <code class="literal">no</code>. For example,
  1266. <code class="literal">t</code>, <code class="literal">T</code>, and <code class="literal">tR</code>
  1267. will all be considered to be <code class="literal">true</code>.
  1268. </p><p>
  1269. Expressions that do not properly evaluate to true or false will
  1270. generate a warning and be treated as false.
  1271. </p><p>
  1272. Lines being skipped are parsed normally to identify queries and
  1273. backslash commands, but queries are not sent to the server, and
  1274. backslash commands other than conditionals
  1275. (<code class="command">\if</code>, <code class="command">\elif</code>,
  1276. <code class="command">\else</code>, <code class="command">\endif</code>) are
  1277. ignored. Conditional commands are checked only for valid nesting.
  1278. Variable references in skipped lines are not expanded, and backquote
  1279. expansion is not performed either.
  1280. </p><p>
  1281. All the backslash commands of a given conditional block must appear in
  1282. the same source file. If EOF is reached on the main input file or an
  1283. <code class="command">\include</code>-ed file before all local
  1284. <code class="command">\if</code>-blocks have been closed,
  1285. then <span class="application">psql</span> will raise an error.
  1286. </p><p>
  1287. Here is an example:
  1288. </p><pre class="programlisting">
  1289. -- check for the existence of two separate records in the database and store
  1290. -- the results in separate psql variables
  1291. SELECT
  1292. EXISTS(SELECT 1 FROM customer WHERE customer_id = 123) as is_customer,
  1293. EXISTS(SELECT 1 FROM employee WHERE employee_id = 456) as is_employee
  1294. \gset
  1295. \if :is_customer
  1296. SELECT * FROM customer WHERE customer_id = 123;
  1297. \elif :is_employee
  1298. \echo 'is not a customer but is an employee'
  1299. SELECT * FROM employee WHERE employee_id = 456;
  1300. \else
  1301. \if yes
  1302. \echo 'not a customer or employee'
  1303. \else
  1304. \echo 'this will never print'
  1305. \endif
  1306. \endif
  1307. </pre></dd><dt><span class="term"><code class="literal">\ir</code> or <code class="literal">\include_relative</code> <em class="replaceable"><code>filename</code></em></span></dt><dd><p>
  1308. The <code class="literal">\ir</code> command is similar to <code class="literal">\i</code>, but resolves
  1309. relative file names differently. When executing in interactive mode,
  1310. the two commands behave identically. However, when invoked from a
  1311. script, <code class="literal">\ir</code> interprets file names relative to the
  1312. directory in which the script is located, rather than the current
  1313. working directory.
  1314. </p></dd><dt><span class="term"><code class="literal">\l[+]</code> or <code class="literal">\list[+] [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  1315. List the databases in the server and show their names, owners,
  1316. character set encodings, and access privileges.
  1317. If <em class="replaceable"><code>pattern</code></em> is specified,
  1318. only databases whose names match the pattern are listed.
  1319. If <code class="literal">+</code> is appended to the command name, database
  1320. sizes, default tablespaces, and descriptions are also displayed.
  1321. (Size information is only available for databases that the current
  1322. user can connect to.)
  1323. </p></dd><dt><span class="term"><code class="literal">\lo_export <em class="replaceable"><code>loid</code></em> <em class="replaceable"><code>filename</code></em></code></span></dt><dd><p>
  1324. Reads the large object with <acronym class="acronym">OID</acronym> <em class="replaceable"><code>loid</code></em> from the database and
  1325. writes it to <em class="replaceable"><code>filename</code></em>. Note that this is
  1326. subtly different from the server function
  1327. <code class="function">lo_export</code>, which acts with the permissions
  1328. of the user that the database server runs as and on the server's
  1329. file system.
  1330. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1331. Use <code class="command">\lo_list</code> to find out the large object's
  1332. <acronym class="acronym">OID</acronym>.
  1333. </p></div></dd><dt><span class="term"><code class="literal">\lo_import <em class="replaceable"><code>filename</code></em> [ <em class="replaceable"><code>comment</code></em> ]</code></span></dt><dd><p>
  1334. Stores the file into a <span class="productname">PostgreSQL</span>
  1335. large object. Optionally, it associates the given
  1336. comment with the object. Example:
  1337. </p><pre class="programlisting">
  1338. foo=&gt; <strong class="userinput"><code>\lo_import '/home/peter/pictures/photo.xcf' 'a picture of me'</code></strong>
  1339. lo_import 152801
  1340. </pre><p>
  1341. The response indicates that the large object received object
  1342. ID 152801, which can be used to access the newly-created large
  1343. object in the future. For the sake of readability, it is
  1344. recommended to always associate a human-readable comment with
  1345. every object. Both OIDs and comments can be viewed with the
  1346. <code class="command">\lo_list</code> command.
  1347. </p><p>
  1348. Note that this command is subtly different from the server-side
  1349. <code class="function">lo_import</code> because it acts as the local user
  1350. on the local file system, rather than the server's user and file
  1351. system.
  1352. </p></dd><dt><span class="term"><code class="literal">\lo_list</code></span></dt><dd><p>
  1353. Shows a list of all <span class="productname">PostgreSQL</span>
  1354. large objects currently stored in the database,
  1355. along with any comments provided for them.
  1356. </p></dd><dt><span class="term"><code class="literal">\lo_unlink <em class="replaceable"><code>loid</code></em></code></span></dt><dd><p>
  1357. Deletes the large object with <acronym class="acronym">OID</acronym>
  1358. <em class="replaceable"><code>loid</code></em> from the
  1359. database.
  1360. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1361. Use <code class="command">\lo_list</code> to find out the large object's
  1362. <acronym class="acronym">OID</acronym>.
  1363. </p></div></dd><dt><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ <em class="replaceable"><code>filename</code></em> ]</code><br /></span><span class="term"><code class="literal">\o</code> or <code class="literal">\out [ |<em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
  1364. Arranges to save future query results to the file <em class="replaceable"><code>filename</code></em> or pipe future results
  1365. to the shell command <em class="replaceable"><code>command</code></em>. If no argument is
  1366. specified, the query output is reset to the standard output.
  1367. </p><p>
  1368. If the argument begins with <code class="literal">|</code>, then the entire remainder
  1369. of the line is taken to be
  1370. the <em class="replaceable"><code>command</code></em> to execute,
  1371. and neither variable interpolation nor backquote expansion are
  1372. performed in it. The rest of the line is simply passed literally to
  1373. the shell.
  1374. </p><p>
  1375. <span class="quote">“<span class="quote">Query results</span>”</span> includes all tables, command
  1376. responses, and notices obtained from the database server, as
  1377. well as output of various backslash commands that query the
  1378. database (such as <code class="command">\d</code>); but not error
  1379. messages.
  1380. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1381. To intersperse text output in between query results, use
  1382. <code class="command">\qecho</code>.
  1383. </p></div></dd><dt><span class="term"><code class="literal">\p</code> or <code class="literal">\print</code></span></dt><dd><p>
  1384. Print the current query buffer to the standard output.
  1385. If the current query buffer is empty, the most recently executed query
  1386. is printed instead.
  1387. </p></dd><dt><span class="term"><code class="literal">\password [ <em class="replaceable"><code>username</code></em> ]</code></span></dt><dd><p>
  1388. Changes the password of the specified user (by default, the current
  1389. user). This command prompts for the new password, encrypts it, and
  1390. sends it to the server as an <code class="command">ALTER ROLE</code> command. This
  1391. makes sure that the new password does not appear in cleartext in the
  1392. command history, the server log, or elsewhere.
  1393. </p></dd><dt><span class="term"><code class="literal">\prompt [ <em class="replaceable"><code>text</code></em> ] <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
  1394. Prompts the user to supply text, which is assigned to the variable
  1395. <em class="replaceable"><code>name</code></em>.
  1396. An optional prompt string, <em class="replaceable"><code>text</code></em>, can be specified. (For multiword
  1397. prompts, surround the text with single quotes.)
  1398. </p><p>
  1399. By default, <code class="literal">\prompt</code> uses the terminal for input and
  1400. output. However, if the <code class="option">-f</code> command line switch was
  1401. used, <code class="literal">\prompt</code> uses standard input and standard output.
  1402. </p></dd><dt><span class="term"><code class="literal">\pset [ <em class="replaceable"><code>option</code></em> [ <em class="replaceable"><code>value</code></em> ] ]</code></span></dt><dd><p>
  1403. This command sets options affecting the output of query result tables.
  1404. <em class="replaceable"><code>option</code></em>
  1405. indicates which option is to be set. The semantics of
  1406. <em class="replaceable"><code>value</code></em> vary depending
  1407. on the selected option. For some options, omitting <em class="replaceable"><code>value</code></em> causes the option to be toggled
  1408. or unset, as described under the particular option. If no such
  1409. behavior is mentioned, then omitting
  1410. <em class="replaceable"><code>value</code></em> just results in
  1411. the current setting being displayed.
  1412. </p><p>
  1413. <code class="command">\pset</code> without any arguments displays the current status
  1414. of all printing options.
  1415. </p><p>
  1416. Adjustable printing options are:
  1417. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">border</code></span></dt><dd><p>
  1418. The <em class="replaceable"><code>value</code></em> must be a
  1419. number. In general, the higher
  1420. the number the more borders and lines the tables will have,
  1421. but details depend on the particular format.
  1422. In <acronym class="acronym">HTML</acronym> format, this will translate directly
  1423. into the <code class="literal">border=...</code> attribute.
  1424. In most other formats only values 0 (no border), 1 (internal
  1425. dividing lines), and 2 (table frame) make sense, and values above 2
  1426. will be treated the same as <code class="literal">border = 2</code>.
  1427. The <code class="literal">latex</code> and <code class="literal">latex-longtable</code>
  1428. formats additionally allow a value of 3 to add dividing lines
  1429. between data rows.
  1430. </p></dd><dt><span class="term"><code class="literal">columns</code></span></dt><dd><p>
  1431. Sets the target width for the <code class="literal">wrapped</code> format, and also
  1432. the width limit for determining whether output is wide enough to
  1433. require the pager or switch to the vertical display in expanded auto
  1434. mode.
  1435. Zero (the default) causes the target width to be controlled by the
  1436. environment variable <code class="envar">COLUMNS</code>, or the detected screen width
  1437. if <code class="envar">COLUMNS</code> is not set.
  1438. In addition, if <code class="literal">columns</code> is zero then the
  1439. <code class="literal">wrapped</code> format only affects screen output.
  1440. If <code class="literal">columns</code> is nonzero then file and pipe output is
  1441. wrapped to that width as well.
  1442. </p></dd><dt><span class="term"><code class="literal">csv_fieldsep</code></span></dt><dd><p>
  1443. Specifies the field separator to be used in
  1444. <acronym class="acronym">CSV</acronym> output format. If the separator character
  1445. appears in a field's value, that field is output within double
  1446. quotes, following standard <acronym class="acronym">CSV</acronym> rules.
  1447. The default is a comma.
  1448. </p></dd><dt><span class="term"><code class="literal">expanded</code> (or <code class="literal">x</code>)</span></dt><dd><p>
  1449. If <em class="replaceable"><code>value</code></em> is specified it
  1450. must be either <code class="literal">on</code> or <code class="literal">off</code>, which
  1451. will enable or disable expanded mode, or <code class="literal">auto</code>.
  1452. If <em class="replaceable"><code>value</code></em> is omitted the
  1453. command toggles between the on and off settings. When expanded mode
  1454. is enabled, query results are displayed in two columns, with the
  1455. column name on the left and the data on the right. This mode is
  1456. useful if the data wouldn't fit on the screen in the
  1457. normal <span class="quote">“<span class="quote">horizontal</span>”</span> mode. In the auto setting, the
  1458. expanded mode is used whenever the query output has more than one
  1459. column and is wider than the screen; otherwise, the regular mode is
  1460. used. The auto setting is only
  1461. effective in the aligned and wrapped formats. In other formats, it
  1462. always behaves as if the expanded mode is off.
  1463. </p></dd><dt><span class="term"><code class="literal">fieldsep</code></span></dt><dd><p>
  1464. Specifies the field separator to be used in unaligned output
  1465. format. That way one can create, for example, tab-separated
  1466. output, which other programs might prefer. To
  1467. set a tab as field separator, type <code class="literal">\pset fieldsep
  1468. '\t'</code>. The default field separator is
  1469. <code class="literal">'|'</code> (a vertical bar).
  1470. </p></dd><dt><span class="term"><code class="literal">fieldsep_zero</code></span></dt><dd><p>
  1471. Sets the field separator to use in unaligned output format to a zero
  1472. byte.
  1473. </p></dd><dt><span class="term"><code class="literal">footer</code></span></dt><dd><p>
  1474. If <em class="replaceable"><code>value</code></em> is specified
  1475. it must be either <code class="literal">on</code> or <code class="literal">off</code>
  1476. which will enable or disable display of the table footer
  1477. (the <code class="literal">(<em class="replaceable"><code>n</code></em> rows)</code> count).
  1478. If <em class="replaceable"><code>value</code></em> is omitted the
  1479. command toggles footer display on or off.
  1480. </p></dd><dt><span class="term"><code class="literal">format</code></span></dt><dd><p>
  1481. Sets the output format to one of <code class="literal">aligned</code>,
  1482. <code class="literal">asciidoc</code>,
  1483. <code class="literal">csv</code>,
  1484. <code class="literal">html</code>,
  1485. <code class="literal">latex</code>,
  1486. <code class="literal">latex-longtable</code>, <code class="literal">troff-ms</code>,
  1487. <code class="literal">unaligned</code>, or <code class="literal">wrapped</code>.
  1488. Unique abbreviations are allowed.
  1489. </p><p><code class="literal">aligned</code> format is the standard,
  1490. human-readable, nicely formatted text output; this is the default.
  1491. </p><p><code class="literal">unaligned</code> format writes all columns of a row on one
  1492. line, separated by the currently active field separator. This
  1493. is useful for creating output that might be intended to be read
  1494. in by other programs, for example, tab-separated or comma-separated
  1495. format. However, the field separator character is not treated
  1496. specially if it appears in a column's value;
  1497. so <acronym class="acronym">CSV</acronym> format may be better suited for such
  1498. purposes.
  1499. </p><p><code class="literal">csv</code> format
  1500. <a id="id-1.9.4.18.8.4.10.1.69.2.3.1.8.2.4.2" class="indexterm"></a>
  1501. writes column values separated by commas, applying the quoting
  1502. rules described in
  1503. <a class="ulink" href="https://tools.ietf.org/html/rfc4180" target="_top">RFC 4180</a>.
  1504. This output is compatible with the CSV format of the server's
  1505. <code class="command">COPY</code> command.
  1506. A header line with column names is generated unless
  1507. the <code class="literal">tuples_only</code> parameter is
  1508. <code class="literal">on</code>. Titles and footers are not printed.
  1509. Each row is terminated by the system-dependent end-of-line character,
  1510. which is typically a single newline (<code class="literal">\n</code>) for
  1511. Unix-like systems or a carriage return and newline sequence
  1512. (<code class="literal">\r\n</code>) for Microsoft Windows.
  1513. Field separator characters other than comma can be selected with
  1514. <code class="command">\pset csv_fieldsep</code>.
  1515. </p><p><code class="literal">wrapped</code> format is like <code class="literal">aligned</code> but wraps
  1516. wide data values across lines to make the output fit in the target
  1517. column width. The target width is determined as described under
  1518. the <code class="literal">columns</code> option. Note that <span class="application">psql</span> will
  1519. not attempt to wrap column header titles; therefore,
  1520. <code class="literal">wrapped</code> format behaves the same as <code class="literal">aligned</code>
  1521. if the total width needed for column headers exceeds the target.
  1522. </p><p>
  1523. The <code class="literal">asciidoc</code>, <code class="literal">html</code>,
  1524. <code class="literal">latex</code>, <code class="literal">latex-longtable</code>, and
  1525. <code class="literal">troff-ms</code> formats put out tables that are intended
  1526. to be included in documents using the respective mark-up
  1527. language. They are not complete documents! This might not be
  1528. necessary in <acronym class="acronym">HTML</acronym>, but in
  1529. <span class="application">LaTeX</span> you must have a complete
  1530. document wrapper.
  1531. The <code class="literal">latex</code> format
  1532. uses <span class="application">LaTeX</span>'s <code class="literal">tabular</code>
  1533. environment.
  1534. The <code class="literal">latex-longtable</code> format
  1535. requires the <span class="application">LaTeX</span>
  1536. <code class="literal">longtable</code> and <code class="literal">booktabs</code> packages.
  1537. </p></dd><dt><span class="term"><code class="literal">linestyle</code></span></dt><dd><p>
  1538. Sets the border line drawing style to one
  1539. of <code class="literal">ascii</code>, <code class="literal">old-ascii</code>,
  1540. or <code class="literal">unicode</code>.
  1541. Unique abbreviations are allowed. (That would mean one
  1542. letter is enough.)
  1543. The default setting is <code class="literal">ascii</code>.
  1544. This option only affects the <code class="literal">aligned</code> and
  1545. <code class="literal">wrapped</code> output formats.
  1546. </p><p><code class="literal">ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
  1547. characters. Newlines in data are shown using
  1548. a <code class="literal">+</code> symbol in the right-hand margin.
  1549. When the <code class="literal">wrapped</code> format wraps data from
  1550. one line to the next without a newline character, a dot
  1551. (<code class="literal">.</code>) is shown in the right-hand margin of the first line,
  1552. and again in the left-hand margin of the following line.
  1553. </p><p><code class="literal">old-ascii</code> style uses plain <acronym class="acronym">ASCII</acronym>
  1554. characters, using the formatting style used
  1555. in <span class="productname">PostgreSQL</span> 8.4 and earlier.
  1556. Newlines in data are shown using a <code class="literal">:</code>
  1557. symbol in place of the left-hand column separator.
  1558. When the data is wrapped from one line
  1559. to the next without a newline character, a <code class="literal">;</code>
  1560. symbol is used in place of the left-hand column separator.
  1561. </p><p><code class="literal">unicode</code> style uses Unicode box-drawing characters.
  1562. Newlines in data are shown using a carriage return symbol
  1563. in the right-hand margin. When the data is wrapped from one line
  1564. to the next without a newline character, an ellipsis symbol
  1565. is shown in the right-hand margin of the first line, and
  1566. again in the left-hand margin of the following line.
  1567. </p><p>
  1568. When the <code class="literal">border</code> setting is greater than zero,
  1569. the <code class="literal">linestyle</code> option also determines the
  1570. characters with which the border lines are drawn.
  1571. Plain <acronym class="acronym">ASCII</acronym> characters work everywhere, but
  1572. Unicode characters look nicer on displays that recognize them.
  1573. </p></dd><dt><span class="term"><code class="literal">null</code></span></dt><dd><p>
  1574. Sets the string to be printed in place of a null value.
  1575. The default is to print nothing, which can easily be mistaken for
  1576. an empty string. For example, one might prefer <code class="literal">\pset null
  1577. '(null)'</code>.
  1578. </p></dd><dt><span class="term"><code class="literal">numericlocale</code></span></dt><dd><p>
  1579. If <em class="replaceable"><code>value</code></em> is specified
  1580. it must be either <code class="literal">on</code> or <code class="literal">off</code>
  1581. which will enable or disable display of a locale-specific character
  1582. to separate groups of digits to the left of the decimal marker.
  1583. If <em class="replaceable"><code>value</code></em> is omitted the
  1584. command toggles between regular and locale-specific numeric output.
  1585. </p></dd><dt><span class="term"><code class="literal">pager</code></span></dt><dd><p>
  1586. Controls use of a pager program for query and <span class="application">psql</span>
  1587. help output. If the environment variable <code class="envar">PSQL_PAGER</code>
  1588. or <code class="envar">PAGER</code> is set, the output is piped to the
  1589. specified program. Otherwise a platform-dependent default program
  1590. (such as <code class="filename">more</code>) is used.
  1591. </p><p>
  1592. When the <code class="literal">pager</code> option is <code class="literal">off</code>, the pager
  1593. program is not used. When the <code class="literal">pager</code> option is
  1594. <code class="literal">on</code>, the pager is used when appropriate, i.e., when the
  1595. output is to a terminal and will not fit on the screen.
  1596. The <code class="literal">pager</code> option can also be set to <code class="literal">always</code>,
  1597. which causes the pager to be used for all terminal output regardless
  1598. of whether it fits on the screen. <code class="literal">\pset pager</code>
  1599. without a <em class="replaceable"><code>value</code></em>
  1600. toggles pager use on and off.
  1601. </p></dd><dt><span class="term"><code class="literal">pager_min_lines</code></span></dt><dd><p>
  1602. If <code class="literal">pager_min_lines</code> is set to a number greater than the
  1603. page height, the pager program will not be called unless there are
  1604. at least this many lines of output to show. The default setting
  1605. is 0.
  1606. </p></dd><dt><span class="term"><code class="literal">recordsep</code></span></dt><dd><p>
  1607. Specifies the record (line) separator to use in unaligned
  1608. output format. The default is a newline character.
  1609. </p></dd><dt><span class="term"><code class="literal">recordsep_zero</code></span></dt><dd><p>
  1610. Sets the record separator to use in unaligned output format to a zero
  1611. byte.
  1612. </p></dd><dt><span class="term"><code class="literal">tableattr</code> (or <code class="literal">T</code>)</span></dt><dd><p>
  1613. In <acronym class="acronym">HTML</acronym> format, this specifies attributes
  1614. to be placed inside the <code class="sgmltag-element">table</code> tag. This
  1615. could for example be <code class="literal">cellpadding</code> or
  1616. <code class="literal">bgcolor</code>. Note that you probably don't want
  1617. to specify <code class="literal">border</code> here, as that is already
  1618. taken care of by <code class="literal">\pset border</code>.
  1619. If no
  1620. <em class="replaceable"><code>value</code></em> is given,
  1621. the table attributes are unset.
  1622. </p><p>
  1623. In <code class="literal">latex-longtable</code> format, this controls
  1624. the proportional width of each column containing a left-aligned
  1625. data type. It is specified as a whitespace-separated list of values,
  1626. e.g. <code class="literal">'0.2 0.2 0.6'</code>. Unspecified output columns
  1627. use the last specified value.
  1628. </p></dd><dt><span class="term"><code class="literal">title</code> (or <code class="literal">C</code>)</span></dt><dd><p>
  1629. Sets the table title for any subsequently printed tables. This
  1630. can be used to give your output descriptive tags. If no
  1631. <em class="replaceable"><code>value</code></em> is given,
  1632. the title is unset.
  1633. </p></dd><dt><span class="term"><code class="literal">tuples_only</code> (or <code class="literal">t</code>)</span></dt><dd><p>
  1634. If <em class="replaceable"><code>value</code></em> is specified
  1635. it must be either <code class="literal">on</code> or <code class="literal">off</code>
  1636. which will enable or disable tuples-only mode.
  1637. If <em class="replaceable"><code>value</code></em> is omitted the
  1638. command toggles between regular and tuples-only output.
  1639. Regular output includes extra information such
  1640. as column headers, titles, and various footers. In tuples-only
  1641. mode, only actual table data is shown.
  1642. </p></dd><dt><span class="term"><code class="literal">unicode_border_linestyle</code></span></dt><dd><p>
  1643. Sets the border drawing style for the <code class="literal">unicode</code>
  1644. line style to one of <code class="literal">single</code>
  1645. or <code class="literal">double</code>.
  1646. </p></dd><dt><span class="term"><code class="literal">unicode_column_linestyle</code></span></dt><dd><p>
  1647. Sets the column drawing style for the <code class="literal">unicode</code>
  1648. line style to one of <code class="literal">single</code>
  1649. or <code class="literal">double</code>.
  1650. </p></dd><dt><span class="term"><code class="literal">unicode_header_linestyle</code></span></dt><dd><p>
  1651. Sets the header drawing style for the <code class="literal">unicode</code>
  1652. line style to one of <code class="literal">single</code>
  1653. or <code class="literal">double</code>.
  1654. </p></dd></dl></div><p>
  1655. </p><p>
  1656. Illustrations of how these different formats look can be seen in
  1657. the <a class="xref" href="app-psql.html#APP-PSQL-EXAMPLES" title="Examples">Examples</a> section.
  1658. </p><div class="tip"><h3 class="title">Tip</h3><p>
  1659. There are various shortcut commands for <code class="command">\pset</code>. See
  1660. <code class="command">\a</code>, <code class="command">\C</code>, <code class="command">\f</code>,
  1661. <code class="command">\H</code>, <code class="command">\t</code>, <code class="command">\T</code>,
  1662. and <code class="command">\x</code>.
  1663. </p></div></dd><dt><span class="term"><code class="literal">\q</code> or <code class="literal">\quit</code></span></dt><dd><p>
  1664. Quits the <span class="application">psql</span> program.
  1665. In a script file, only execution of that script is terminated.
  1666. </p></dd><dt><span class="term"><code class="literal">\qecho <em class="replaceable"><code>text</code></em> [ ... ] </code></span></dt><dd><p>
  1667. This command is identical to <code class="command">\echo</code> except
  1668. that the output will be written to the query output channel, as
  1669. set by <code class="command">\o</code>.
  1670. </p></dd><dt><span class="term"><code class="literal">\r</code> or <code class="literal">\reset</code></span></dt><dd><p>
  1671. Resets (clears) the query buffer.
  1672. </p></dd><dt><span class="term"><code class="literal">\s [ <em class="replaceable"><code>filename</code></em> ]</code></span></dt><dd><p>
  1673. Print <span class="application">psql</span>'s command line history
  1674. to <em class="replaceable"><code>filename</code></em>.
  1675. If <em class="replaceable"><code>filename</code></em> is omitted,
  1676. the history is written to the standard output (using the pager if
  1677. appropriate). This command is not available
  1678. if <span class="application">psql</span> was built
  1679. without <span class="application">Readline</span> support.
  1680. </p></dd><dt><span class="term"><code class="literal">\set [ <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> [ ... ] ] ]</code></span></dt><dd><p>
  1681. Sets the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if more than one value
  1682. is given, to the concatenation of all of them. If only one
  1683. argument is given, the variable is set to an empty-string value. To
  1684. unset a variable, use the <code class="command">\unset</code> command.
  1685. </p><p><code class="command">\set</code> without any arguments displays the names and values
  1686. of all currently-set <span class="application">psql</span> variables.
  1687. </p><p>
  1688. Valid variable names can contain letters, digits, and
  1689. underscores. See the section <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> below for details.
  1690. Variable names are case-sensitive.
  1691. </p><p>
  1692. Certain variables are special, in that they
  1693. control <span class="application">psql</span>'s behavior or are
  1694. automatically set to reflect connection state. These variables are
  1695. documented in <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
  1696. </p><div class="note"><h3 class="title">Note</h3><p>
  1697. This command is unrelated to the <acronym class="acronym">SQL</acronym>
  1698. command <a class="xref" href="sql-set.html" title="SET"><span class="refentrytitle">SET</span></a>.
  1699. </p></div></dd><dt><span class="term"><code class="literal">\setenv <em class="replaceable"><code>name</code></em> [ <em class="replaceable"><code>value</code></em> ]</code></span></dt><dd><p>
  1700. Sets the environment variable <em class="replaceable"><code>name</code></em> to <em class="replaceable"><code>value</code></em>, or if the
  1701. <em class="replaceable"><code>value</code></em> is
  1702. not supplied, unsets the environment variable. Example:
  1703. </p><pre class="programlisting">
  1704. testdb=&gt; <strong class="userinput"><code>\setenv PAGER less</code></strong>
  1705. testdb=&gt; <strong class="userinput"><code>\setenv LESS -imx4F</code></strong>
  1706. </pre></dd><dt><span class="term"><code class="literal">\sf[+] <em class="replaceable"><code>function_description</code></em> </code></span></dt><dd><p>
  1707. This command fetches and shows the definition of the named function or procedure,
  1708. in the form of a <code class="command">CREATE OR REPLACE FUNCTION</code> or
  1709. <code class="command">CREATE OR REPLACE PROCEDURE</code> command.
  1710. The definition is printed to the current query output channel,
  1711. as set by <code class="command">\o</code>.
  1712. </p><p>
  1713. The target function can be specified by name alone, or by name
  1714. and arguments, for example <code class="literal">foo(integer, text)</code>.
  1715. The argument types must be given if there is more
  1716. than one function of the same name.
  1717. </p><p>
  1718. If <code class="literal">+</code> is appended to the command name, then the
  1719. output lines are numbered, with the first line of the function body
  1720. being line 1.
  1721. </p><p>
  1722. Unlike most other meta-commands, the entire remainder of the line is
  1723. always taken to be the argument(s) of <code class="command">\sf</code>, and neither
  1724. variable interpolation nor backquote expansion are performed in the
  1725. arguments.
  1726. </p></dd><dt><span class="term"><code class="literal">\sv[+] <em class="replaceable"><code>view_name</code></em> </code></span></dt><dd><p>
  1727. This command fetches and shows the definition of the named view,
  1728. in the form of a <code class="command">CREATE OR REPLACE VIEW</code> command.
  1729. The definition is printed to the current query output channel,
  1730. as set by <code class="command">\o</code>.
  1731. </p><p>
  1732. If <code class="literal">+</code> is appended to the command name, then the
  1733. output lines are numbered from 1.
  1734. </p><p>
  1735. Unlike most other meta-commands, the entire remainder of the line is
  1736. always taken to be the argument(s) of <code class="command">\sv</code>, and neither
  1737. variable interpolation nor backquote expansion are performed in the
  1738. arguments.
  1739. </p></dd><dt><span class="term"><code class="literal">\t</code></span></dt><dd><p>
  1740. Toggles the display of output column name headings and row count
  1741. footer. This command is equivalent to <code class="literal">\pset
  1742. tuples_only</code> and is provided for convenience.
  1743. </p></dd><dt><span class="term"><code class="literal">\T <em class="replaceable"><code>table_options</code></em></code></span></dt><dd><p>
  1744. Specifies attributes to be placed within the
  1745. <code class="sgmltag-element">table</code> tag in <acronym class="acronym">HTML</acronym>
  1746. output format. This command is equivalent to <code class="literal">\pset
  1747. tableattr <em class="replaceable"><code>table_options</code></em></code>.
  1748. </p></dd><dt><span class="term"><code class="literal">\timing [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> ]</code></span></dt><dd><p>
  1749. With a parameter, turns displaying of how long each SQL statement
  1750. takes on or off. Without a parameter, toggles the display between
  1751. on and off. The display is in milliseconds; intervals longer than
  1752. 1 second are also shown in minutes:seconds format, with hours and
  1753. days fields added if needed.
  1754. </p></dd><dt><span class="term"><code class="literal">\unset <em class="replaceable"><code>name</code></em></code></span></dt><dd><p>
  1755. Unsets (deletes) the <span class="application">psql</span> variable <em class="replaceable"><code>name</code></em>.
  1756. </p><p>
  1757. Most variables that control <span class="application">psql</span>'s behavior
  1758. cannot be unset; instead, an <code class="literal">\unset</code> command is interpreted
  1759. as setting them to their default values.
  1760. See <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a>, below.
  1761. </p></dd><dt><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <em class="replaceable"><code>filename</code></em><br /></span><span class="term"><code class="literal">\w</code> or <code class="literal">\write</code> <code class="literal">|</code><em class="replaceable"><code>command</code></em></span></dt><dd><p>
  1762. Writes the current query buffer to the file <em class="replaceable"><code>filename</code></em> or pipes it to the shell
  1763. command <em class="replaceable"><code>command</code></em>.
  1764. If the current query buffer is empty, the most recently executed query
  1765. is written instead.
  1766. </p><p>
  1767. If the argument begins with <code class="literal">|</code>, then the entire remainder
  1768. of the line is taken to be
  1769. the <em class="replaceable"><code>command</code></em> to execute,
  1770. and neither variable interpolation nor backquote expansion are
  1771. performed in it. The rest of the line is simply passed literally to
  1772. the shell.
  1773. </p></dd><dt><span class="term"><code class="literal">\watch [ <em class="replaceable"><code>seconds</code></em> ]</code></span></dt><dd><p>
  1774. Repeatedly execute the current query buffer (as <code class="literal">\g</code> does)
  1775. until interrupted or the query fails. Wait the specified number of
  1776. seconds (default 2) between executions. Each query result is
  1777. displayed with a header that includes the <code class="literal">\pset title</code>
  1778. string (if any), the time as of query start, and the delay interval.
  1779. </p><p>
  1780. If the current query buffer is empty, the most recently sent query
  1781. is re-executed instead.
  1782. </p></dd><dt><span class="term"><code class="literal">\x [ <em class="replaceable"><code>on</code></em> | <em class="replaceable"><code>off</code></em> | <em class="replaceable"><code>auto</code></em> ]</code></span></dt><dd><p>
  1783. Sets or toggles expanded table formatting mode. As such it is equivalent to
  1784. <code class="literal">\pset expanded</code>.
  1785. </p></dd><dt><span class="term"><code class="literal">\z [ <a class="link" href="app-psql.html#APP-PSQL-PATTERNS" title="Patterns"><em class="replaceable"><code>pattern</code></em></a> ]</code></span></dt><dd><p>
  1786. Lists tables, views and sequences with their
  1787. associated access privileges.
  1788. If a <em class="replaceable"><code>pattern</code></em> is
  1789. specified, only tables, views and sequences whose names match the
  1790. pattern are listed.
  1791. </p><p>
  1792. This is an alias for <code class="command">\dp</code> (<span class="quote">“<span class="quote">display
  1793. privileges</span>”</span>).
  1794. </p></dd><dt><span class="term"><code class="literal">\! [ <em class="replaceable"><code>command</code></em> ]</code></span></dt><dd><p>
  1795. With no argument, escapes to a sub-shell; <span class="application">psql</span>
  1796. resumes when the sub-shell exits. With an argument, executes the
  1797. shell command <em class="replaceable"><code>command</code></em>.
  1798. </p><p>
  1799. Unlike most other meta-commands, the entire remainder of the line is
  1800. always taken to be the argument(s) of <code class="command">\!</code>, and neither
  1801. variable interpolation nor backquote expansion are performed in the
  1802. arguments. The rest of the line is simply passed literally to the
  1803. shell.
  1804. </p></dd><dt><span class="term"><code class="literal">\? [ <em class="replaceable"><code>topic</code></em> ]</code></span></dt><dd><p>
  1805. Shows help information. The optional
  1806. <em class="replaceable"><code>topic</code></em> parameter
  1807. (defaulting to <code class="literal">commands</code>) selects which part of <span class="application">psql</span> is
  1808. explained: <code class="literal">commands</code> describes <span class="application">psql</span>'s
  1809. backslash commands; <code class="literal">options</code> describes the command-line
  1810. options that can be passed to <span class="application">psql</span>;
  1811. and <code class="literal">variables</code> shows help about <span class="application">psql</span> configuration
  1812. variables.
  1813. </p></dd><dt><span class="term"><code class="literal">\;</code></span></dt><dd><p>
  1814. Backslash-semicolon is not a meta-command in the same way as the
  1815. preceding commands; rather, it simply causes a semicolon to be
  1816. added to the query buffer without any further processing.
  1817. </p><p>
  1818. Normally, <span class="application">psql</span> will dispatch a SQL command to the
  1819. server as soon as it reaches the command-ending semicolon, even if
  1820. more input remains on the current line. Thus for example entering
  1821. </p><pre class="programlisting">
  1822. select 1; select 2; select 3;
  1823. </pre><p>
  1824. will result in the three SQL commands being individually sent to
  1825. the server, with each one's results being displayed before
  1826. continuing to the next command. However, a semicolon entered
  1827. as <code class="literal">\;</code> will not trigger command processing, so that the
  1828. command before it and the one after are effectively combined and
  1829. sent to the server in one request. So for example
  1830. </p><pre class="programlisting">
  1831. select 1\; select 2\; select 3;
  1832. </pre><p>
  1833. results in sending the three SQL commands to the server in a single
  1834. request, when the non-backslashed semicolon is reached.
  1835. The server executes such a request as a single transaction,
  1836. unless there are explicit <code class="command">BEGIN</code>/<code class="command">COMMIT</code>
  1837. commands included in the string to divide it into multiple
  1838. transactions. (See <a class="xref" href="protocol-flow.html#PROTOCOL-FLOW-MULTI-STATEMENT" title="52.2.2.1. Multiple Statements in a Simple Query">Section 52.2.2.1</a>
  1839. for more details about how the server handles multi-query strings.)
  1840. <span class="application">psql</span> prints only the last query result
  1841. it receives for each request; in this example, although all
  1842. three <code class="command">SELECT</code>s are indeed executed, <span class="application">psql</span>
  1843. only prints the <code class="literal">3</code>.
  1844. </p></dd></dl></div><p>
  1845. </p><div class="refsect3" id="APP-PSQL-PATTERNS"><h4>Patterns</h4><a id="id-1.9.4.18.8.4.11.2" class="indexterm"></a><p>
  1846. The various <code class="literal">\d</code> commands accept a <em class="replaceable"><code>pattern</code></em> parameter to specify the
  1847. object name(s) to be displayed. In the simplest case, a pattern
  1848. is just the exact name of the object. The characters within a
  1849. pattern are normally folded to lower case, just as in SQL names;
  1850. for example, <code class="literal">\dt FOO</code> will display the table named
  1851. <code class="literal">foo</code>. As in SQL names, placing double quotes around
  1852. a pattern stops folding to lower case. Should you need to include
  1853. an actual double quote character in a pattern, write it as a pair
  1854. of double quotes within a double-quote sequence; again this is in
  1855. accord with the rules for SQL quoted identifiers. For example,
  1856. <code class="literal">\dt "FOO""BAR"</code> will display the table named
  1857. <code class="literal">FOO"BAR</code> (not <code class="literal">foo"bar</code>). Unlike the normal
  1858. rules for SQL names, you can put double quotes around just part
  1859. of a pattern, for instance <code class="literal">\dt FOO"FOO"BAR</code> will display
  1860. the table named <code class="literal">fooFOObar</code>.
  1861. </p><p>
  1862. Whenever the <em class="replaceable"><code>pattern</code></em> parameter
  1863. is omitted completely, the <code class="literal">\d</code> commands display all objects
  1864. that are visible in the current schema search path — this is
  1865. equivalent to using <code class="literal">*</code> as the pattern.
  1866. (An object is said to be <em class="firstterm">visible</em> if its
  1867. containing schema is in the search path and no object of the same
  1868. kind and name appears earlier in the search path. This is equivalent to the
  1869. statement that the object can be referenced by name without explicit
  1870. schema qualification.)
  1871. To see all objects in the database regardless of visibility,
  1872. use <code class="literal">*.*</code> as the pattern.
  1873. </p><p>
  1874. Within a pattern, <code class="literal">*</code> matches any sequence of characters
  1875. (including no characters) and <code class="literal">?</code> matches any single character.
  1876. (This notation is comparable to Unix shell file name patterns.)
  1877. For example, <code class="literal">\dt int*</code> displays tables whose names
  1878. begin with <code class="literal">int</code>. But within double quotes, <code class="literal">*</code>
  1879. and <code class="literal">?</code> lose these special meanings and are just matched
  1880. literally.
  1881. </p><p>
  1882. A pattern that contains a dot (<code class="literal">.</code>) is interpreted as a schema
  1883. name pattern followed by an object name pattern. For example,
  1884. <code class="literal">\dt foo*.*bar*</code> displays all tables whose table name
  1885. includes <code class="literal">bar</code> that are in schemas whose schema name
  1886. starts with <code class="literal">foo</code>. When no dot appears, then the pattern
  1887. matches only objects that are visible in the current schema search path.
  1888. Again, a dot within double quotes loses its special meaning and is matched
  1889. literally.
  1890. </p><p>
  1891. Advanced users can use regular-expression notations such as character
  1892. classes, for example <code class="literal">[0-9]</code> to match any digit. All regular
  1893. expression special characters work as specified in
  1894. <a class="xref" href="functions-matching.html#FUNCTIONS-POSIX-REGEXP" title="9.7.3. POSIX Regular Expressions">Section 9.7.3</a>, except for <code class="literal">.</code> which
  1895. is taken as a separator as mentioned above, <code class="literal">*</code> which is
  1896. translated to the regular-expression notation <code class="literal">.*</code>,
  1897. <code class="literal">?</code> which is translated to <code class="literal">.</code>, and
  1898. <code class="literal">$</code> which is matched literally. You can emulate
  1899. these pattern characters at need by writing
  1900. <code class="literal">?</code> for <code class="literal">.</code>,
  1901. <code class="literal">(<em class="replaceable"><code>R</code></em>+|)</code> for
  1902. <code class="literal"><em class="replaceable"><code>R</code></em>*</code>, or
  1903. <code class="literal">(<em class="replaceable"><code>R</code></em>|)</code> for
  1904. <code class="literal"><em class="replaceable"><code>R</code></em>?</code>.
  1905. <code class="literal">$</code> is not needed as a regular-expression character since
  1906. the pattern must match the whole name, unlike the usual
  1907. interpretation of regular expressions (in other words, <code class="literal">$</code>
  1908. is automatically appended to your pattern). Write <code class="literal">*</code> at the
  1909. beginning and/or end if you don't wish the pattern to be anchored.
  1910. Note that within double quotes, all regular expression special characters
  1911. lose their special meanings and are matched literally. Also, the regular
  1912. expression special characters are matched literally in operator name
  1913. patterns (i.e., the argument of <code class="literal">\do</code>).
  1914. </p></div></div><div class="refsect2" id="id-1.9.4.18.8.5"><h3>Advanced Features</h3><div class="refsect3" id="APP-PSQL-VARIABLES"><h4>Variables</h4><p>
  1915. <span class="application">psql</span> provides variable substitution
  1916. features similar to common Unix command shells.
  1917. Variables are simply name/value pairs, where the value
  1918. can be any string of any length. The name must consist of letters
  1919. (including non-Latin letters), digits, and underscores.
  1920. </p><p>
  1921. To set a variable, use the <span class="application">psql</span> meta-command
  1922. <code class="command">\set</code>. For example,
  1923. </p><pre class="programlisting">
  1924. testdb=&gt; <strong class="userinput"><code>\set foo bar</code></strong>
  1925. </pre><p>
  1926. sets the variable <code class="literal">foo</code> to the value
  1927. <code class="literal">bar</code>. To retrieve the content of the variable, precede
  1928. the name with a colon, for example:
  1929. </p><pre class="programlisting">
  1930. testdb=&gt; <strong class="userinput"><code>\echo :foo</code></strong>
  1931. bar
  1932. </pre><p>
  1933. This works in both regular SQL commands and meta-commands; there is
  1934. more detail in <a class="xref" href="app-psql.html#APP-PSQL-INTERPOLATION" title="SQL Interpolation"><acronym class="acronym">SQL</acronym> Interpolation</a>, below.
  1935. </p><p>
  1936. If you call <code class="command">\set</code> without a second argument, the
  1937. variable is set to an empty-string value. To unset (i.e., delete)
  1938. a variable, use the command <code class="command">\unset</code>. To show the
  1939. values of all variables, call <code class="command">\set</code> without any argument.
  1940. </p><div class="note"><h3 class="title">Note</h3><p>
  1941. The arguments of <code class="command">\set</code> are subject to the same
  1942. substitution rules as with other commands. Thus you can construct
  1943. interesting references such as <code class="literal">\set :foo
  1944. 'something'</code> and get <span class="quote">“<span class="quote">soft links</span>”</span> or
  1945. <span class="quote">“<span class="quote">variable variables</span>”</span> of <span class="productname">Perl</span>
  1946. or <span class="productname"><acronym class="acronym">PHP</acronym></span> fame,
  1947. respectively. Unfortunately (or fortunately?), there is no way to do
  1948. anything useful with these constructs. On the other hand,
  1949. <code class="literal">\set bar :foo</code> is a perfectly valid way to copy a
  1950. variable.
  1951. </p></div><p>
  1952. A number of these variables are treated specially
  1953. by <span class="application">psql</span>. They represent certain option
  1954. settings that can be changed at run time by altering the value of
  1955. the variable, or in some cases represent changeable state of
  1956. <span class="application">psql</span>.
  1957. By convention, all specially treated variables' names
  1958. consist of all upper-case ASCII letters (and possibly digits and
  1959. underscores). To ensure maximum compatibility in the future, avoid
  1960. using such variable names for your own purposes.
  1961. </p><p>
  1962. Variables that control <span class="application">psql</span>'s behavior
  1963. generally cannot be unset or set to invalid values. An <code class="literal">\unset</code>
  1964. command is allowed but is interpreted as setting the variable to its
  1965. default value. A <code class="literal">\set</code> command without a second argument is
  1966. interpreted as setting the variable to <code class="literal">on</code>, for control
  1967. variables that accept that value, and is rejected for others. Also,
  1968. control variables that accept the values <code class="literal">on</code>
  1969. and <code class="literal">off</code> will also accept other common spellings of Boolean
  1970. values, such as <code class="literal">true</code> and <code class="literal">false</code>.
  1971. </p><p>
  1972. The specially treated variables are:
  1973. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">
  1974. <code class="varname">AUTOCOMMIT</code>
  1975. <a id="id-1.9.4.18.8.5.2.9.1.1.2" class="indexterm"></a>
  1976. </span></dt><dd><p>
  1977. When <code class="literal">on</code> (the default), each SQL command is automatically
  1978. committed upon successful completion. To postpone commit in this
  1979. mode, you must enter a <code class="command">BEGIN</code> or <code class="command">START
  1980. TRANSACTION</code> SQL command. When <code class="literal">off</code> or unset, SQL
  1981. commands are not committed until you explicitly issue
  1982. <code class="command">COMMIT</code> or <code class="command">END</code>. The autocommit-off
  1983. mode works by issuing an implicit <code class="command">BEGIN</code> for you, just
  1984. before any command that is not already in a transaction block and
  1985. is not itself a <code class="command">BEGIN</code> or other transaction-control
  1986. command, nor a command that cannot be executed inside a transaction
  1987. block (such as <code class="command">VACUUM</code>).
  1988. </p><div class="note"><h3 class="title">Note</h3><p>
  1989. In autocommit-off mode, you must explicitly abandon any failed
  1990. transaction by entering <code class="command">ABORT</code> or <code class="command">ROLLBACK</code>.
  1991. Also keep in mind that if you exit the session
  1992. without committing, your work will be lost.
  1993. </p></div><div class="note"><h3 class="title">Note</h3><p>
  1994. The autocommit-on mode is <span class="productname">PostgreSQL</span>'s traditional
  1995. behavior, but autocommit-off is closer to the SQL spec. If you
  1996. prefer autocommit-off, you might wish to set it in the system-wide
  1997. <code class="filename">psqlrc</code> file or your
  1998. <code class="filename">~/.psqlrc</code> file.
  1999. </p></div></dd><dt><span class="term"><code class="varname">COMP_KEYWORD_CASE</code></span></dt><dd><p>
  2000. Determines which letter case to use when completing an SQL key word.
  2001. If set to <code class="literal">lower</code> or <code class="literal">upper</code>, the
  2002. completed word will be in lower or upper case, respectively. If set
  2003. to <code class="literal">preserve-lower</code>
  2004. or <code class="literal">preserve-upper</code> (the default), the completed word
  2005. will be in the case of the word already entered, but words being
  2006. completed without anything entered will be in lower or upper case,
  2007. respectively.
  2008. </p></dd><dt><span class="term"><code class="varname">DBNAME</code></span></dt><dd><p>
  2009. The name of the database you are currently connected to. This is
  2010. set every time you connect to a database (including program
  2011. start-up), but can be changed or unset.
  2012. </p></dd><dt><span class="term"><code class="varname">ECHO</code></span></dt><dd><p>
  2013. If set to <code class="literal">all</code>, all nonempty input lines are printed
  2014. to standard output as they are read. (This does not apply to lines
  2015. read interactively.) To select this behavior on program
  2016. start-up, use the switch <code class="option">-a</code>. If set to
  2017. <code class="literal">queries</code>,
  2018. <span class="application">psql</span> prints each query to standard output
  2019. as it is sent to the server. The switch to select this behavior is
  2020. <code class="option">-e</code>. If set to <code class="literal">errors</code>, then only
  2021. failed queries are displayed on standard error output. The switch
  2022. for this behavior is <code class="option">-b</code>. If set to
  2023. <code class="literal">none</code> (the default), then no queries are displayed.
  2024. </p></dd><dt><span class="term"><code class="varname">ECHO_HIDDEN</code></span></dt><dd><p>
  2025. When this variable is set to <code class="literal">on</code> and a backslash command
  2026. queries the database, the query is first shown.
  2027. This feature helps you to study
  2028. <span class="productname">PostgreSQL</span> internals and provide
  2029. similar functionality in your own programs. (To select this behavior
  2030. on program start-up, use the switch <code class="option">-E</code>.) If you set
  2031. this variable to the value <code class="literal">noexec</code>, the queries are
  2032. just shown but are not actually sent to the server and executed.
  2033. The default value is <code class="literal">off</code>.
  2034. </p></dd><dt><span class="term"><code class="varname">ENCODING</code></span></dt><dd><p>
  2035. The current client character set encoding.
  2036. This is set every time you connect to a database (including
  2037. program start-up), and when you change the encoding
  2038. with <code class="literal">\encoding</code>, but it can be changed or unset.
  2039. </p></dd><dt><span class="term"><code class="varname">ERROR</code></span></dt><dd><p>
  2040. <code class="literal">true</code> if the last SQL query failed, <code class="literal">false</code> if
  2041. it succeeded. See also <code class="varname">SQLSTATE</code>.
  2042. </p></dd><dt><span class="term"><code class="varname">FETCH_COUNT</code></span></dt><dd><p>
  2043. If this variable is set to an integer value greater than zero,
  2044. the results of <code class="command">SELECT</code> queries are fetched
  2045. and displayed in groups of that many rows, rather than the
  2046. default behavior of collecting the entire result set before
  2047. display. Therefore only a
  2048. limited amount of memory is used, regardless of the size of
  2049. the result set. Settings of 100 to 1000 are commonly used
  2050. when enabling this feature.
  2051. Keep in mind that when using this feature, a query might
  2052. fail after having already displayed some rows.
  2053. </p><div class="tip"><h3 class="title">Tip</h3><p>
  2054. Although you can use any output format with this feature,
  2055. the default <code class="literal">aligned</code> format tends to look bad
  2056. because each group of <code class="varname">FETCH_COUNT</code> rows
  2057. will be formatted separately, leading to varying column
  2058. widths across the row groups. The other output formats work better.
  2059. </p></div></dd><dt><span class="term"><code class="varname">HIDE_TABLEAM</code></span></dt><dd><p>
  2060. If this variable is set to <code class="literal">true</code>, a table's access
  2061. method details are not displayed. This is mainly useful for
  2062. regression tests.
  2063. </p></dd><dt><span class="term"><code class="varname">HISTCONTROL</code></span></dt><dd><p>
  2064. If this variable is set to <code class="literal">ignorespace</code>,
  2065. lines which begin with a space are not entered into the history
  2066. list. If set to a value of <code class="literal">ignoredups</code>, lines
  2067. matching the previous history line are not entered. A value of
  2068. <code class="literal">ignoreboth</code> combines the two options. If
  2069. set to <code class="literal">none</code> (the default), all lines
  2070. read in interactive mode are saved on the history list.
  2071. </p><div class="note"><h3 class="title">Note</h3><p>
  2072. This feature was shamelessly plagiarized from
  2073. <span class="application">Bash</span>.
  2074. </p></div></dd><dt><span class="term"><code class="varname">HISTFILE</code></span></dt><dd><p>
  2075. The file name that will be used to store the history list. If unset,
  2076. the file name is taken from the <code class="envar">PSQL_HISTORY</code>
  2077. environment variable. If that is not set either, the default
  2078. is <code class="filename">~/.psql_history</code>,
  2079. or <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
  2080. For example, putting:
  2081. </p><pre class="programlisting">
  2082. \set HISTFILE ~/.psql_history- :DBNAME
  2083. </pre><p>
  2084. in <code class="filename">~/.psqlrc</code> will cause
  2085. <span class="application">psql</span> to maintain a separate history for
  2086. each database.
  2087. </p><div class="note"><h3 class="title">Note</h3><p>
  2088. This feature was shamelessly plagiarized from
  2089. <span class="application">Bash</span>.
  2090. </p></div></dd><dt><span class="term"><code class="varname">HISTSIZE</code></span></dt><dd><p>
  2091. The maximum number of commands to store in the command history
  2092. (default 500). If set to a negative value, no limit is applied.
  2093. </p><div class="note"><h3 class="title">Note</h3><p>
  2094. This feature was shamelessly plagiarized from
  2095. <span class="application">Bash</span>.
  2096. </p></div></dd><dt><span class="term"><code class="varname">HOST</code></span></dt><dd><p>
  2097. The database server host you are currently connected to. This is
  2098. set every time you connect to a database (including program
  2099. start-up), but can be changed or unset.
  2100. </p></dd><dt><span class="term"><code class="varname">IGNOREEOF</code></span></dt><dd><p>
  2101. If set to 1 or less, sending an <acronym class="acronym">EOF</acronym> character (usually
  2102. <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>D</strong></span>)
  2103. to an interactive session of <span class="application">psql</span>
  2104. will terminate the application. If set to a larger numeric value,
  2105. that many consecutive <acronym class="acronym">EOF</acronym> characters must be typed to
  2106. make an interactive session terminate. If the variable is set to a
  2107. non-numeric value, it is interpreted as 10. The default is 0.
  2108. </p><div class="note"><h3 class="title">Note</h3><p>
  2109. This feature was shamelessly plagiarized from
  2110. <span class="application">Bash</span>.
  2111. </p></div></dd><dt><span class="term"><code class="varname">LASTOID</code></span></dt><dd><p>
  2112. The value of the last affected OID, as returned from an
  2113. <code class="command">INSERT</code> or <code class="command">\lo_import</code>
  2114. command. This variable is only guaranteed to be valid until
  2115. after the result of the next <acronym class="acronym">SQL</acronym> command has
  2116. been displayed.
  2117. <span class="productname">PostgreSQL</span> servers since version 12 do not
  2118. support OID system columns anymore, thus LASTOID will always be 0
  2119. following <code class="command">INSERT</code> when targeting such servers.
  2120. </p></dd><dt><span class="term"><code class="varname">LAST_ERROR_MESSAGE</code><br /></span><span class="term"><code class="varname">LAST_ERROR_SQLSTATE</code></span></dt><dd><p>
  2121. The primary error message and associated SQLSTATE code for the most
  2122. recent failed query in the current <span class="application">psql</span> session, or
  2123. an empty string and <code class="literal">00000</code> if no error has occurred in
  2124. the current session.
  2125. </p></dd><dt><span class="term">
  2126. <code class="varname">ON_ERROR_ROLLBACK</code>
  2127. <a id="id-1.9.4.18.8.5.2.9.17.1.2" class="indexterm"></a>
  2128. </span></dt><dd><p>
  2129. When set to <code class="literal">on</code>, if a statement in a transaction block
  2130. generates an error, the error is ignored and the transaction
  2131. continues. When set to <code class="literal">interactive</code>, such errors are only
  2132. ignored in interactive sessions, and not when reading script
  2133. files. When set to <code class="literal">off</code> (the default), a statement in a
  2134. transaction block that generates an error aborts the entire
  2135. transaction. The error rollback mode works by issuing an
  2136. implicit <code class="command">SAVEPOINT</code> for you, just before each command
  2137. that is in a transaction block, and then rolling back to the
  2138. savepoint if the command fails.
  2139. </p></dd><dt><span class="term"><code class="varname">ON_ERROR_STOP</code></span></dt><dd><p>
  2140. By default, command processing continues after an error. When this
  2141. variable is set to <code class="literal">on</code>, processing will instead stop
  2142. immediately. In interactive mode,
  2143. <span class="application">psql</span> will return to the command prompt;
  2144. otherwise, <span class="application">psql</span> will exit, returning
  2145. error code 3 to distinguish this case from fatal error
  2146. conditions, which are reported using error code 1. In either case,
  2147. any currently running scripts (the top-level script, if any, and any
  2148. other scripts which it may have in invoked) will be terminated
  2149. immediately. If the top-level command string contained multiple SQL
  2150. commands, processing will stop with the current command.
  2151. </p></dd><dt><span class="term"><code class="varname">PORT</code></span></dt><dd><p>
  2152. The database server port to which you are currently connected.
  2153. This is set every time you connect to a database (including
  2154. program start-up), but can be changed or unset.
  2155. </p></dd><dt><span class="term"><code class="varname">PROMPT1</code><br /></span><span class="term"><code class="varname">PROMPT2</code><br /></span><span class="term"><code class="varname">PROMPT3</code></span></dt><dd><p>
  2156. These specify what the prompts <span class="application">psql</span>
  2157. issues should look like. See <a class="xref" href="app-psql.html#APP-PSQL-PROMPTING" title="Prompting">Prompting</a> below.
  2158. </p></dd><dt><span class="term"><code class="varname">QUIET</code></span></dt><dd><p>
  2159. Setting this variable to <code class="literal">on</code> is equivalent to the command
  2160. line option <code class="option">-q</code>. It is probably not too useful in
  2161. interactive mode.
  2162. </p></dd><dt><span class="term"><code class="varname">ROW_COUNT</code></span></dt><dd><p>
  2163. The number of rows returned or affected by the last SQL query, or 0
  2164. if the query failed or did not report a row count.
  2165. </p></dd><dt><span class="term"><code class="varname">SERVER_VERSION_NAME</code><br /></span><span class="term"><code class="varname">SERVER_VERSION_NUM</code></span></dt><dd><p>
  2166. The server's version number as a string, for
  2167. example <code class="literal">9.6.2</code>, <code class="literal">10.1</code> or <code class="literal">11beta1</code>,
  2168. and in numeric form, for
  2169. example <code class="literal">90602</code> or <code class="literal">100001</code>.
  2170. These are set every time you connect to a database
  2171. (including program start-up), but can be changed or unset.
  2172. </p></dd><dt><span class="term"><code class="varname">SHOW_CONTEXT</code></span></dt><dd><p>
  2173. This variable can be set to the
  2174. values <code class="literal">never</code>, <code class="literal">errors</code>, or <code class="literal">always</code>
  2175. to control whether <code class="literal">CONTEXT</code> fields are displayed in
  2176. messages from the server. The default is <code class="literal">errors</code> (meaning
  2177. that context will be shown in error messages, but not in notice or
  2178. warning messages). This setting has no effect
  2179. when <code class="varname">VERBOSITY</code> is set to <code class="literal">terse</code>
  2180. or <code class="literal">sqlstate</code>.
  2181. (See also <code class="command">\errverbose</code>, for use when you want a verbose
  2182. version of the error you just got.)
  2183. </p></dd><dt><span class="term"><code class="varname">SINGLELINE</code></span></dt><dd><p>
  2184. Setting this variable to <code class="literal">on</code> is equivalent to the command
  2185. line option <code class="option">-S</code>.
  2186. </p></dd><dt><span class="term"><code class="varname">SINGLESTEP</code></span></dt><dd><p>
  2187. Setting this variable to <code class="literal">on</code> is equivalent to the command
  2188. line option <code class="option">-s</code>.
  2189. </p></dd><dt><span class="term"><code class="varname">SQLSTATE</code></span></dt><dd><p>
  2190. The error code (see <a class="xref" href="errcodes-appendix.html" title="Appendix A. PostgreSQL Error Codes">Appendix A</a>) associated
  2191. with the last SQL query's failure, or <code class="literal">00000</code> if it
  2192. succeeded.
  2193. </p></dd><dt><span class="term"><code class="varname">USER</code></span></dt><dd><p>
  2194. The database user you are currently connected as. This is set
  2195. every time you connect to a database (including program
  2196. start-up), but can be changed or unset.
  2197. </p></dd><dt><span class="term"><code class="varname">VERBOSITY</code></span></dt><dd><p>
  2198. This variable can be set to the values <code class="literal">default</code>,
  2199. <code class="literal">verbose</code>, <code class="literal">terse</code>,
  2200. or <code class="literal">sqlstate</code> to control the verbosity of error
  2201. reports.
  2202. (See also <code class="command">\errverbose</code>, for use when you want a verbose
  2203. version of the error you just got.)
  2204. </p></dd><dt><span class="term"><code class="varname">VERSION</code><br /></span><span class="term"><code class="varname">VERSION_NAME</code><br /></span><span class="term"><code class="varname">VERSION_NUM</code></span></dt><dd><p>
  2205. These variables are set at program start-up to reflect
  2206. <span class="application">psql</span>'s version, respectively as a verbose string,
  2207. a short string (e.g., <code class="literal">9.6.2</code>, <code class="literal">10.1</code>,
  2208. or <code class="literal">11beta1</code>), and a number (e.g., <code class="literal">90602</code>
  2209. or <code class="literal">100001</code>). They can be changed or unset.
  2210. </p></dd></dl></div></div><div class="refsect3" id="APP-PSQL-INTERPOLATION"><h4><acronym class="acronym">SQL</acronym> Interpolation</h4><p>
  2211. A key feature of <span class="application">psql</span>
  2212. variables is that you can substitute (<span class="quote">“<span class="quote">interpolate</span>”</span>)
  2213. them into regular <acronym class="acronym">SQL</acronym> statements, as well as the
  2214. arguments of meta-commands. Furthermore,
  2215. <span class="application">psql</span> provides facilities for
  2216. ensuring that variable values used as SQL literals and identifiers are
  2217. properly quoted. The syntax for interpolating a value without
  2218. any quoting is to prepend the variable name with a colon
  2219. (<code class="literal">:</code>). For example,
  2220. </p><pre class="programlisting">
  2221. testdb=&gt; <strong class="userinput"><code>\set foo 'my_table'</code></strong>
  2222. testdb=&gt; <strong class="userinput"><code>SELECT * FROM :foo;</code></strong>
  2223. </pre><p>
  2224. would query the table <code class="literal">my_table</code>. Note that this
  2225. may be unsafe: the value of the variable is copied literally, so it can
  2226. contain unbalanced quotes, or even backslash commands. You must make sure
  2227. that it makes sense where you put it.
  2228. </p><p>
  2229. When a value is to be used as an SQL literal or identifier, it is
  2230. safest to arrange for it to be quoted. To quote the value of
  2231. a variable as an SQL literal, write a colon followed by the variable
  2232. name in single quotes. To quote the value as an SQL identifier, write
  2233. a colon followed by the variable name in double quotes.
  2234. These constructs deal correctly with quotes and other special
  2235. characters embedded within the variable value.
  2236. The previous example would be more safely written this way:
  2237. </p><pre class="programlisting">
  2238. testdb=&gt; <strong class="userinput"><code>\set foo 'my_table'</code></strong>
  2239. testdb=&gt; <strong class="userinput"><code>SELECT * FROM :"foo";</code></strong>
  2240. </pre><p>
  2241. </p><p>
  2242. Variable interpolation will not be performed within quoted
  2243. <acronym class="acronym">SQL</acronym> literals and identifiers. Therefore, a
  2244. construction such as <code class="literal">':foo'</code> doesn't work to produce a quoted
  2245. literal from a variable's value (and it would be unsafe if it did work,
  2246. since it wouldn't correctly handle quotes embedded in the value).
  2247. </p><p>
  2248. One example use of this mechanism is to
  2249. copy the contents of a file into a table column.
  2250. First load the file into a variable and then interpolate the variable's
  2251. value as a quoted string:
  2252. </p><pre class="programlisting">
  2253. testdb=&gt; <strong class="userinput"><code>\set content `cat my_file.txt`</code></strong>
  2254. testdb=&gt; <strong class="userinput"><code>INSERT INTO my_table VALUES (:'content');</code></strong>
  2255. </pre><p>
  2256. (Note that this still won't work if <code class="filename">my_file.txt</code> contains NUL bytes.
  2257. <span class="application">psql</span> does not support embedded NUL bytes in variable values.)
  2258. </p><p>
  2259. Since colons can legally appear in SQL commands, an apparent attempt
  2260. at interpolation (that is, <code class="literal">:name</code>,
  2261. <code class="literal">:'name'</code>, or <code class="literal">:"name"</code>) is not
  2262. replaced unless the named variable is currently set. In any case, you
  2263. can escape a colon with a backslash to protect it from substitution.
  2264. </p><p>
  2265. The <code class="literal">:{?<em class="replaceable"><code>name</code></em>}</code> special syntax returns TRUE
  2266. or FALSE depending on whether the variable exists or not, and is thus
  2267. always substituted, unless the colon is backslash-escaped.
  2268. </p><p>
  2269. The colon syntax for variables is standard <acronym class="acronym">SQL</acronym> for
  2270. embedded query languages, such as <span class="application">ECPG</span>.
  2271. The colon syntaxes for array slices and type casts are
  2272. <span class="productname">PostgreSQL</span> extensions, which can sometimes
  2273. conflict with the standard usage. The colon-quote syntax for escaping a
  2274. variable's value as an SQL literal or identifier is a
  2275. <span class="application">psql</span> extension.
  2276. </p></div><div class="refsect3" id="APP-PSQL-PROMPTING"><h4>Prompting</h4><p>
  2277. The prompts <span class="application">psql</span> issues can be customized
  2278. to your preference. The three variables <code class="varname">PROMPT1</code>,
  2279. <code class="varname">PROMPT2</code>, and <code class="varname">PROMPT3</code> contain strings
  2280. and special escape sequences that describe the appearance of the
  2281. prompt. Prompt 1 is the normal prompt that is issued when
  2282. <span class="application">psql</span> requests a new command. Prompt 2 is
  2283. issued when more input is expected during command entry, for example
  2284. because the command was not terminated with a semicolon or a quote
  2285. was not closed.
  2286. Prompt 3 is issued when you are running an <acronym class="acronym">SQL</acronym>
  2287. <code class="command">COPY FROM STDIN</code> command and you need to type in
  2288. a row value on the terminal.
  2289. </p><p>
  2290. The value of the selected prompt variable is printed literally,
  2291. except where a percent sign (<code class="literal">%</code>) is encountered.
  2292. Depending on the next character, certain other text is substituted
  2293. instead. Defined substitutions are:
  2294. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">%M</code></span></dt><dd><p>
  2295. The full host name (with domain name) of the database server,
  2296. or <code class="literal">[local]</code> if the connection is over a Unix
  2297. domain socket, or
  2298. <code class="literal">[local:<em class="replaceable"><code>/dir/name</code></em>]</code>,
  2299. if the Unix domain socket is not at the compiled in default
  2300. location.
  2301. </p></dd><dt><span class="term"><code class="literal">%m</code></span></dt><dd><p>
  2302. The host name of the database server, truncated at the
  2303. first dot, or <code class="literal">[local]</code> if the connection is
  2304. over a Unix domain socket.
  2305. </p></dd><dt><span class="term"><code class="literal">%&gt;</code></span></dt><dd><p>The port number at which the database server is listening.</p></dd><dt><span class="term"><code class="literal">%n</code></span></dt><dd><p>
  2306. The database session user name. (The expansion of this
  2307. value might change during a database session as the result
  2308. of the command <code class="command">SET SESSION
  2309. AUTHORIZATION</code>.)
  2310. </p></dd><dt><span class="term"><code class="literal">%/</code></span></dt><dd><p>The name of the current database.</p></dd><dt><span class="term"><code class="literal">%~</code></span></dt><dd><p>Like <code class="literal">%/</code>, but the output is <code class="literal">~</code>
  2311. (tilde) if the database is your default database.</p></dd><dt><span class="term"><code class="literal">%#</code></span></dt><dd><p>
  2312. If the session user is a database superuser, then a
  2313. <code class="literal">#</code>, otherwise a <code class="literal">&gt;</code>.
  2314. (The expansion of this value might change during a database
  2315. session as the result of the command <code class="command">SET SESSION
  2316. AUTHORIZATION</code>.)
  2317. </p></dd><dt><span class="term"><code class="literal">%p</code></span></dt><dd><p>The process ID of the backend currently connected to.</p></dd><dt><span class="term"><code class="literal">%R</code></span></dt><dd><p>
  2318. In prompt 1 normally <code class="literal">=</code>,
  2319. but <code class="literal">@</code> if the session is in an inactive branch of a
  2320. conditional block, or <code class="literal">^</code> if in single-line mode,
  2321. or <code class="literal">!</code> if the session is disconnected from the
  2322. database (which can happen if <code class="command">\connect</code> fails).
  2323. In prompt 2 <code class="literal">%R</code> is replaced by a character that
  2324. depends on why <span class="application">psql</span> expects more input:
  2325. <code class="literal">-</code> if the command simply wasn't terminated yet,
  2326. but <code class="literal">*</code> if there is an unfinished
  2327. <code class="literal">/* ... */</code> comment,
  2328. a single quote if there is an unfinished quoted string,
  2329. a double quote if there is an unfinished quoted identifier,
  2330. a dollar sign if there is an unfinished dollar-quoted string,
  2331. or <code class="literal">(</code> if there is an unmatched left parenthesis.
  2332. In prompt 3 <code class="literal">%R</code> doesn't produce anything.
  2333. </p></dd><dt><span class="term"><code class="literal">%x</code></span></dt><dd><p>
  2334. Transaction status: an empty string when not in a transaction
  2335. block, or <code class="literal">*</code> when in a transaction block, or
  2336. <code class="literal">!</code> when in a failed transaction block, or <code class="literal">?</code>
  2337. when the transaction state is indeterminate (for example, because
  2338. there is no connection).
  2339. </p></dd><dt><span class="term"><code class="literal">%l</code></span></dt><dd><p>
  2340. The line number inside the current statement, starting from <code class="literal">1</code>.
  2341. </p></dd><dt><span class="term"><code class="literal">%</code><em class="replaceable"><code>digits</code></em></span></dt><dd><p>
  2342. The character with the indicated octal code is substituted.
  2343. </p></dd><dt><span class="term"><code class="literal">%:</code><em class="replaceable"><code>name</code></em><code class="literal">:</code></span></dt><dd><p>
  2344. The value of the <span class="application">psql</span> variable
  2345. <em class="replaceable"><code>name</code></em>. See the
  2346. section <a class="xref" href="app-psql.html#APP-PSQL-VARIABLES" title="Variables">Variables</a> for details.
  2347. </p></dd><dt><span class="term"><code class="literal">%`</code><em class="replaceable"><code>command</code></em><code class="literal">`</code></span></dt><dd><p>
  2348. The output of <em class="replaceable"><code>command</code></em>, similar to ordinary
  2349. <span class="quote">“<span class="quote">back-tick</span>”</span> substitution.
  2350. </p></dd><dt><span class="term"><code class="literal">%[</code> ... <code class="literal">%]</code></span></dt><dd><p>
  2351. Prompts can contain terminal control characters which, for
  2352. example, change the color, background, or style of the prompt
  2353. text, or change the title of the terminal window. In order for
  2354. the line editing features of <span class="application">Readline</span> to work properly, these
  2355. non-printing control characters must be designated as invisible
  2356. by surrounding them with <code class="literal">%[</code> and
  2357. <code class="literal">%]</code>. Multiple pairs of these can occur within
  2358. the prompt. For example:
  2359. </p><pre class="programlisting">
  2360. testdb=&gt; \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
  2361. </pre><p>
  2362. results in a boldfaced (<code class="literal">1;</code>) yellow-on-black
  2363. (<code class="literal">33;40</code>) prompt on VT100-compatible, color-capable
  2364. terminals.
  2365. </p></dd></dl></div><p>
  2366. To insert a percent sign into your prompt, write
  2367. <code class="literal">%%</code>. The default prompts are
  2368. <code class="literal">'%/%R%# '</code> for prompts 1 and 2, and
  2369. <code class="literal">'&gt;&gt; '</code> for prompt 3.
  2370. </p><div class="note"><h3 class="title">Note</h3><p>
  2371. This feature was shamelessly plagiarized from
  2372. <span class="application">tcsh</span>.
  2373. </p></div></div><div class="refsect3" id="id-1.9.4.18.8.5.5"><h4>Command-Line Editing</h4><p>
  2374. <span class="application">psql</span> supports the <span class="application">Readline</span>
  2375. library for convenient line editing and retrieval. The command
  2376. history is automatically saved when <span class="application">psql</span>
  2377. exits and is reloaded when
  2378. <span class="application">psql</span> starts up. Tab-completion is also
  2379. supported, although the completion logic makes no claim to be an
  2380. <acronym class="acronym">SQL</acronym> parser. The queries generated by tab-completion
  2381. can also interfere with other SQL commands, e.g. <code class="literal">SET
  2382. TRANSACTION ISOLATION LEVEL</code>.
  2383. If for some reason you do not like the tab completion, you
  2384. can turn it off by putting this in a file named
  2385. <code class="filename">.inputrc</code> in your home directory:
  2386. </p><pre class="programlisting">
  2387. $if psql
  2388. set disable-completion on
  2389. $endif
  2390. </pre><p>
  2391. (This is not a <span class="application">psql</span> but a
  2392. <span class="application">Readline</span> feature. Read its documentation
  2393. for further details.)
  2394. </p></div></div></div><div class="refsect1" id="APP-PSQL-ENVIRONMENT"><h2>Environment</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="envar">COLUMNS</code></span></dt><dd><p>
  2395. If <code class="literal">\pset columns</code> is zero, controls the
  2396. width for the <code class="literal">wrapped</code> format and width for determining
  2397. if wide output requires the pager or should be switched to the
  2398. vertical format in expanded auto mode.
  2399. </p></dd><dt><span class="term"><code class="envar">PGDATABASE</code><br /></span><span class="term"><code class="envar">PGHOST</code><br /></span><span class="term"><code class="envar">PGPORT</code><br /></span><span class="term"><code class="envar">PGUSER</code></span></dt><dd><p>
  2400. Default connection parameters (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
  2401. </p></dd><dt><span class="term"><code class="envar">PG_COLOR</code></span></dt><dd><p>
  2402. Specifies whether to use color in diagnostic messages. Possible values
  2403. are <code class="literal">always</code>, <code class="literal">auto</code> and
  2404. <code class="literal">never</code>.
  2405. </p></dd><dt><span class="term"><code class="envar">PSQL_EDITOR</code><br /></span><span class="term"><code class="envar">EDITOR</code><br /></span><span class="term"><code class="envar">VISUAL</code></span></dt><dd><p>
  2406. Editor used by the <code class="command">\e</code>, <code class="command">\ef</code>,
  2407. and <code class="command">\ev</code> commands.
  2408. These variables are examined in the order listed;
  2409. the first that is set is used.
  2410. If none of them is set, the default is to use <code class="filename">vi</code>
  2411. on Unix systems or <code class="filename">notepad.exe</code> on Windows systems.
  2412. </p></dd><dt><span class="term"><code class="envar">PSQL_EDITOR_LINENUMBER_ARG</code></span></dt><dd><p>
  2413. When <code class="command">\e</code>, <code class="command">\ef</code>, or
  2414. <code class="command">\ev</code> is used
  2415. with a line number argument, this variable specifies the
  2416. command-line argument used to pass the starting line number to
  2417. the user's editor. For editors such as <span class="productname">Emacs</span> or
  2418. <span class="productname">vi</span>, this is a plus sign. Include a trailing
  2419. space in the value of the variable if there needs to be space
  2420. between the option name and the line number. Examples:
  2421. </p><pre class="programlisting">
  2422. PSQL_EDITOR_LINENUMBER_ARG='+'
  2423. PSQL_EDITOR_LINENUMBER_ARG='--line '
  2424. </pre><p>
  2425. </p><p>
  2426. The default is <code class="literal">+</code> on Unix systems
  2427. (corresponding to the default editor <code class="filename">vi</code>,
  2428. and useful for many other common editors); but there is no
  2429. default on Windows systems.
  2430. </p></dd><dt><span class="term"><code class="envar">PSQL_HISTORY</code></span></dt><dd><p>
  2431. Alternative location for the command history file. Tilde (<code class="literal">~</code>) expansion is performed.
  2432. </p></dd><dt><span class="term"><code class="envar">PSQL_PAGER</code><br /></span><span class="term"><code class="envar">PAGER</code></span></dt><dd><p>
  2433. If a query's results do not fit on the screen, they are piped
  2434. through this command. Typical values are <code class="literal">more</code>
  2435. or <code class="literal">less</code>.
  2436. Use of the pager can be disabled by setting <code class="envar">PSQL_PAGER</code>
  2437. or <code class="envar">PAGER</code> to an empty string, or by adjusting the
  2438. pager-related options of the <code class="command">\pset</code> command.
  2439. These variables are examined in the order listed;
  2440. the first that is set is used.
  2441. If none of them is set, the default is to use <code class="literal">more</code> on most
  2442. platforms, but <code class="literal">less</code> on Cygwin.
  2443. </p></dd><dt><span class="term"><code class="envar">PSQLRC</code></span></dt><dd><p>
  2444. Alternative location of the user's <code class="filename">.psqlrc</code> file. Tilde (<code class="literal">~</code>) expansion is performed.
  2445. </p></dd><dt><span class="term"><code class="envar">SHELL</code></span></dt><dd><p>
  2446. Command executed by the <code class="command">\!</code> command.
  2447. </p></dd><dt><span class="term"><code class="envar">TMPDIR</code></span></dt><dd><p>
  2448. Directory for storing temporary files. The default is
  2449. <code class="filename">/tmp</code>.
  2450. </p></dd></dl></div><p>
  2451. This utility, like most other <span class="productname">PostgreSQL</span> utilities,
  2452. also uses the environment variables supported by <span class="application">libpq</span>
  2453. (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
  2454. </p></div><div class="refsect1" id="id-1.9.4.18.10"><h2>Files</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="filename">psqlrc</code> and <code class="filename">~/.psqlrc</code></span></dt><dd><p>
  2455. Unless it is passed an <code class="option">-X</code> option,
  2456. <span class="application">psql</span> attempts to read and execute commands
  2457. from the system-wide startup file (<code class="filename">psqlrc</code>) and then
  2458. the user's personal startup file (<code class="filename">~/.psqlrc</code>), after
  2459. connecting to the database but before accepting normal commands.
  2460. These files can be used to set up the client and/or the server to taste,
  2461. typically with <code class="command">\set</code> and <code class="command">SET</code>
  2462. commands.
  2463. </p><p>
  2464. The system-wide startup file is named <code class="filename">psqlrc</code> and is
  2465. sought in the installation's <span class="quote">“<span class="quote">system configuration</span>”</span> directory,
  2466. which is most reliably identified by running <code class="literal">pg_config
  2467. --sysconfdir</code>. By default this directory will be <code class="filename">../etc/</code>
  2468. relative to the directory containing
  2469. the <span class="productname">PostgreSQL</span> executables. The name of this
  2470. directory can be set explicitly via the <code class="envar">PGSYSCONFDIR</code>
  2471. environment variable.
  2472. </p><p>
  2473. The user's personal startup file is named <code class="filename">.psqlrc</code>
  2474. and is sought in the invoking user's home directory. On Windows, which
  2475. lacks such a concept, the personal startup file is named
  2476. <code class="filename">%APPDATA%\postgresql\psqlrc.conf</code>.
  2477. The location of the user's startup file can be set explicitly via
  2478. the <code class="envar">PSQLRC</code> environment variable.
  2479. </p><p>
  2480. Both the system-wide startup file and the user's personal startup file
  2481. can be made <span class="application">psql</span>-version-specific
  2482. by appending a dash and the <span class="productname">PostgreSQL</span>
  2483. major or minor release number to the file name,
  2484. for example <code class="filename">~/.psqlrc-9.2</code> or
  2485. <code class="filename">~/.psqlrc-9.2.5</code>. The most specific
  2486. version-matching file will be read in preference to a
  2487. non-version-specific file.
  2488. </p></dd><dt><span class="term"><code class="filename">.psql_history</code></span></dt><dd><p>
  2489. The command-line history is stored in the file
  2490. <code class="filename">~/.psql_history</code>, or
  2491. <code class="filename">%APPDATA%\postgresql\psql_history</code> on Windows.
  2492. </p><p>
  2493. The location of the history file can be set explicitly via
  2494. the <code class="varname">HISTFILE</code> <span class="application">psql</span> variable or
  2495. the <code class="envar">PSQL_HISTORY</code> environment variable.
  2496. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.4.18.11"><h2>Notes</h2><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><span class="application">psql</span> works best with servers of the same
  2497. or an older major version. Backslash commands are particularly likely
  2498. to fail if the server is of a newer version than <span class="application">psql</span>
  2499. itself. However, backslash commands of the <code class="literal">\d</code> family should
  2500. work with servers of versions back to 7.4, though not necessarily with
  2501. servers newer than <span class="application">psql</span> itself. The general
  2502. functionality of running SQL commands and displaying query results
  2503. should also work with servers of a newer major version, but this cannot
  2504. be guaranteed in all cases.
  2505. </p><p>
  2506. If you want to use <span class="application">psql</span> to connect to several
  2507. servers of different major versions, it is recommended that you use the
  2508. newest version of <span class="application">psql</span>. Alternatively, you
  2509. can keep around a copy of <span class="application">psql</span> from each
  2510. major version and be sure to use the version that matches the
  2511. respective server. But in practice, this additional complication should
  2512. not be necessary.
  2513. </p></li><li class="listitem"><p>
  2514. Before <span class="productname">PostgreSQL</span> 9.6,
  2515. the <code class="option">-c</code> option implied <code class="option">-X</code>
  2516. (<code class="option">--no-psqlrc</code>); this is no longer the case.
  2517. </p></li><li class="listitem"><p>
  2518. Before <span class="productname">PostgreSQL</span> 8.4,
  2519. <span class="application">psql</span> allowed the
  2520. first argument of a single-letter backslash command to start
  2521. directly after the command, without intervening whitespace.
  2522. Now, some whitespace is required.
  2523. </p></li></ul></div></div><div class="refsect1" id="id-1.9.4.18.12"><h2>Notes for Windows Users</h2><p>
  2524. <span class="application">psql</span> is built as a <span class="quote">“<span class="quote">console
  2525. application</span>”</span>. Since the Windows console windows use a different
  2526. encoding than the rest of the system, you must take special care
  2527. when using 8-bit characters within <span class="application">psql</span>.
  2528. If <span class="application">psql</span> detects a problematic
  2529. console code page, it will warn you at startup. To change the
  2530. console code page, two things are necessary:
  2531. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2532. Set the code page by entering <strong class="userinput"><code>cmd.exe /c chcp
  2533. 1252</code></strong>. (1252 is a code page that is appropriate for
  2534. German; replace it with your value.) If you are using Cygwin,
  2535. you can put this command in <code class="filename">/etc/profile</code>.
  2536. </p></li><li class="listitem"><p>
  2537. Set the console font to <code class="literal">Lucida Console</code>, because the
  2538. raster font does not work with the ANSI code page.
  2539. </p></li></ul></div></div><div class="refsect1" id="APP-PSQL-EXAMPLES"><h2>Examples</h2><p>
  2540. The first example shows how to spread a command over several lines of
  2541. input. Notice the changing prompt:
  2542. </p><pre class="programlisting">
  2543. testdb=&gt; <strong class="userinput"><code>CREATE TABLE my_table (</code></strong>
  2544. testdb(&gt; <strong class="userinput"><code> first integer not null default 0,</code></strong>
  2545. testdb(&gt; <strong class="userinput"><code> second text)</code></strong>
  2546. testdb-&gt; <strong class="userinput"><code>;</code></strong>
  2547. CREATE TABLE
  2548. </pre><p>
  2549. Now look at the table definition again:
  2550. </p><pre class="programlisting">
  2551. testdb=&gt; <strong class="userinput"><code>\d my_table</code></strong>
  2552. Table "public.my_table"
  2553. Column | Type | Collation | Nullable | Default
  2554. --------+---------+-----------+----------+---------
  2555. first | integer | | not null | 0
  2556. second | text | | |
  2557. </pre><p>
  2558. Now we change the prompt to something more interesting:
  2559. </p><pre class="programlisting">
  2560. testdb=&gt; <strong class="userinput"><code>\set PROMPT1 '%n@%m %~%R%# '</code></strong>
  2561. peter@localhost testdb=&gt;
  2562. </pre><p>
  2563. Let's assume you have filled the table with data and want to take a
  2564. look at it:
  2565. </p><pre class="programlisting">
  2566. peter@localhost testdb=&gt; SELECT * FROM my_table;
  2567. first | second
  2568. -------+--------
  2569. 1 | one
  2570. 2 | two
  2571. 3 | three
  2572. 4 | four
  2573. (4 rows)
  2574. </pre><p>
  2575. You can display tables in different ways by using the
  2576. <code class="command">\pset</code> command:
  2577. </p><pre class="programlisting">
  2578. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset border 2</code></strong>
  2579. Border style is 2.
  2580. peter@localhost testdb=&gt; <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
  2581. +-------+--------+
  2582. | first | second |
  2583. +-------+--------+
  2584. | 1 | one |
  2585. | 2 | two |
  2586. | 3 | three |
  2587. | 4 | four |
  2588. +-------+--------+
  2589. (4 rows)
  2590. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset border 0</code></strong>
  2591. Border style is 0.
  2592. peter@localhost testdb=&gt; <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
  2593. first second
  2594. ----- ------
  2595. 1 one
  2596. 2 two
  2597. 3 three
  2598. 4 four
  2599. (4 rows)
  2600. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset border 1</code></strong>
  2601. Border style is 1.
  2602. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset format csv</code></strong>
  2603. Output format is csv.
  2604. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset tuples_only</code></strong>
  2605. Tuples only is on.
  2606. peter@localhost testdb=&gt; <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
  2607. one,1
  2608. two,2
  2609. three,3
  2610. four,4
  2611. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset format unaligned</code></strong>
  2612. Output format is unaligned.
  2613. peter@localhost testdb=&gt; <strong class="userinput"><code>\pset fieldsep '\t'</code></strong>
  2614. Field separator is " ".
  2615. peter@localhost testdb=&gt; <strong class="userinput"><code>SELECT second, first FROM my_table;</code></strong>
  2616. one 1
  2617. two 2
  2618. three 3
  2619. four 4
  2620. </pre><p>
  2621. Alternatively, use the short commands:
  2622. </p><pre class="programlisting">
  2623. peter@localhost testdb=&gt; <strong class="userinput"><code>\a \t \x</code></strong>
  2624. Output format is aligned.
  2625. Tuples only is off.
  2626. Expanded display is on.
  2627. peter@localhost testdb=&gt; <strong class="userinput"><code>SELECT * FROM my_table;</code></strong>
  2628. -[ RECORD 1 ]-
  2629. first | 1
  2630. second | one
  2631. -[ RECORD 2 ]-
  2632. first | 2
  2633. second | two
  2634. -[ RECORD 3 ]-
  2635. first | 3
  2636. second | three
  2637. -[ RECORD 4 ]-
  2638. first | 4
  2639. second | four
  2640. </pre><p>
  2641. When suitable, query results can be shown in a crosstab representation
  2642. with the <code class="command">\crosstabview</code> command:
  2643. </p><pre class="programlisting">
  2644. testdb=&gt; <strong class="userinput"><code>SELECT first, second, first &gt; 2 AS gt2 FROM my_table;</code></strong>
  2645. first | second | gt2
  2646. -------+--------+-----
  2647. 1 | one | f
  2648. 2 | two | f
  2649. 3 | three | t
  2650. 4 | four | t
  2651. (4 rows)
  2652. testdb=&gt; <strong class="userinput"><code>\crosstabview first second</code></strong>
  2653. first | one | two | three | four
  2654. -------+-----+-----+-------+------
  2655. 1 | f | | |
  2656. 2 | | f | |
  2657. 3 | | | t |
  2658. 4 | | | | t
  2659. (4 rows)
  2660. </pre><p>
  2661. This second example shows a multiplication table with rows sorted in reverse
  2662. numerical order and columns with an independent, ascending numerical order.
  2663. </p><pre class="programlisting">
  2664. testdb=&gt; <strong class="userinput"><code>SELECT t1.first as "A", t2.first+100 AS "B", t1.first*(t2.first+100) as "AxB",</code></strong>
  2665. testdb(&gt; <strong class="userinput"><code>row_number() over(order by t2.first) AS ord</code></strong>
  2666. testdb(&gt; <strong class="userinput"><code>FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC</code></strong>
  2667. testdb(&gt; <strong class="userinput"><code>\crosstabview "A" "B" "AxB" ord</code></strong>
  2668. A | 101 | 102 | 103 | 104
  2669. ---+-----+-----+-----+-----
  2670. 4 | 404 | 408 | 412 | 416
  2671. 3 | 303 | 306 | 309 | 312
  2672. 2 | 202 | 204 | 206 | 208
  2673. 1 | 101 | 102 | 103 | 104
  2674. (4 rows)
  2675. </pre><p>
  2676. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-pgrestore.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="app-reindexdb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">pg_restore </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">reindexdb</span></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1