Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5bql-0001Jk-D6 for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 18:49:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f5bqk-0007ma-9v for pgsql-general@arkaria.postgresql.org; Mon, 09 Apr 2018 18:49:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5bqj-0007mR-Uy for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 18:49:06 +0000 Received: from mail.verite.pro ([185.16.44.216]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f5bqg-0007YD-LA for pgsql-general@lists.postgresql.org; Mon, 09 Apr 2018 18:49:04 +0000 Received: from genius-map.net (verite.pro [195.154.189.246]) by mail.verite.pro (Postfix) with ESMTP id DB90B8C00D5 for ; Mon, 9 Apr 2018 20:45:43 +0200 (CEST) Received: by genius-map.net (Postfix, from userid 1001) id 1727838A07E1; Mon, 9 Apr 2018 20:48:58 +0200 (CEST) Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: Conflict between JSON_AGG and COPY From: "Daniel Verite" To: "pgsql-general" In-Reply-To: <152647427.13167958.1523097853639.JavaMail.zimbra@digi-texx.vn> Date: Mon, 09 Apr 2018 20:48:54 +0200 Message-Id: <3bda7159-76ae-4b9e-9ea6-bfce5391f6b8@manitou-mail.org> X-Mailer: Manitou v1.7.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =C4=90=E1=BB=97 Ng=E1=BB=8Dc Tr=C3=AD C=C6=B0=E1=BB=9Dng wrote: > I want to export it to a file in JSON format so I run the query as below:= =20 > COPY (SELECT...) TO '/home/postgres/test1.json' COPY TO applies its own format rules on top of the data, which include among other things, quoting the newline characters. What you seem to expect is to copy a single-row, single-column piece of data verbatim to the output file, but COPY does not offer that. Such an option has already been discussed among developers, for instance in this thread as a 'RAW' format: https://www.postgresql.org/message-id/CAFj8pRAfer%2Bip3JCMTnvzra2QK7W9hk0hw= 1YqE8%2B%2BPZA1OqSJw%40mail.gmail.com but eventually it was not implemented. The unformatted output can be obtained client-side without COPY: psql -Atc "SELECT row_to_json(...)" > /path/to/file If you really need it server-side, a function that can write a result to a file is required. I don't think it exists as such out of the box, but you may either: - write your own function in any untrusted language to do just that (open file, write the piece of text into it, close it). - or see pg_file_write() from the adminpack contrib module https://www.postgresql.org/docs/current/static/adminpack.html With that, you may call, as a superuser: select pg_file_write('path/to/file.json',=20 (select row_to_json(...))::text, false); But note that the path must be relative to the PGDATA directory. - or export the data as an intermediary large object at the cost of some data churn in the large objects. And in that case, the path is not constrained to postgres data directory. do $$ declare id oid; j json; begin j :=3D (select row_to_json(t) from ); id :=3D lo_from_bytea(0, convert_to(j::text, 'UTF-8')); perform lo_export(id, '/path/to/file.json'); perform lo_unlink(id); end; $$ language plpgsql; For all these server-side methods, you need to be superuser, just like for COPY TO file anyway. Best regards, --=20 Daniel V=C3=A9rit=C3=A9 PostgreSQL-powered mailer: http://www.manitou-mail.org Twitter: @DanielVerite