node-postgres【pg】 介绍
Postgre sql 是 一个 面向对象的关系 数据库 ,postgis是 一个 基于Postgre sql 的空间 数据库 插件 ,主要用于管理地理空间数据。因此在GIS领域,广泛使用Postgre sql 作为空间 数据库 。?
在Node.js中有专门的模块可以用来连接Postgre sql 数据库 ,首先从npm资源库中 获取 数据库 模块,名为”pg”:
npm install pg
该模块连接 数据库 有两种方式:
1 使用连接池
var pg = require('pg');var constring = "postgres://username:password@localhost/database";//this initializes a connection pool//it will keep idle connections open for a (con fig urable) 30 seconds//and set a limit of 20 (also con fig urable)pg.connect(constring,function(err,client) {if(err) {return console.error('error fetching client from pool',err);}client.query('SELECT $1::int AS number',['1'],result) {//call `done()` to release the client back to the poolpg.end();if(err) {return console.error('error running query',err);}console.log(result.rows[0].number);//output: 1});});其中”username”、”password”替换为对应 数据库 的 用户名 和密码,”localhost”替换为 数据库 服务器的地址,”database”替换为 数据库名字 。2 使用客户端实例连接
var pg = require('pg');
var constring = "postgres://username:password@localhost/database";
var client = new pg.Client(constring);
client.connect(function(err) {
if(err) {
return console.error(' Could not connect to postgres',err);
}
client.query('SELECT Now () AS "theTime"',result) {
if(err) {
return console.error('error running query',err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});
网站地址 : https://node-postgres.com
GitHub: https://github.com/brianc/node-postgres
网站描述: 在nodejs中用来连接Postgre sql 数据库 的模块
node-postgres【pg】官方网站
官方网站: https://node-postgres.com
如果觉得 网站内容还不错,欢迎将 网站 推荐给程序员好友。
查看更多关于node-postgres【pg】的详细内容...