Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
postgres-lambda-diff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jakob Huber
postgres-lambda-diff
Commits
27030075
Commit
27030075
authored
18 years ago
by
Tom Lane
Browse files
Options
Downloads
Patches
Plain Diff
Fix example of how to escape data in psql backslash commands.
parent
c6d3c1b8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
doc/src/sgml/ref/psql-ref.sgml
+13
-16
13 additions, 16 deletions
doc/src/sgml/ref/psql-ref.sgml
with
13 additions
and
16 deletions
doc/src/sgml/ref/psql-ref.sgml
+
13
−
16
View file @
27030075
<!--
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.16
4
2006/05/31
11:47:20 momjian
Exp $
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.16
5
2006/05/31
22:34:35 tgl
Exp $
PostgreSQL documentation
-->
...
...
@@ -2265,27 +2265,24 @@ testdb=> <userinput>SELECT * FROM :foo;</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>
One problem with this approach is that <filename>my_file.txt</filename>
might contain single quotes. These need to be escaped so that
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>
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
<command>sed</command> parses this it will replace the two
backslashes with a single one and then do the substitution. Perhaps
If you are using non-standard-conforming strings then you'll also need
to double backslashes. This is a bit tricky:
<programlisting>
testdb=> <userinput>\set content '''' `sed -e "s/'/''/g" -e 's/\\/\\\\/g' < my_file.txt` ''''</userinput>
</programlisting>
Note the use of different shell quoting conventions so that neither
the single quote marks nor the backslashes are special to the shell.
Backslashes are still special to <command>sed</command>, however, so
we need to double them. (Perhaps
at one point you thought it was great that all Unix commands use the
same escape character. And this is ignoring the fact that you might
have to escape all backslashes as well because
<acronym>SQL</acronym> text constants are also subject to certain
interpretations. In that case you might be better off preparing the
file externally.
same escape character.)
</para>
<para>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment