3

I have a database setup with a UTF-8 encoding. Trying to copy a table to csv, where the filename has a special character writes out the filename wrong to disk.

On a Windows 10 localhost PostgreSQL installation:

copy
  (select 'tønder')
to 'C:\temp\Sønderborg.csv' (FORMAT CSV, HEADER TRUE, DELIMITER ';', ENCODING 'UTF8');

Names the csv file: Sønderborg.csv and not Sønderborg.csv.

Both

SHOW CLIENT ENCODING;
SHOW SERVER_ENCODING;

returns UTF8

How can one control the csv filename encoding? The encoding inside the csv is ok writing Tønder!

UPDATE

I have run the copy command from pgAdmin, DataGrip and a psql console. DataGrip uses JDBC and will only handle UTF8. All three applications writes the csv filename in wrong encoding. The only difference is that the psql console says the client encoding is WIN1252.

2
  • What program are you using to run the copy command? Commented Mar 5, 2019 at 11:26
  • I updated the question with program running the copy command Commented Mar 5, 2019 at 14:45

1 Answer 1

5

I don't think it's possible to change this behaviour. It looks like Postgres assumes that the filename encoding matches the server_encoding (as suggested on the mailing lists here and here). The only workaround I could find was to run the command while connected to a WIN1252-encoded database, which is probably not very helpful.

If you're trying to run this on the same machine as the server itself, then instead of using the server-side COPY, you can run psql's client-side \copy, which will respect your client_encoding when interpreting the file path:

psql -c "\copy (select 'tønder') to 'C:\temp\Sønderborg.csv' (FORMAT CSV, HEADER TRUE, DELIMITER ';', ENCODING 'UTF8')"

Note that cmd.exe (and even powershell.exe) still uses legacy DOS encodings by default, so you might need to run chcp 1252 to set the console codepage before launching psql.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the clarification, the psql \copy returns the correct filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.