---
title: "node.js install postgreSQL 初体验"
author: "ynial"
date: "2012-11-10T09:59:52Z"
tags: []
categories: ["企业架构"]
slug: "node-js-install-postgresql"
draft: false
lastmod: "2012-11-10T10:02:18Z"
id: 1722116
url: "https://ynial.iteye.com/blog/1722116"
---

node.js install postgreSQL 初体验


1：安装postgresql

2:命令行：
npm install pg
.......

【第一个难题】 
我们需要安装python
![](http://dl.iteye.com/upload/attachment/0076/4133/965d030c-2c2b-3504-871b-485daf64de7c.jpg)
【第二个难题】
这个错误的原因很令人头痛,查阅了很多资料无果。
尝试着在环境变量path里添加 C:\Program Files\PostgreSQL\9.2\bin 后继续无果
在自定义path继续中 添加C:\Program Files\PostgreSQL\9.2\bin 
后OK

----------------------------------------------------------------------

3：测试代码 


```java

var express = require('express');
var pg = require('pg');
var app = express();

////////////////////////////用户名：密码/服务器名/数据库名
var conString = "postgres://postgres:password@localhost/test";
var client = new pg.Client(conString);
client.connect();
client.query('INSERT INTO t_user(id, name) VALUES($1, $2)', [3, 'laoyang']);

app.get('/', function(req, res){
  query = client.query('SELECT COUNT(id) AS n FROM t_user');
  query.on('row', function(result) {
    console.log(result);

    if (!result) {
      return res.send('No data found');
    } else {
      res.send('Visits today: ' + result.n);
    }
  });
});
app.listen(3000);
console.log('Listening on port 3000');

```


测试OK