Sign up ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question
It's customary to add a link when quoting. – Erwin Brandstetter Feb 4 '13 at 19:37
I've found the source and added the link. – Erwin Brandstetter Feb 4 '13 at 20:55

1 Answer 1

up vote 1 down vote accepted

You can circumvent this limitation by piping the whole command to psql:

echo '\copy tbl FROM ''/path/to/myfile.pgsql''' | psql mydb
share|improve this answer
Thanks, that does the job. – beacon_bonanza Feb 5 '13 at 9:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.