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 1uu4Pe-001dbE-I8 for pgsql-admin@arkaria.postgresql.org; Thu, 04 Sep 2025 07:29:43 +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 1uu4Pc-00DpYr-2y for pgsql-admin@arkaria.postgresql.org; Thu, 04 Sep 2025 07:29:40 +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 1uu4Pb-00DpXz-MV for pgsql-admin@lists.postgresql.org; Thu, 04 Sep 2025 07:29:40 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uu4Pa-000T2q-15 for pgsql-admin@lists.postgresql.org; Thu, 04 Sep 2025 07:29:39 +0000 Received: by mail-ed1-x52f.google.com with SMTP id 4fb4d7f45d1cf-61ebe5204c2so965098a12.3 for ; Thu, 04 Sep 2025 00:29:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1756970976; x=1757575776; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=a0OwnCw6SBLZ84POT0fI1zC3UoUmp9HvvzwClS0cYs8=; b=DCYaCG+7JoS34RG29NUWMcYeGBCU8vOqqKFwgsooBVOzw914pJYHpfAWxwOYc6nE02 E9OudvsxffgNvslumYL/2wz85rB1nTcG3+6Mh2vS2THxNKTM1U3xm8uODMotgCK0rZr0 J2GjdLxewA1FlRjcVireMIq/gulnrSUllq0b7wZEC6J2E50vVz6SHrv7uoDbNrx2CN8P kw3VbokezT5YyFcLeNq4m8j0ncyCI7UGQT49h6GTCBNFDvxxTw2UtSgx4q1toLUFuhXl QVjl6FfPxqKgPV5ov855lDym3zLu840gnfOVnmgd2tv/4GIEOroRIC1OhPZ0xmlPyrl7 I/Pg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756970976; x=1757575776; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=a0OwnCw6SBLZ84POT0fI1zC3UoUmp9HvvzwClS0cYs8=; b=JNUCZvrYHukxtoVP68jpwyDVHLv4nh1Q9leRhwZjEs6yP2q0q47zZ+rbcwBWF4RvZU MbVVrnEhjyP85OeptI6s9PINy0iFhKGs+8qIQzWTjdWUkAmtxLKnzNsNh92kBy1Sgh7A vljIqhBjvKY1rZOcbgGwDhxPw+NEg4Q7Zril2yiVExiU0jEqDxtbunn71XvmWXljgiJK FJU9uZjjxU5z6dDFsZCoDIN+IjU1mCgMGWi2d1wFKbOwDhP26w7XgCnFhzKtwT/xMRA2 vj8mZ7GGU0JEgzzpP9Xyc2TbyRFLT751T6pHP4WS6+smScvY8k/A6AYs7qfSEiQsZ741 3D8Q== X-Forwarded-Encrypted: i=1; AJvYcCWK/6uqp7RzbNqP5icD8rxrfh4s7UXlX7IYle+P6JBcfNANrm7SCg2fcL9EhbuDAgaM6RSJO19Ys41zqg==@lists.postgresql.org X-Gm-Message-State: AOJu0YwrO2ciBh12kwLs9W5M2iynIvfsSihJYT0QkJup80LAHntBThst ooXE2d4fuZihLtyYdPvBoOFvx8/crdEaTsrSTaQSPfTqjN38OszxN5r6zFiv+3DhJ4s4LbrSqcE CAFIl X-Gm-Gg: ASbGncsfDs1qIrTmfHdCKC3FgpOHZaLbfCugZy8YOsAvZayw/8w7dD3kDI4I4lWKuvq /TkgUQ5vMxjU+cAhi6J0Uv/3nbHgw1vi3b0Il4BUyrklM6RfrC+5/uK2bawMGkHPfvZn7D1pLPh vW/C3IkZbHs10sn0B8GIluByLQES77FmyUSlczGw3I+7mOCu9uOeYi+qCI5D4rA9H+ndO/ZCqWb L6GEVw/LQCI000AYMR+n9VtwB+vKWkyrrB1jVgN6b8giE9Qpu7uJRuvfD1rd7R29g1iPugr0wT2 YPbx7lhgif5fvY/sQFY6zbQXzsf4yas1HCukLCYRc17XJrqLt1ZtEnHgN/HCqZbDIJwkj0gD6ZE Ebmb/yKaScfigl+jMted+tUVrEkzVgakfr7tv4fhNLS5ZlILv1t1qN4ykWAU= X-Google-Smtp-Source: AGHT+IGQIOM/D2TP7YcWVbj1Gfu/ajVv4nmFnz6u4y/amstYDlK74B82we8oWln7gM/RrsBSTDMZuQ== X-Received: by 2002:a05:6402:5c8:b0:618:534:550a with SMTP id 4fb4d7f45d1cf-61d26c53c4amr17141584a12.24.1756970976549; Thu, 04 Sep 2025 00:29:36 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([41.66.96.30]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-61cfc4ea764sm13718142a12.40.2025.09.04.00.29.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Sep 2025 00:29:36 -0700 (PDT) Message-ID: <8ec53dc7d4508410124a5a6d1442840aa90a8f0c.camel@cybertec.at> Subject: Re: Escaping special characters - \r when doing COPY CSV From: Laurenz Albe To: Edwin UY , Pgsql-admin Date: Thu, 04 Sep 2025 09:29:35 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-1.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2025-09-04 at 18:01 +1200, Edwin UY wrote: > I don't have access to run copy as this is a RDS database so I can only u= se \copy. > I need to send the output to csv. > One of the fields has a special character =3D \r. I can see it visually.= =20 >=20 > Running the \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 >=20 > 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'); >=20 > But it gives ERROR: =C2=A0COPY escape must be a single one-byte character= . >=20 > Besides using a view for the SQL, as suggested when I do a web search, an= y chance someone here knows how to do it without=C2=A0using a view? There is no problem with having the carriage return characters in the CSV o= utput, but if you'd rather do without them, you could trim them: \copy (SELECT trim(textcol, E'\u000d'), othercol, ... FROM tab ...) TO ..= . In order to include escape sequences into a string literal, you have to pre= pend the opening single quote with an "E" for "extended": E'\r' E'\x0D' E'\u000D' E'\U0000000D' Yours, Laurenz Albe