Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda