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"