Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

310
Views
How to use pg-promise to download data as a CSV file?
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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

  • Save PL/pgSQL output from PostgreSQL to a CSV file
  • Export Postgres table to CSV file with headings
  • How do you print the result of a PostgreSQL query in CSV or TSV format from the command line?

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:

  • Download a file from NodeJS Server using Express
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!