Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uu33G-001B37-2g for pgsql-admin@arkaria.postgresql.org; Thu, 04 Sep 2025 06:02:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uu33C-00DJEJ-DY for pgsql-admin@arkaria.postgresql.org; Thu, 04 Sep 2025 06:02:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uu33C-00DJEB-0D for pgsql-admin@lists.postgresql.org; Thu, 04 Sep 2025 06:02:26 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uu33A-000SLK-2J for pgsql-admin@lists.postgresql.org; Thu, 04 Sep 2025 06:02:25 +0000 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-3280264a6e8so436139a91.3 for ; Wed, 03 Sep 2025 23:02:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756965743; x=1757570543; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=yhdQv+NrgRmDp3jiYNB194N54KloKdHXc6sE0oWCN4w=; b=fjNEu6XtdMYqJUhHrofLqZnn+PH/TmcEM4AqXPkYRWjr8MqsqmSSnpb+JydSAWOC2h Nn5GJgIJW3kAfFQ/9P1KJrxbuxJ/Vhd54a7TsQb4iUW3TUIp+NsoxcJNHZhjfpQlFb80 aWNn8V9D7Pb/Ccj3SlDB6kCbLT0fAcPgjS6Jyj/wjW8NP3xPNRIDZNlTmJ4p3ZLTQrC9 F4f3Oz8F2rKd6WvEexOc1d+g0bbPmA2UCJWgxF+0pA07uhglgnc2NdTgPEEYOpkPtpMG e1ZQ8r8oY80VHQFzDWKDSne4lctJSHUXAABN7ztS1Mqj+rZcZN3JE8QMykyii2UnKsjz AB8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756965743; x=1757570543; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yhdQv+NrgRmDp3jiYNB194N54KloKdHXc6sE0oWCN4w=; b=H5Ny10aCuJXr5vlHrSAtKejyFu088y72aun3FkfLgd1zAlH26SIFnAujdTt4aTYPDN mXO3Kv+jdFgOJkDKhP5ad4NXSTWprBm6j6QCqXICH5NSm5jwjUiBOuYwwwR40Eaqt0Vh eU4ez6/gDRog8mH4U6wGik6p53CKGkujeSeV2J+wliGr/PWQLJe3GmB1DGIRAiXHlmIF hI5wyqcdvqZKTb14Nbzi5xfFu5uA82Rez+fQDrqT/+dkP8OQefeEi59OsOhz549fWrln gINfc8n4ZwCCZ+vPPVpFoUji1jzFreR0rZlwugmwetpC44LoH2EuVMmB0mVDM70Ig6AU DHEw== X-Gm-Message-State: AOJu0Yx6qLkvHHd5RuQpn5k9x6LeZcSPehy4wsnm3S3gtGX/vSH4fZFd eUtjC34gghIH1gCBykZIhZCnEb8NMd4ndxjMN3sKAH5TDqxWVgns5LbaztsPxDmRLNdVTgMDFK6 NKOVUHYHxSHz7Dor/L2dZ6MML0GXUfsWLeFhFAF0= X-Gm-Gg: ASbGncv+rA5VPUeaMHrtX7iDpYkPGsmJlpRAhN9v0paeN+ogPh/xwWW4/FB98M5fjlU Z+H89gvcrnMJQYZOUCdhDKcbz6jyTaDS5pNHkD5WLWS2AkPnT9lMg8EcppVffJg4xRGcLLgiBBK w0viZ8rHS0s0SBMHBc+A6TYz/KAswc5i/Y7O2GMv9CYD+mFl7SLD3mK1250BCAImTZ/rJzty4py 77Oy+L5n1vK4DYHSw== X-Google-Smtp-Source: AGHT+IH6QdqIaHfhLYqzN8C23ciRoN+ZbvT+/FoX0pb4AlBQ6cM+BTGcd+NopAsmothRJpfMbLwgoPY3omWnWSIMxVY= X-Received: by 2002:a17:90b:3810:b0:32b:6132:5fa0 with SMTP id 98e67ed59e1d1-32b6132623fmr6648850a91.17.1756965743061; Wed, 03 Sep 2025 23:02:23 -0700 (PDT) MIME-Version: 1.0 From: Edwin UY Date: Thu, 4 Sep 2025 18:01:45 +1200 X-Gm-Features: Ac12FXxZ5AZhVw24jKPA2Q_eLBp0Q5Wc5t0n2wLk_iSKC3vgV1QIIA6aplnddQA Message-ID: Subject: Escaping special characters - \r when doing COPY CSV To: Pgsql-admin Content-Type: multipart/alternative; boundary="000000000000ff73e6063df37615" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ff73e6063df37615 Content-Type: text/plain; charset="UTF-8" Hi, I don't have access to run copy as this is a RDS database so I can only use \copy. I need to send the output to csv. One of the fields has a special character = \r. I can see it visually. Running the \copy below gives me extra row/s because of the \r \copy ( select * from blah where blah_ref = '666' order by date_created desc limit 5; ) to 'abc.csv' WITH CSV DELIMITER ',' HEADER I have tried this \copy ( select * from blah where blah_ref = '666' order by date_created desc limit 5; ) to 'abc.csv' WITH (FORMAT CSV, HEADER, ESCAPE '\r'); But it gives ERROR: COPY escape must be a single one-byte character. Besides using a view for the SQL, as suggested when I do a web search, any chance someone here knows how to do it without using a view? Please advise. Thanks in advance. Regards, Ed --000000000000ff73e6063df37615 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

I don't have access = to run copy as this is a RDS database so I can only use \copy.
I = need to send the output to csv.
One of the fields has a special c= haracter =3D \r. I can see it visually.

Running t= he \copy below gives me extra row/s because of the \r
\copy ( select *=C2=A0 from blah=C2=A0 where blah_ref =3D '666'=C2=A0=C2=A0= order by date_created desc=C2=A0=C2=A0limit 5; )=C2=A0to 'abc.csv' WITH CSV DELIMITER ',' HEADER

I have tried this
\copy ( select *=C2=A0 from blah=C2=A0 where blah_ref =3D '666'=C2=A0=C2=A0= order by date_created desc=C2=A0=C2=A0limit 5; )=C2=A0to 'abc.csv' WITH (FORMAT CSV, HEADER, ESCAPE '\r');=

But it gives ERROR: =C2=A0COPY escape must be= a single one-byte character.

Besides using a view= for the SQL, as suggested when I do a web search, any chance someone here = knows how to do it without=C2=A0using a view?

Plea= se advise. Thanks in advance.


Regar= ds,
Ed

--000000000000ff73e6063df37615--