2
db.any(query)
    .then(data => {
        //console.log("data: " + JSON.stringify(data));
        res.json(data);
        //res.send("data: " + JSON.stringify(data));
    })
    .catch(error => {
        console.log("ERROR:", error)
    });

Is it possible to get the data in a CSV format rather than JSON?

vitaly-t
  • 20,421
  • 5
  • 85
  • 117

1 Answers1

1

As I was stating in a comment, this is not relevant to pg-promise, which simply executes queries.

The question should have been asked - how to select into a CSV file in PostgreSQL, for which however there are plenty of answers already:

Then for pg-promise you would arrive to something as trivial as this:

var values = {
    delimiter: ',',
    path: 'd:/temp/users.csv'
};

db.none('COPY (SELECT * FROM users) TO ${path} WITH CSV DELIMITER ${delimiter}', values)
    .then(() => {
        console.log('Successfully saved as:', values.path);
    })
    .catch(error => {
        console.log(error);
    });

And if instead of exporting result of the select, you want to download it from an HTTP service, well, that's an entirely different question then. But you would simply forward the result of a select into your HTTP response, marking it as a file.

Examples:

Community
  • 1
  • 1
vitaly-t
  • 20,421
  • 5
  • 85
  • 117