<!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.162 2006/05/26 19:51:29 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.163 2006/05/31 11:35:17 momjian Exp $
PostgreSQL documentation
-->
copy the contents of a file into a table column. First load the file into a
variable and then proceed as above.
<programlisting>
-testdb=> <userinput>\set content '\'' `cat my_file.txt` '\''</userinput>
+testdb=> <userinput>\set content '''' `cat my_file.txt` ''''</userinput>
testdb=> <userinput>INSERT INTO my_table VALUES (:content);</userinput>
</programlisting>
One possible problem with this approach is that <filename>my_file.txt</filename>
they don't cause a syntax error when the second line is processed. This
could be done with the program <command>sed</command>:
<programlisting>
-testdb=> <userinput>\set content '\'' `sed -e "s/'/\\\\\\'/g" < my_file.txt` '\''</userinput>
+testdb=> <userinput>\set content '''' `sed -e "s/'/\\\\''/g" < my_file.txt` ''''</userinput>
</programlisting>
Observe the correct number of backslashes (6)! It works
this way: After <application>psql</application> has parsed this
- line, it passes <literal>sed -e "s/'/\\\'/g" < my_file.txt</literal>
+ line, it passes <literal>sed -e "s/'/\\''/g" < my_file.txt</literal>
to the shell. The shell will do its own thing inside the double
quotes and execute <command>sed</command> with the arguments
- <literal>-e</literal> and <literal>s/'/\\'/g</literal>. When
+ <literal>-e</literal> and <literal>s/'/''/g</literal>. When
<command>sed</command> parses this it will replace the two
backslashes with a single one and then do the substitution. Perhaps
at one point you thought it was great that all Unix commands use the
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.18 2006/05/11 19:15:35 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/psqlscan.l,v 1.19 2006/05/31 11:35:17 momjian Exp $
*
*-------------------------------------------------------------------------
*/
{quote} { return LEXRES_OK; }
+ /* We don't need a state here because we are already in a string */
+{xqdouble} { emit("'", 1); }
+
"\\n" { appendPQExpBufferChar(output_buf, '\n'); }
"\\t" { appendPQExpBufferChar(output_buf, '\t'); }
"\\b" { appendPQExpBufferChar(output_buf, '\b'); }