Assuming a UNIX(-oid) shell.
If you can't use a here document like @wildplasser provided I suggest dollar quotes for string literals within your SQL command. Replacing the surrounding single quotes with double quotes would turn on variable expansion in the shell, which may have side effects if special characters are contained: $
, `
(backquote), and \
.
psql -U username -d mydatabase -c 'COPY products_273 TO $$/tmp/products_199.csv$$ DELIMITER $$,$$ CSV HEADER;'
With full quoting in the shell (single quotes) the string is passed as is, no special meaning for any character.
If special characters in the SQL string can be ruled out or are dealt with properly, @ferhat's simple solution will do fine.
Or you can replace each single quote ('
) within the fully quoted string with '\''
- which effectively ends the quoted string, appends a literal '
and starts a new full quote.
psql -U username -d mydatabase -c 'COPY products_273 TO '\''/tmp/products_199.csv'\'' DELIMITER '\'','\'' CSV HEADER;'
Related:
Or put the SQL command in a separate file (without surrounding quotes) and use psql -f
:
psql -U username -d mydatabase -f /path/to/myfile.sql
I assume you are aware of the difference between \copy
(the psql meta-command) and COPY
(the SQL command). \copy
reads and writes files local to the client, while COPY
does the same for files on the server. If client and server run on the same installation, you can use either. Certain differences apply, though.