title: psql - write a query and the query's output to a file
body: <p>In postgresql 9.3.1, when interactively developing a query using the <a href="http://www.postgresql.org/docs/9.3/static/app-psql.html">psql</a> command, the end result is sometimes to write the query results to a file:</p>
  
  <pre><code>boron.production=&gt; \o /tmp/output
  boron.production=&gt; select 1;
  boron.production=&gt; \o
  boron.production=&gt; \q
  $ cat /tmp/output
  ?column? 
  ----------
          1
  (1 row)
  </code></pre>
  
  <p>This works fine.  But how can I get the <em>query itself</em> to be written to the file along with the query results?</p>
  
  <p>I've tried giving psql the <code>--echo-queries</code> switch:</p>
  
  <pre><code>   -e, --echo-queries
         Copy all SQL commands sent to the server to standard output as well.
         This is equivalent to setting the variable ECHO to queries.
  </code></pre>
  
  <p>But this always echoes to stdout, not to the file I gave with the \o command.</p>
  
  <p>I've tried the <code>--echo-all</code> switch as well, but it does not appear to echo interactive input.</p>
  
  <p>Using command editing, I can repeat the query with <code>\qecho</code> in front of it.  That works, but is tedious.</p>
  
  <p>Is there any way to direct an interactive psql session to write both the query and the query output to a file?</p>