nodejs模块pg操作postgres数据库

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

---

原文地址:https://www.cnblogs.com/xy-ouyang/p/12321471.html

时间: 2024-08-28 11:22:52

nodejs模块pg操作postgres数据库的相关文章

Python MySQLdb模块连接操作mysql数据库实例_python

mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql-python.sourceforge.net/有下载和文档. 由于python的数据库模块有专门的数据库模块的规范,所以,其实不管使用哪种数据库的方法都大同小异的,这里就给出一段示范的代码: #-*- encoding: gb2312 -*- import os, sys, string impo

Python使用cx_Oracle模块连接操作Oracle数据库

1. 简介 cx_Oracle 是一个用来连接并操作 Oracle 数据库的 Python 扩展模块, 支持包括 Oracle 9.2 10.2 以及 11.1 等版本 2.安装 最后是去官网http://cx-oracle.sourceforge.net/上下载安装,我自己通过pip和easy install安装都失败了,我是在win8.1的环境下安装的 3.使用 使用就很简单,以下为代码示例 #!/usr/bin/env python #-*- coding:utf-8 -*- import

递归、os.walk、内置函数、lambda、hashlib模块、md5加密、python安装第三方模块、操作mysql数据库

#递归就是函数自己调自己,一般递归都会有在什么情况下结束递归,一业可以有结束条件#递归最多死循环999次,递归不能设定次数# count=0# def abc():# global count# count+=1# print(count)# print('abc')# abc()# abc() #示例:# def add():# all_product=read_product()# print(all_product)# pname=input('请输入产品名称').strip()# ppr

Anaconda 安装redis-py模块操作redis数据库

今天遇到了一个很神奇的事情 ,在使用python操作redis 数据库的时候 ,如果使用  pip install redis    安装的是python 连接 redis的模块,  但是如果是在anaconda  里面使用  conda install -c anaconda redis  安装的是  redis数据库, 这个问题折腾了一下午,最终还是搞明白的,其实在使用 conda 要安装的是  redis-py   这个文件才对,但是至于pip安装与conda安装为什么是不同的文件我也不清

二十六、Nodejs 操作 MongoDb 数据库

一. 在 Nodejs 中使用 Mongodb 前面的课程我们讲了用命令操作 MongoDB,这里我们看下如何用 nodejs 来操作数据库需要引包: npm install mongodb --save-dev / cnpm install mongodb --save-dev mongodb官方文档网址: https://docs.mongodb.com/manual/ 进入下面的页面,你就可以选择你使用的开发语言的操作mongodb的文档,注意,一般很难加载出来的... node.js操作

nodejs操作mongodb数据库的方法

//http://mongodb.github.io/node-mongodb-native/3.0/quick-start/quick-start/ /* nodejs操作mongodb数据库 1.安装mongodb. cnpm install mongodb --save 2.引入mongodb下面的MongoClient var MongoClient = require('mongodb').MongoClient; 3.定义数据库连接的地址 以及配置数据库 qianfeng数据库的名称

postgres优化项及linux上pg操作记录

1.linux切换到pg命令: $ su - postgres $ psql postgres=# 2.查看/退出pg ps -ef |grep postgres postgres=# \q 3.一般优化项 参数名 作用 64GB建议值 256GB建议值 shared_buffers 设置PostgreSQL中用于缓存的专用内存量.建议的设置值为机器总内存大小的25%. 16GB 64GB wal_buffers WAL(预写日志)记录缓冲区,由wal_buffers定义的缓冲区的默认大小为16

专访阿里资深研发工程师窦贤明:PG与商业数据库差距并不明显

窦贤明认为, 支持类型.功能和语法丰富,性能优良 9月24日,窦贤明将参加在北京举办的线下活动,并做主题为<Greenplum分片案例分析>的分享.值此,他分享了PG.工作上的一些经历和经验. 想和这些大咖面对面聊PG吗?点击这里>>>免费报名 正文: 和大部分人一样,窦贤明也是被PG吸引过去的.有点特别的是,他之前完全不是做数据库的,“云计算刚刚兴起,分布式方兴未艾时,我一头扎了进去.”而和PG的结缘,也很巧合,“后来分布式数据库有紧急的工作需要去支持一下,然后就接触到了P

Koa 操作 Mongodb 数据库

node-mongodb-native的介绍 使用基于官方的 node-mongodb-native 驱动,封装一个更小.更快.更灵活的 DB 模块, 让我们用 nodejs 操作 Mongodb 数据库更方便.更灵活. 官方文档:http://mongodb.github.io/node-mongodb-native/ node-mongodb-native的基本使用 安装mongodb npm install mongodb --save 引入mongodb下面的MongoClient va