pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Maintaining accents with "COPY" ?
6+ messages / 3 participants
[nested] [flat]

* Maintaining accents with "COPY" ?
@ 2023-05-25 07:14 Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  2023-05-25 07:48 ` Re: Maintaining accents with "COPY" ? Erik Wienhold <ewie@ewie.name>
  2023-05-25 11:26 ` Re: Maintaining accents with "COPY" ? Peter J. Holzer <hjp-pgsql@hjp.at>
  0 siblings, 2 replies; 6+ messages in thread

From: Laura Smith @ 2023-05-25 07:14 UTC (permalink / raw)
  To: postgre <pgsql-general@lists.postgresql.org>

Hi

I'm currently doing a CSV export using COPY:

COPY (select * from foo where bar='foo') TO '/tmp/bar.csv' DELIMITER ',' CSV HEADER;


This works great apart from accents are not preserved in the output, for example é gets converted to random characters, e.g. √© or similar.


How can I preserve accents ?


Thanks !


Laura





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Maintaining accents with "COPY" ?
  2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
@ 2023-05-25 07:48 ` Erik Wienhold <ewie@ewie.name>
  2023-05-25 10:08   ` Re: Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  1 sibling, 1 reply; 6+ messages in thread

From: Erik Wienhold @ 2023-05-25 07:48 UTC (permalink / raw)
  To: Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>; postgre <pgsql-general@lists.postgresql.org>

> On 25/05/2023 09:14 CEST Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch> wrote:
>
> I'm currently doing a CSV export using COPY:
>
> COPY (select * from foo where bar='foo') TO '/tmp/bar.csv' DELIMITER ',' CSV HEADER;
>
> This works great apart from accents are not preserved in the output, for
> example é gets converted to random characters, e.g. √© or similar.
>
> How can I preserve accents ?

Looks like an encoding issue and a mismatch between database encoding and client
encoding.  You can check both with:

	SHOW server_encoding;
	SHOW client_encoding;

Then either set the client encoding or use COPY's encoding option to match the
database encoding (I assume utf8 in this example):

	SET client_encoding = 'utf8';
	COPY (...) TO /tmp/bar.csv DELIMITER ',' CSV HEADER ENCODING 'utf8';

--
Erik





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Maintaining accents with "COPY" ?
  2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  2023-05-25 07:48 ` Re: Maintaining accents with "COPY" ? Erik Wienhold <ewie@ewie.name>
@ 2023-05-25 10:08   ` Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  2023-05-25 12:37     ` Re: Maintaining accents with "COPY" ? Erik Wienhold <ewie@ewie.name>
  0 siblings, 1 reply; 6+ messages in thread

From: Laura Smith @ 2023-05-25 10:08 UTC (permalink / raw)
  To: Erik Wienhold <ewie@ewie.name>; +Cc: postgre <pgsql-general@lists.postgresql.org>


> Looks like an encoding issue and a mismatch between database encoding and client
> encoding. You can check both with:
> 
> SHOW server_encoding;
> SHOW client_encoding;
> 
> Then either set the client encoding or use COPY's encoding option to match the
> database encoding (I assume utf8 in this example):
> 
> SET client_encoding = 'utf8';
> COPY (...) TO /tmp/bar.csv DELIMITER ',' CSV HEADER ENCODING 'utf8';
> 
> --
> Erik

Hi Erik,

Looks like you could well be right about encoding:

postgres=# SHOW server_encoding;
 server_encoding 
-----------------
 UTF8
(1 row)

postgres=# SHOW client_encoding;
 client_encoding 
-----------------
 SQL_ASCII
(1 row)

I will try your suggestion...






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Maintaining accents with "COPY" ?
  2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  2023-05-25 07:48 ` Re: Maintaining accents with "COPY" ? Erik Wienhold <ewie@ewie.name>
  2023-05-25 10:08   ` Re: Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
@ 2023-05-25 12:37     ` Erik Wienhold <ewie@ewie.name>
  0 siblings, 0 replies; 6+ messages in thread

From: Erik Wienhold @ 2023-05-25 12:37 UTC (permalink / raw)
  To: Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>; +Cc: postgre <pgsql-general@lists.postgresql.org>

