Node. js accesses the postgresql database

Source: Internet
Author: User

1) install the pg module: Enter/usr/local/lib: A node_schedules directory is found, the module npm install-g node_gyp export $ PATH = $ PATH:/usr/local/pgsql/bin/npm install pg //-g indicates global 2) connect to the database and access the connection string = "tcp: // User name: password @ localhost: 5432/Database Name"; 1) event format: var pg = require ('pg '); var MATH = require ('Math'); var constring = "tcp: // s: 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 () ;}); test successful. Output content: {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). Use the callback method to implement the above event format: var pg = require ('pg '); var MATH = require ('Math'); var constring = "tcp: // s: 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 <result. rowCount; 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.