Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda