你可以在这里通过谷歌搜索找到这个
使用“pg-promise”的示例:
var pgp = require('pg-promise')(/*options*/);
var cn = {
host: 'my_host', // server name or IP address;
port: 5401,
database: 'myDatabase',
user: 'myUser',
password: 'myPassword'
};
var db = pgp(cn);
// SELECT all rows with id = 111 in my_table
db.one("SELECT * FROM my_table WHERE id=$1", 111)
.then(function (result) {
console.log(result); // print user result;
})
.catch(function (error) {
console.log(error); // print why failed;
});
以及其他一个使用“pg”模块连接 Postgres 数据库的示例:
var Client = require('pg').Client;
var configServer = {
user : 'user', // if don't have user and pass then ''
password : 'my_pass',
database : 'my_database',
host : 'my_host',
port : 54103 // this is a example
};
var client = new Client(configServer);
client.connect();
// SELECT all rows in my_table
var sql = "SELECT * FROM my_table";
client.query(sql, callback);