Node. js accesses the PostgresQL database

Source: Internet
Author: User
Tags postgresql module
Node. js accesses the PostgresQL database and installs the PostgresQL module: Access usrlocallib: A node_schedules directory is found, which contains the model you installed.

Node. js accesses the PostgresQL database and installs the PostgresQL module: Go to/usr/local/lib: A node_schedules directory is found, which contains your installed Model

1) install the PostgresQL module:
Enter/usr/local/lib: A node_schedules directory is found, which contains the module you installed.

Npm install-g node_gyp
Export $ PATH = $ PATH:/usr/local/pgsql/bin/
Npm install pg //-g indicates global

2) connect to and access the database
Connection string = "tcp: // User name: password @ localhost: 5432/Database Name ";

1) event form:
Var pg = require ('pg ');
Var MATH = require ('Math ');
Var constring = "tcp: // postgres: 1234 @ localhost/my ";

Var client = new pg. Client (constring );
Client. connect ();

Client. query ("create temp table beatle (name varchar (10), height integer )");
Client. query ("insert into beatle (name, height) values ('john', 50 )");
Client. query ("insert into beatle (name, height) values ($1, $2)", ['brown ', 68]);
Var query = client. query ("select * from beatle ");

Query. on ('row', function (row ){
Console. log (row );
Console. log ("Beatle name: % s", row. name );
Console. log ("beatle height: % d' % d \" ", MATH. floor (row. height/12), row. height % 12 );
});

Query. on ('end', function (){
Client. end ();
});

The test is successful and the output content is:
{Name: 'john', height: 50}
Beatle name: john
Beatle height: 4'2"
{Name: 'brown ', height: 68}
Beatle name: brown
Beatle height: 5'8"

2) callback function form:
Var pg = require ('pg ');

Var conString = "tcp: // postgres: 1234 @ localhost/my ";

Var client = new pg. Client (conString );
Client. connect (function (err ){
Client. query ('select NOW () AS "theTime" ', function (err, result ){
Console. log (result. rows [0]. theTime );
Client. end ();
})
});

The test is successful. The output result is:
Wed Jan 23 2013 14:30:12 GMT + 0800 (CST)

3) implement the above event form using callback:
Var pg = require ('pg ');
Var MATH = require ('Math ');
Var constring = "tcp: // postgres: 1234 @ localhost/my ";

Var client = new pg. Client (constring );

Client. connect (function (err ){
Client. query ("create temp table beatle (name varchar (10), height integer )");
Client. query ("insert into beatle (name, height) values ('john', 50 )");
Client. query ("insert into beatle (name, height) values ($1, $2)", ['brown ', 68]);
Client. query ("select * from beatle", function (err, result ){
Console. log (result );
For (var I = 0; I {
Console. log (result. rows [I]);
Console. log ("Beatle name: % s", result. rows [0]. name );
Console. log ("beatle height: % d' % d \" ", MATH. floor (result. rows [0]. height/12), result. rows [0]. height % 12 );
}
Client. end ();
});
});

The test is successful. The output result is:
{Command: 'select ',
RowCount: 2,
Oid: NaN,
Rows: [{name: 'john', height: 50}, {name: 'brown ', height: 68}]}
{Name: 'john', height: 50}
Beatle name: john
Beatle height: 4'2"
{Name: 'brown ', height: 68}
Beatle name: john

Beatle height: 4'2"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.