标签:root postgre 扩展 timeout arch length 数据 data efi
postgres数据库安装:windows安装解压版postgresql
1、使用nodejs模块pg操作postgres数据库
const pg = require(‘pg‘) // 数据库配置 var config = { user: "wenbin.ouyang", host: ‘localhost‘, database: "test", password: "", port: 5432, // 扩展属性 max: 20, // 连接池最大连接数 idleTimeoutMillis: 3000, // 连接最大空闲时间 3s } // 创建连接池 var pool = new pg.Pool(config); // 查询 pool.connect(function (err, client, done) { if (err) { return console.error(‘数据库连接出错‘, err); } // 简单输出个 Hello World client.query(‘SELECT $1::varchar AS OUT‘, ["Hello World"], function (err, result) { done();// 释放连接(将其返回给连接池) if (err) { return console.error(‘查询出错‘, err); } console.log(result.rows[0].out); //output: Hello World }); }); pool.connect().then(client => { // insert 数据 client.query("INSERT INTO student(name, age) VALUES($1::varchar, $2::int)", ["xiaoming", "20"]).then(res => { console.log("Insert Success") // 如果是自增ID,有返回值的,在res里 return res; }) .then(res => { // 查询xiaoming return client.query("Select * FROM student WHERE name = $1", ["xiaoming"]); }) .then(res => { // 输出结果,看是否插入成功 console.log(res.rows[0]) // { id: 4, name: ‘xiaoming‘, age: 20 } console.log(res.rows.length) }) .then(res => { // update 数据,将age改为21 return client.query("UPDATE student SET age=$1 WHERE name=$2", [21, "xiaoming"]) }) .then(res => { // 再查询一次xiaoming return client.query("Select * FROM student WHERE name = $1", ["xiaoming"]); }) .then(res => { // 再输出结果,看是否改为了21 console.log(res.rows[0]) console.log(res.rows.length) }) .then(res => { // 删除数据 client.query("DELETE FROM student WHERE name=$1", ["xiaoming"]) }) .then(res => { // 最后再查询一次xiaoming res = client.query("Select * FROM student WHERE name = $1", ["xiaoming"]); // 释放连接 client.release() return res }) .then(res => { // 再输出结果,没数据 undefined console.log(res.rows[0]) // undefined console.log(res.rows.length) // 0 }) })
2、封装pg模块
dbConfig.js
const pg = require(‘pg‘) // 数据库配置 var config = { user: "wenbin.ouyang", host: ‘localhost‘, database: "test", password: "root", port: 5432, // 扩展属性 max: 20, // 连接池最大连接数 idleTimeoutMillis: 3000, // 连接最大空闲时间 3s } // 创建连接池 var pool = new pg.Pool(config) module.exports = pool
---
标签:root postgre 扩展 timeout arch length 数据 data efi
原文地址:https://www.cnblogs.com/xy-ouyang/p/12321471.html