Install npm i pg, if slow, remember to climb a ladder
Connection Pooling way:
the require PG = var ( 'PG'); // database configuration var config = { User: "Postgres", Database: "Test", password: "Postgres", Port: 5432, // extended attribute max: 20, // The maximum number of connection pools idleTimeoutMillis: 3000, // the maximum idle time is connected 3S } // create a connection pool var = new new pg.Pool the pool (config); // query pool.connect (function (ERR, Client, DONE) { IF (ERR) { return console.error ( 'error database connection', ERR); } // output a simple World the Hello client.query ( '$ the SELECT OUT. 1 :: VARCHAR the AS', [ "the Hello World"], function (ERR , Result) { DONE ();// releasable connection (which is returned to the pool) IF (ERR) { return console.error ( 'Query Error', err); } console.log(result.rows[0].out); //output: Hello World }); });
Client direct connection:
const pg=require('pg') var conString = "postgres://username:password@localhost/databaseName"; var client = new pg.Client(conString); client.connect(function(err) { if(err) { return console.error('连接postgreSQL数据库失败', err); } client.query('SELECT * FROM tableName', function(err, data) { if(err) { return console.error('查询失败', err); }else{ // console.log('成功',data.rows); console.log('成功',JSON.stringify(data.rows)); } client.end(); }); });
One way to find their own way:
// Connect PG var PG = the require ( 'PG'); var the pool = new new ({pg.Pool Host: 'XXXXXX', User: 'XXXX', password: 'xxxxx', Database: 'XXX', Port: 5432 , max: 20 is, // the maximum number of connections the connection pool idleTimeoutMillis: 3000, // the maximum idle time is connected 3S }); function Query (SQL, the callback) { pool.connect (function (ERR, connection, DONE) { IF (ERR ) { the console.log ( 'Connect Query:' + err.message); return; } // the Use Connection The connection.query (SQL, [], function (ERR, rows) { DONE (); the callback (ERR, rows ); //connection.release();// release link // release link }); }); } exports.query = Query;
/** * 获取survey *localhost:5000/getAllSurveyList */ app.get('/getAllSurveyList', function (req, res) { console.log("getAllSurveyList"); db.query('select id,trim(latitude) as latitude,trim(lontitude) as lontitude,isshow as '+'"'+'isShow'+'"'+' from survey', function (err,rows) { if (err) { console.log(err); console.log('--getAllSurveyList: 查询失败'); res.json({result:'false',datas: null}); } else { console.log('--getAllSurveyList: 查询成功'); //console.log(rows); res.json({result: 'true', datas: rows.rows}); } }) });
CRUD
var pg = require ( './ pg '); // load module node-postgres, the module to be placed in this document in the same directory var conString = "postgres: // postgres : postgres @ localhost: 5432 / node- test "; // when the database must be created // Anything: // User: password @ Host: Port / database var = new new Client pg.Client (conString); the client.connect (function (ERR) { IF (ERR) { return console.error ( 'Could Not Connect to Postgres', ERR); } // delete the table exists the console.log ( "Dropping table 'Person'") var = client.query Query ( "drop Person table IF eXISTS") ; query.on ( 'End', function () { the console.log () "the Dropped!"; }); // Create a table console.log ( "Creating table 'person'"); query = client.query("create table person(id serial, username varchar(10), password varchar(10), age integer)"); query.on('end', function(){ console.log("Created!"); }); //添加 client.query('INSERT INTO person(username, password, age) VALUES($1, $2, $3)', ["zhangsan", "123456", 20], function(err, result) { console.log( "====================add========================"); if(err) { console.log(err); return rollback(client); } console.log( result); }); //查询 client.query('select username, password, age from person where id = $1', [3], function(err, result) { console.log( "===================query========================="); if(err) { console.log(err); } console.log(result.rows[0]); }); //更新 client.query('update person set password=$1 where id = $2', ["11a",1], function(err, result) { console.log( "=====================update======================="); if(err) { console.log(err); } console.log(result); }); //删除 client.query('delete from person where id = $1', [1], function(err, result) { console.log( "====================remove======================="); if(err) { console.log(err); } console.log(result); client.end(); }); }); var rollback = function(client) { client.query('ROLLBACK', function() { client.end(); }); };