Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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());
  });
});
like image 941
Andrew Young Avatar asked Mar 28 '26 23:03

Andrew Young


1 Answers

You just need to call pg.end.

like image 177
David Schwartz Avatar answered Apr 02 '26 04:04

David Schwartz