2

I would like to know how can you check the row count of the query in PostgreSQL in node.js I have this code for the meantime.

    var client = new pg.Client(conString);
    client.connect();
    var query = client.query("SELECT * FROM users"); 
    query.on('row', function(row) {
      console.log(row);
    }); 
1
  • thanks for editing Milen Commented Feb 28, 2012 at 7:33

2 Answers 2

7
var client = new pg.Client(conString);

client.connect();

client.query("SELECT * FROM users", function(err, result) {
    console.log("Row count: %d",result.rows.length);  // n
});
Sign up to request clarification or add additional context in comments.

Comments

5

Another options is to use the rowCount property! Like this:

var pg = require('pg');

var pgClient = new pg.Client();
pgClient.connect();

var pgQuery = pgClient.query("SELECT * FROM information_schema.tables;");
pgQuery.on('error', function(err) {
    pgClient.end();
    console.error(err);
});
pgQuery.on('end', function(result) {
    pgClient.end();
    console.log(result.rowCount);
});

or like this:

var pg = require('pg');

var pgClient = new pg.Client();
pgClient.connect();

var pgQuery = pgClient.query("SELECT * FROM information_schema.tables;", function(err, result) {
    pgClient.end();
    if (err) return console.error(err);
    console.log(result.rowCount);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.