1.首先打开node.js command prompt, 然后进入工程目录下,使用npm install pg来安装postgresql module
2.代码中:
var pg = require('pg');
// Connect to the PostgreSQL database, 123 is password
var conString = "tcp://postgres:123@localhost/MyDataBase";
var client = new pg.Client(conString);
client.connect(function(error, results) {
if(error){
console.log('PostgreSQL connection Error: ' + error.message);
client.end();
return;
}
console.log('client.connect OK.\n');
});
// Execute select
exports.select = function(callback){
var selectStr = "select * from 表名 where XXX =XXX'";
client.query( selectStr, function(error, results){
if (error){
console.log('exports.select Error: ' + error.message);
callback(error, results);
return;
}
callback(null, results.rows);
});
};