Andrew Young
Andrew Young

Reputation: 1779

Why does running this query in node.js block the process from exiting?

Why does running queries using the pg module block the process from terminating?

This piece of code is taken straight from the github page's readme. Executing this node.js script in terminal performs the sql query but does not return to the bash prompt right away. It probably takes about 20 seconds to exit.

var pg = require('pg'); 
var conString = "tcp://postgres:1234@localhost/postgres";

//error handling omitted
pg.connect(conString, function(err, client) {
  client.query("SELECT NOW() as when", function(err, result) {
    console.log("Row count: %d",result.rows.length);  // 1
    console.log("Current year: %d", result.rows[0].when.getYear());
  });
});

Upvotes: 3

Views: 405

Answers (1)

David Schwartz
David Schwartz

Reputation: 182893

You just need to call pg.end.

Upvotes: 3

Related Questions