Skip to content
The postgres backend protocol implemented in pure JS for use with node.js
JavaScript
Find file
Pull request Compare This branch is 32 commits ahead, 5 commits behind creationix:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib
.gitignore
README.md
demo.js
demo.sh
demo.sql

README.md

PostgreSQL for Javascript

This library is a implementation of the PostgreSQL backend/frontend protocol in javascript. It uses the node.js tcp and event libraries. A javascript md5 library is included for servers that require md5 password hashing (this is default).

This library allows for the correct handling of prepared queries.

If you wish to nest DB calls, db.close must be in the deepest callback, or all statements that occur inside of callbacks deeper than the callback which handles db.close will not be executed.

All code is available under the terms of the MIT license, unless otherwise noted (md5.js)

Example use

var sys = require("sys");
var pg = require("postgres");

var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
db.query("SELECT * FROM sometable", function (data) {
    sys.p(data);
});
db.close();

Example use of Parameterized Queries

var sys = require("sys");
var pg = require("postgres");

var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");
db.query("SELECT * FROM yourtable WHERE id = ?", [1], function (data) {
    sys.p(data);
});
db.close();

Example use of Prepared Queries

var sys = require("sys");
var pg = require("postgres");

var db = new pg.connect("pgsql://test:12345@localhost:5432/template1");

var stmt = db.prepare("SELECT * FROM yourtable WHERE id = ?");

stmt.execute([1], function (d) {
    sys.p(d);
    db.close();
});

Example Demo

The demo.js file contains a simple example of working with postgres-js. There is also an associated demo.sh that will run demo.sql to create a simple table for the demo.

Something went wrong with that request. Please try again.