简体   繁体   中英

how to convert data selected from a postgres database to json or csv to use it with d3js lib?

is there any way to convert data that get from my postgresql database to csv or json, I just want to use it as input for my D3.js ! otherwise is it possible use it without conversion ?

this how I get data from my Database :

    var pg = require("pg");

    var connectionString = {
        user: 'user',
        host: 'host',
        database: 'db',
        password: 'pass',
        port: 5432,
    };

    var pool = new pg.Pool(connectionString);
    pool.connect(function(err, client, done) {
        const query = client.query(new pg.Query("SELECT * from products")), query.on('row', (row) => {
            console.log(row);
        })
        query.on('end', (res) => {
            // pool shutdown 
            console.log("ending");
            pool.end()
        })

        query.on('error', (res) => {
            console.log(res);
        })

        done()
    })
    // done

You can use row_to_json and array_to_string in your query.

See below an example.

SELECT array_to_string(array( SELECT row_to_json(products.*) FROM products),', ') as jsonData;

Hope this will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM