PostgreSQL exporter en json

De wikidavenne
Aller à : navigation, rechercher
Hdr left.png
CENTOS logo small.png

First, get the data into the format you want, with fewer columns and any name changes.

SELECT
 name AS fruit_name,
 quantity
FROM data;

Then, put this in a subquery and convert it to JSON.

SELECT row_to_json(fruit_data) FROM (
 SELECT
   name AS fruit_name,
   quantity
 FROM data
) fruit_data;

Finally, wrap everything in copy.

COPY (
 SELECT row_to_json(fruit_data) FROM (
   SELECT
     name AS fruit_name,
     quantity
   FROM data
 ) fruit_data
) TO 'a.file';


here is another method : https://hashrocket.com/blog/posts/create-quick-json-data-dumps-from-postgresql