> On 25/05/2023 12:08 CEST Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch> wrote:
>
> > Looks like an encoding issue and a mismatch between database encoding and
> > client encoding. You can check both with:
> >
> > SHOW server_encoding;
> > SHOW client_encoding;
> >
> > Then either set the client encoding or use COPY's encoding option to match
> > the database encoding (I assume utf8 in this example):
> >
> > SET client_encoding = 'utf8';
> > COPY (...) TO /tmp/bar.csv DELIMITER ',' CSV HEADER ENCODING 'utf8';
>
> Hi Erik,
>
> Looks like you could well be right about encoding:
>
> postgres=# SHOW server_encoding;
>  server_encoding
> -----------------
>  UTF8
> (1 row)
>
> postgres=# SHOW client_encoding;
>  client_encoding
> -----------------
>  SQL_ASCII
> (1 row)
>
> I will try your suggestion...

The client encoding is not the problem here.  Using SQL_ASCII effectively uses
the server encoding.  SQL_ASCII basically means uninterpreted bytes/characters.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Maintaining accents with "COPY" ?
  2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
@ 2023-05-25 11:26 ` Peter J. Holzer <hjp-pgsql@hjp.at>
  2023-05-25 12:23   ` Re: Maintaining accents with "COPY" ? Erik Wienhold <ewie@ewie.name>
  1 sibling, 1 reply; 6+ messages in thread

From: Peter J. Holzer @ 2023-05-25 11:26 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2023-05-25 07:14:40 +0000, Laura Smith wrote:
> I'm currently doing a CSV export using COPY:
> 
> COPY (select * from foo where bar='foo') TO '/tmp/bar.csv' DELIMITER ',' CSV HEADER;
> 
> 
> This works great apart from accents are not preserved in the output,
> for example é gets converted to random characters, e.g. √© or similar.

How do you check the output?

If a single character is turned into 2 or 3 characters the issue is
usually that the program which produces the output (in the case of COPY
I think that would be the PostgreSQL server, not the client) produces
UTF-8, but the program consuming it expects an 8-bit character set
(typically windows-1252). See if oyu can tell that program that the file
is in UTF-8.

> How can I preserve accents ?

They probably already are preserved.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Maintaining accents with "COPY" ?
  2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
  2023-05-25 11:26 ` Re: Maintaining accents with "COPY" ? Peter J. Holzer <hjp-pgsql@hjp.at>
@ 2023-05-25 12:23   ` Erik Wienhold <ewie@ewie.name>
  0 siblings, 0 replies; 6+ messages in thread

From: Erik Wienhold @ 2023-05-25 12:23 UTC (permalink / raw)
  To: Peter J. Holzer <hjp-pgsql@hjp.at>; pgsql-general@lists.postgresql.org

> On 25/05/2023 13:26 CEST Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
>
> On 2023-05-25 07:14:40 +0000, Laura Smith wrote:
> > I'm currently doing a CSV export using COPY:
> >
> > COPY (select * from foo where bar='foo') TO '/tmp/bar.csv' DELIMITER ',' CSV HEADER;
> >
> > This works great apart from accents are not preserved in the output,
> > for example é gets converted to random characters, e.g. √© or similar.
>
> How do you check the output?
>
> If a single character is turned into 2 or 3 characters the issue is
> usually that the program which produces the output (in the case of COPY
> I think that would be the PostgreSQL server, not the client) produces
> UTF-8, but the program consuming it expects an 8-bit character set
> (typically windows-1252). See if oyu can tell that program that the file
> is in UTF-8.
>
> > How can I preserve accents ?
>
> They probably already are preserved.

You're right.  The bytes are probably interpreted as Mac OS Roman:

	$ echo é | iconv -f macintosh
	é
	$ echo -n é | xxd
	00000000: c3a9

--
Erik





^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2023-05-25 12:37 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-05-25 07:14 Maintaining accents with "COPY" ? Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
2023-05-25 07:48 ` Erik Wienhold <ewie@ewie.name>
2023-05-25 10:08   ` Laura Smith <n5d9xq3ti233xiyif2vp@protonmail.ch>
2023-05-25 12:37     ` Erik Wienhold <ewie@ewie.name>
2023-05-25 11:26 ` Peter J. Holzer <hjp-pgsql@hjp.at>
2023-05-25 12:23   ` Erik Wienhold <ewie@ewie.name>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox