patternsqlMinor
Variable substitution in psql when using \copy
Viewed 0 times
substitutionpsqlusingwhenvariablecopy
Problem
I'm using the psql command \copy and I would like to pass a variable to it from the shell (for table name) like I've done when scripting queries. I've read in the documentation for psql that:
The syntax of the command is similar to that of the SQL COPY command. Note that, because of this, special parsing rules apply to the \copy command. In particular, the variable substitution rules and backslash escapes do not apply.
This seems quite definitive, however I'm wondering if anyone knows of a workaround?
The syntax of the command is similar to that of the SQL COPY command. Note that, because of this, special parsing rules apply to the \copy command. In particular, the variable substitution rules and backslash escapes do not apply.
This seems quite definitive, however I'm wondering if anyone knows of a workaround?
Solution
You can circumvent this limitation by piping the whole command to psql:
echo "\copy tbl FROM '/path/to/myfile.pgsql'" | psql mydb
Context
StackExchange Database Administrators Q#34077, answer score: 6
Revisions (0)
No revisions yet.