Import/Export Data fails for a file name containing a backslash, and creates a stray file named E
Describe the bug
Import/Export Data fails for any file whose path contains a backslash (\), and leaves behind an empty file named E in psql's working directory.
The file name is written into the \copy command with qtLiteral() (web/pgadmin/tools/import_export/templates/import_export/sql/*/cmd.sql). For a value containing a backslash, that produces an escape-string literal, E'...'. psql parses the file name of \copy itself, and its tokenizer doesn't recognise the E'...' form there. It takes the file name as just E, opens (and, for an export, creates or truncates) ./E, and sends the rest of the line to the server. The server then fails with a syntax error.
To Reproduce
- Create a file or directory whose name contains a backslash, e.g.
/tmp/a\b.csv(possible on Linux and macOS). - Right-click a table > Import/Export Data, choose Export, and select that file.
- Run the job.
- The job fails, and an empty file named
Eappears in psql's working directory.
The same happens with psql directly:
\copy (select 1) TO E'/tmp/a\\b.csv' WITH(FORMAT csv)
Expected behavior
The data is exported to (or imported from) the selected file, and no other file is created.
Error message
ERROR: syntax error at or near "'/tmp/a\\b.csv'"
Desktop (please complete the following information):
- OS: Linux, macOS
- pgAdmin version: 9.18 (also current master)
- Mode: Desktop and Server
- Package type: any
Additional context
psql's \copy file-name token has no backslash escapes, so the file name should be written as a plain '...' literal with only embedded single quotes doubled, never as E'...'. The option values later in the command (DELIMITER, QUOTE, ESCAPE, NULL, ENCODING) are parsed by the server, where E'...' is valid, so they are not affected.
Hi, I'd like to work on this. I have a fix ready and will open a PR shortly.
Approach: psql parses the file name of \copy itself (parse_slash_copy in src/bin/psql/copy.c calls strip_quotes with no escape character), so the file name should not go through qtLiteral(). I added a small helper that wraps the name in plain single quotes and only doubles an embedded single quote, and the three cmd.sql templates now use that value. The option values (DELIMITER, QUOTE, ESCAPE, NULL, ENCODING) still use qtLiteral(), since the server parses those.