Giter Club home page Giter Club logo

node-postgres's Introduction

node-postgres

Build Status NPM version NPM downloads

Non-blocking PostgreSQL client for Node.js. Pure JavaScript and optional native libpq bindings.

Monorepo

This repo is a monorepo which contains the core pg module as well as a handful of related modules.

Documentation

Each package in this repo should have its own readme more focused on how to develop/contribute. For overall documentation on the project and the related modules managed by this repo please see:

The source repo for the documentation is available for contribution here.

Features

  • Pure JavaScript client and native libpq bindings share the same API
  • Connection pooling
  • Extensible JS ↔ PostgreSQL data-type coercion
  • Supported PostgreSQL features
    • Parameterized queries
    • Named statements with query plan caching
    • Async notifications with LISTEN/NOTIFY
    • Bulk import & export with COPY TO/COPY FROM

Extras

node-postgres is by design pretty light on abstractions. These are some handy modules we've been using over the years to complete the picture. The entire list can be found on our wiki.

Support

node-postgres is free software. If you encounter a bug with the library please open an issue on the GitHub repo. If you have questions unanswered by the documentation please open an issue pointing out how the documentation was unclear & I will do my best to make it better!

When you open an issue please provide:

  • version of Node
  • version of Postgres
  • smallest possible snippet of code to reproduce the problem

You can also follow me @briancarlson if that's your thing. I try to always announce noteworthy changes & developments with node-postgres on Twitter.

Sponsorship 💕

node-postgres's continued development has been made possible in part by generous financial support from the community.

If you or your company are benefiting from node-postgres and would like to help keep the project financially sustainable please consider supporting its development.

Contributing

❤️ contributions!

I will happily accept your pull request if it:

  • has tests
  • looks reasonable
  • does not break backwards compatibility

If your change involves breaking backwards compatibility please please point that out in the pull request & we can discuss & plan when and how to release it and what type of documentation or communication it will require.

Setting up for local development

  1. Clone the repo
  2. From your workspace root run yarn and then yarn lerna bootstrap
  3. Ensure you have a PostgreSQL instance running with SSL enabled and an empty database for tests
  4. Ensure you have the proper environment variables configured for connecting to the instance
  5. Run yarn test to run all the tests

Troubleshooting and FAQ

The causes and solutions to common errors can be found among the Frequently Asked Questions (FAQ)

License

Copyright (c) 2010-2020 Brian Carlson ([email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-postgres's People

Contributors

abenhamdine avatar alexanders avatar alxndrsn avatar amilajack avatar benesch avatar benhc123 avatar benighted avatar benjie avatar bjornblomqvist avatar booo avatar brianc avatar charmander avatar chyzwar avatar dependabot[bot] avatar eugeneware avatar grncdr avatar gurjeet avatar hjr3 avatar hoegaarden avatar lalitkapoor avatar mble avatar monteslu avatar petebacondarwin avatar phated avatar regevbr avatar sberan avatar sehrope avatar shine-on avatar vitaly-t avatar whitelynx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-postgres's Issues

Feature: support for LISTEN/NOTIFY?

Maybe node-postgres already supports the LISTEN/NOTIFY features of postgres in which case I apologize! If not I think this would be an interesting addition which would make a lot of sense in nodejs since it's so call-back focused!

node-PostgresClient has support for this it looks like but node-postgres looks more active.

possible update to https://github.com/brianc/node-postgres/wiki/Example

any anyone provide the full html for the page for this example - in particular what the

<script type="text/javascript" src= part would look like? I come from a strong postgres background and am just getting into the javascript side of things. Everything has been installed, but just seeking a little bit of page context for the example page.

invalid string in message

This just popped up starting with node v0.5.10. I am not able to get any query to work. The pg module seems to connect to the database, but it gets an error when it runs a query.

Here is some sample code that will produce this error:

var pg = require('pg'),
    util = require('util');
var connectstring = 'pg://user:[email protected]/db1';

function getTheTime() {
  pg.connect(connectstring, function (err, client) {
    if (err) util.log('Error with pg.connect!'), util.log(util.inspect(err));
    else client.query('SELECT now() as when', function (err, result) {
      if (err) util.log('Error with client.query!'), util.log(util.inspect(err));
      else util.log('The postres time is: ' + result.rows[0].when);
    });
  });
}

getTheTime();

This gives the following output:

31 Oct 13:24:35 - Error with client.query!
31 Oct 13:24:35 - { [error: invalid string in message]
  length: 100,
  name: 'error',
  severity: 'ERROR',
  code: '08P01',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: '.\\src\\backend\\libpq\\pqformat.c',
  line: '636',
  routine: 'pq_getmsgstring' }

I don't know what invalid string in message means or even where to begin looking in the module code. I tried it with postgres v8.4, and with postgres v9.0.

Per the node blog, node v0.5.10 is supposed to be the last version before node v0.6.

Small Fix to Example

The example on the front page attempts to query the beatles table by doing this:

var query = client.query("SELECT * FROM beatles WHERE name = $1", ['john'])

The J should be capital in John because that is the way it was inserted and PostgreSQL is case sensitive. Because of this the example code doesn't return John's record. This issue can be confusing for new users wondering why the data is not coming back.

Query should be:
var query = client.query("SELECT * FROM beatles WHERE name = $1", ['John'])

Uncaught, unspecified 'error' event.

Not sure if I'm doing something wrong or something erronous is happening. This is a stack trace I get from my app running in production... don't have more than that. It is using the JS driver. Any ideas?

{"stack":"Error: Uncaught, unspecified 'error' event.
    at [object Object].emit (events.js:47:15)
    at [object Object].<anonymous> (/var/www/adn/node_modules/pg/lib/client.js:106:12)
    at [object Object].emit (events.js:64:17)
    at Socket.<anonymous> (/var/www/adn/node_modules/pg/lib/connection.js:47:12)
    at Socket.emit (events.js:64:17)
    at Socket._onReadable (net.js:678:14)
    at IOWatcher.onReadable [as callback] (net.js:177:10)","message":"Uncaught, unspecified 'error' event."}

invalid startup packet layout: expected terminator as last byte

I just upgraded to node 0.5.10 and started getting "invalid startup packet layout: expected terminator as last byte" when connecting (connection then fails). I'm not entirely sure if this is a node-postgres issue, but it seems to be the most likely source of the problem. Things were working fine until the upgrade.

Additional detail: I'm use node-orm on top of node-postgres, though I don't believe there's any issue there.

garbage in npm-package 'pg'

Probably related to issue #21: somewhere in the build there's a hardcoded /home/brian/dev/node-postgres/build — could this be a stale .lock-wscript in the npm pkg despite its mention in .gitignore?

$ npm i .

> [email protected] install /home/s/p/channel-server/node_modules/pg
> node-waf configure build || true

Checking for program g++ or c++          : /usr/bin/g++ 
Checking for program cpp                 : /usr/bin/cpp 
Checking for program ar                  : /usr/bin/ar 
Checking for program ranlib              : /usr/bin/ranlib 
Checking for g++                         : ok  
Checking for node path                   : ok /home/s/.node_libraries 
Checking for node prefix                 : ok /usr 
Checking for program pg_config           : /usr/bin/pg_config 
'configure' finished successfully (0.058s)
Waf: Entering directory `/home/brian/dev/node-postgres/build'
Waf: Leaving directory `/home/brian/dev/node-postgres/build'
Traceback (most recent call last):
  File "/usr/bin/node-waf", line 18, in <module>
    Scripting.prepare(t, os.getcwd(), VERSION, wafdir)
  File "/usr/share/nodejs/wafadmin/Scripting.py", line 145, in prepare
    prepare_impl(t, cwd, ver, wafdir)
  File "/usr/share/nodejs/wafadmin/Scripting.py", line 135, in prepare_impl
    main()
  File "/usr/share/nodejs/wafadmin/Scripting.py", line 188, in main
    fun(ctx)
  File "/usr/share/nodejs/wafadmin/Scripting.py", line 386, in build
    return build_impl(bld)
  File "/usr/share/nodejs/wafadmin/Scripting.py", line 405, in build_impl
    bld.compile()
  File "/usr/share/nodejs/wafadmin/Build.py", line 268, in compile
    os.chdir(self.bldnode.abspath())
OSError: [Errno 2] No such file or directory: '/home/brian/dev/node-postgres/build'

> [email protected] install /home/s/p/channel-server
> node-waf configure build

Checking for program coffee              : /home/s/p/channel-server/node_modules/.bin/coffee 
Checking for program cp                  : /bin/cp 
'configure' finished successfully (0.002s)
Waf: Entering directory `/home/s/p/channel-server/build'
Waf: Leaving directory `/home/s/p/channel-server/build'
'build' finished successfully (0.020s)
[email protected] ./node_modules/pg 
└── [email protected]

ProcessStartupPacket issues

Error with postgres works fine on node v0.5.9 but breaks on 0.5.10 and up I am currently running off node master since v0.6 is so close to coming out. Also running postgres 9.1

{ [error: invalid startup packet layout: expected terminator as last byte]
length: 126,
name: 'error',
severity: 'FATAL',
code: '08P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
file: 'postmaster.c',
line: '1761',
routine: 'ProcessStartupPacket' }
error: invalid startup packet layout: expected terminator as last byte
at [object Object]. (/home/zachaller/zonemap2/node_modules/pg/lib/connection.js:387:11)
at [object Object].parseMessage (/home/zachaller/zonemap2/node_modules/pg/lib/connection.js:262:17)
at Socket. (/home/zachaller/zonemap2/node_modules/pg/lib/connection.js:45:22)
at Socket.emit (events.js:67:17)
at TCP.onread (net.js:306:14)

Binary (bytea) datatype support

Presently, bytea values are returned as backslash-escaped strings, which is not really convenient. I believe those should be returned as Buffers.

Simplest (but not really efficient) implementation I've hacked is:

var parseByteA = function(val) {
  return new Buffer(val.replace(/\\([0-7]{3})/g, function (full_match, code) {
    return String.fromCharCode(parseInt(code, 8));
  }).replace(/\\\\/g, "\\"), "binary");
}

registerStringTypeParser(17, parseByteA);

I also have another, probably more memory-efficient, but certainly less readable implementation with stricter error handling. I don't know what's the better version.

And, most important, I wonder whenever there's a way to get binary data straight from libpq (when using "native" mode), so no such heavy-weight parsing would be needed. Unfortunately, I know almost nothing about libpq internals, so I don't know whenever this is possible or not.

Problem doing Upsert in version 0.5.4

Hi there! Congrats on the excelent job!

I'm trying to do an upsert using node-postgres. The logic I was trying to implement was if an update returned error it should do the insert.

Both updates and inserts work but the code always executes both because the update statement returns the same values for success and error.

Have anyone tried this? Can anyone give a help please?

You can find full details (code + debug) here:
http://stackoverflow.com/questions/7910174/upsert-in-postgres-using-node-js

Thanks and congrats

garbage in npm-package 'pg'

'npm install pg' in ubuntu 64 error. In package present already compiled binaries for x86 with link to directory '/home/bmc/dev/node-postgres/build/default' (home of developer).

No question mark allowed in password

There is an issue that causes question marks in passwords to be rejected from the connection string. e.g.:

var str = 'pg://joe:[email protected]/local';
pg.connect(str, function (err, client) {});

will throw an error, because url.parse(str) returns:

{ protocol: 'pg:',
  slashes: true,
  host: 'joe',
  hostname: 'joe',
  href: 'pg://joe:[email protected]/local',
  search: '[email protected]/local',
  query: '[email protected]/local',
  pathname: '/:shmoe' }

which is clearly incorrect. I expected that if I encodeURIComponent the password, that pg would figure it all out for me. However, this would require we change utils.js from:

27  config.password = auth[1];

to:

27  config.password = decodeURIComponent(auth[1]);

I see no adverse response to this, since any password that would be changed by decodeURIComponent will already break the system (correct me if I'm wrong).

What are your thoughts?

inserting string data into int column fails silently

z, x, y are int columns:

var pg = require('pg');

var credentials = 'pg://demo@localhost/logs';

var client = new pg.Client(credentials);
client.connect();

client.query("INSERT INTO requests (api, name, z, y, x, type) " +
    "VALUES ($1, $2, $3, $4, $5, $6)",
    [ '1.0.0', 'foo', '35', '5', '2', 'png' ],
    function(err) {
        console.log(err);
    });

Reuse of named prepared query within transaction causes error

If you have an outstanding transaction and you reuse a named, prepared query, this causes an error.

client.connect();
client.query("BEGIN"); \\ 1
for (var i = 0; i < 2; ++i)
    client.query({name: "X", text: "SELECT $1::INTEGER", values:[0]}, function (err, result) { if (err) console.log(err); });
client.query("COMMIT"); \\ 2
client.on("drain", function() { console.log("finished"); client.end(); });

The above code errors with:

{ length: 78,
  name: 'error',
  severity: 'ERROR',
  code: '42P03',
  message: 'cursor "X" already exists',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'portalmem.c',
  line: '207',
  routine: 'CreatePortal' }

To fix the error, remove the lines comment as 1 and 2 i.e. remove the surrounding transaction, and all will be fine. Also you can change the loop to go through only once, or remove the query name, to fix the bug.

This is a fairly typical idiom, where you open a transaction so that you can pipeline several INSERT's into it. Having the INSERT statement prepared helps tremendously with this sort of situation.

Simplier prepared statement

You may have already dealt with this but it would be helpful to just use ? like in perl for a placeholder instead of $1 $2 etc... easier to type and don't have to worry about numbering. Just a thought.

bug: parseConnectionString

The parseConnectionString function does not work for connection strings containing for example ? as char. This is because we use url.parse()

  parseConnectionString: function(str) {
    var result = url.parse(str);
    result.host = result.hostname;
    result.database = result.pathname ? result.pathname.slice(1) : null;
    var auth = (result.auth || ':').split(':');
    result.user = auth[0];
    result.password = auth[1];
    return result;
  }

seeing a full prepared statement

i will be the first to admit that i have no clue about the internals of postgres prepared statements, but I was wondering if its possible to show the "final" query after preparing statements without the $1 stuff. It would be super helpful in debugging. Just a thought.

Connection pointer is Null

Found an error when nesting a query call inside of a child_process callback inside of another query callback.
Traceback: http://paste2.org/p/1742168
Code to generate bug: http://jsfiddle.net/ZhabH/ (you need to create a postgres table 'example' with columns "test" and "test_id" where test_id is an automatically updating integer. You may also need to update conString

A similar error is generated without the native flag : http://paste2.org/p/1742186

Software versions:
Postgresql: 8.4.9
node: 0.4.12
node-postgres: 0.6.3
express: 2.5.0

I think that's all the information one would need to know to replicate. Let me know if more info is needed.

callback + named query

this should work:

var q = query({name: 'whatever', text: 'select now()'}, function(err, result) {

});

insufficient data left in message

between either the upgrade to Node v0.4.10 or the upgrade to the latest pg module now gives me this error for any prepared statement:

{ length: 88,
  name: 'error',
  severity: 'ERROR',
  code: '08P01',
  message: 'insufficient data left in message',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'pqformat.c',
  line: '629',
  routine: 'pq_copymsgbytes' }

Is there some string escaping or preparing?

Hello. I'm using node-postgres. And I want to insert some strings into some table.
If I'm using single query, I can do like this and everything is working ok:

client.query('INSERT INTO sometbl VALUES ($1, $2)', ['foo', 'bar']);

But since I am using transactions and trying to do like this:

client.query('BEGIN; INSERT INTO sometbl VALUES ($1, $2); UPDATE sometable SET somevalue = $3; END;', ['foo', 'bar', 'foobar']);

I am getting an error with code 42601: 'cannot insert multiple commands into a prepared statement'.

Yes, I know that I can use simple string concatenation or some realizations of sprintf() to insert these parameters into the query string, but, I think, it isn't safe to do it without any preparation like escaping.
Is there some convinient way to do such preparation before inserting string values into the query string?

For example, I have found PQescapeLiteral function in the libpq, maybe there is some interface to this or similar function exists?
If no, can it be implemented?
//Sorry if the question is silly, I'm newbie in postgresql and node.

Undefined query parameters causes crash

If you pass an undefined value as a parameter to a query it crashes when trying to bind. Unfortunately with a stack trace that makes it very difficult to find your error (at least without putting console.logs inside of _pulseQuery to figure out which query is running).

Not sure what the correct solution would be here. Treat it as NULL? Throw an error?

Test case:

var pg = require('pg');
var conString = "tcp://user:pass@localhost/postgres";
pg.connect( conString, function(err, client) {
client.query("select $1 as foo", [ undefined ], function(err, result) {
console.log(result.rows[0].foo);
});
});

Crash output:

TypeError: Cannot call method 'toString' of undefined
at [object Object].bind (/Users/brian/node_modules/pg/lib/connection.js:138:17)
at [object Object].prepare (/Users/brian/node_modules/pg/lib/query.js:136:14)
at [object Object].submit (/Users/brian/node_modules/pg/lib/query.js:95:10)
at [object Object]._pulseQueryQueue (/Users/brian/node_modules/pg/lib/client.js:140:24)
at [object Object].query (/Users/brian/node_modules/pg/lib/client.js:164:8)
at assign_pending_items (/Users/brian/lucre/app.js:291:8)
at /Users/brian/lucre/app.js:213:3
at callbacks (/Users/brian/node_modules/express/lib/router/index.js:272:11)
at requireLogin (/Users/brian/lucre/app.js:46:3)
at callbacks (/Users/brian/node_modules/express/lib/router/index.js:272:11)

prepared statements and LIKE

Currently this gives me an error:
SELECT * FROM table WHERE field LIKE '%$1%'
Saying:
bind message supplies 1 parameters, but prepared statement "" requires 0

How do you go about using prepared statements with LIKE? Not sure if I'm missing something, didn't see it in docs. Thanks again! This library is working out great BTW.

Unexpected results when querying a table called 'user'

Queries to all other tables work as expected. When I query a table called 'user', it seems to return records from some other table, perhaps postgres' table internally representing database users.

  client.query("SELECT * FROM user", function(err, data) {
    console.log(client, err, data);
  });

returns

{ _queryQueue: [], _namedQueries: {}, _activeQuery: { text: 'SELECT * FROM user', values: [], callback: [Function], rows: [ [Object] ] }, _config: { database: 'mydb_development', user: 'mydbuser', password: 'mydbpass', host: undefined, port: 5432 }, _events: { connect: [Function], _row: [Function], _error: [Function], _readyForQuery: [Function] }, _connected: true } null { rows: [ { current_user: 'mydbuser' } ] }

However, if I duplicate my users table and name it differently, say 'user_copy', everything works as expected. The easy workaround is to just change the name of my user table.

Misunderstanding something

I'm sure I'm just misunderstanding something, but should this not work? Variables defined outside the pg.connect scope are not visible within it? I feel like I must be missing something obvious. Any little advice would be great. Thanks.
function(){ var myResultStore = []; pg.connect(conString, function(err, client) { if(err) throw new Error('Could not connect to the database'); client.query("SELECT name FROM people LIMIT 2", function(err, result) { if(err) throw new Error('Query Failure'); result.rows.forEach(function(row){ myResultStore.push(row.name)}); console.log(myResultStore.toString()); // ** correctly returns [Bob, John] }); } console.log(myResultStore); // ** returns [] }

crash

Really sorry if this isn't the place to post, but I've searched high and low for somewhere to see if someone can shed any light on the following issue.

I've compiled and built node.js version 0.4.11 under linux-mint Debian 201108 RC, and have used npm to install node-postgres. I'm using the following script as a test (new to node,js and wanted a really rough and ready test to compare it's performance against sinatra/rack).

var pg = require('pg');
var http = require('http');
var conString = "postgres://postgres:[email protected]:5432/test";

pg.connect(conString, function(err,client){
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
client.query("select * from items", function(err, result) {
if (err)
{
console.log(err);
}
else
{
for(var i = 0;i<result.rows.length;i++)
{
res.write(result.rows[i].name);
}
}
res.end();
});
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
});

One terminal has node running, and in a second I'm running the following: ab -n 20000 -c 5 http://127.0.0.1:1337/

The performance is amazing, but unfortunately about 30 seconds or so after the requests have all finished I get the following error

net.js:391
throw new Error('Socket is not writable');
^
Error: Socket is not writable
at Socket._writeOut (net.js:391:11)
at Socket.write (net.js:377:17)
at [object Object]._send (/home/richard/Documents/node_modules/pg/lib/connection.js:85:24)
at [object Object].end (/home/richard/Documents/node_modules/pg/lib/connection.js:186:8)
at [object Object].end (/home/richard/Documents/node_modules/pg/lib/client.js:158:19)
at Object.destroy (/home/richard/Documents/node_modules/pg/lib/index.js:43:16)
at Object.destroy (/home/richard/Documents/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:122:13)
at Object.removeIdle as _onTimeout
at Timer.callback (timers.js:83:39)

Can anyone help with the above - it's highly possibly that I've done something wrong, as I'm new to node, but feel that I've followed every instruction (nearly word for word).

Thanks

Client.prototype.connect with callback

What do you think about adding an optional callback function to the Client.prototype.connect function?

pg = require "pg"

client = new pg.Client "pg://postgres:testing@localhost/orm"

client.connect (error, client) ->
  if error then console.log error
  else
    console.log "connected (callback)"
    client.end()

client.on "connect", ->
  #never reached
  console.log "connected..."
  client.end()

client.on "error", (error) ->
  #only for errors afther connect event
  console.log error

I prepared a patch. Maybe you can take a look and tell me where the best place for test is.

Regards
Philipp

doesn't build with node 0.5.x

node_events.h no longer exists in the latest version of node and is no longer needed. Including node.h is enough now.

if server is unavail connection hangs over other connection

I'm working on error catching a postgres condition when the server is not available. The first time, everything works fine. The error is passed in and I can check for it and I'm set.

The second pass in hangs the at the connection line and I never get to the callback to get the error.

    console.log('about to connect');
    pg.connect(DBSTRING, function(err, client) {
        if (err) {
            console.log("ERROR DB " + err.message);
            stream.write(err.message);
        } else {
            // do query and such
        }
    });

Is the bit of code I'm working with. The about to connect is dumped on every request in, but the callback only seems to occur every other pass into here. Am I missing something in the error handling? Is there something I can check?

Thanks

Database Error instances should be instances of the builtin Error class

Hi,

Firstly, thanks for node-postgres - a lot of our projects use Postgres and node-postgres provides a great bridge to Node.

The database errors I've come across in node-postgres (either returned in callbacks or emitted via 'error' events) are all object literals and not Error instances. This means they don't have stack information and can't be handled by default software error handlers. These software handlers include the default node handler and the Express default handler. The former produces an "Uncaught, unspecified 'error' event." (see issue #41) and the latter just emits an 'undefined' error rather than producing a nice stack trace.

It would be great to have node-postgres emit database errors that are at instances of (or subclasses of) the builtin Error class, such that the following holds true:

client.query(
    'BAD SQL: RETURN AN ERROR',
    function (err, result) {
        assert.ok(err instanceof Error);
    }
);

examples don't seem to work out-of-the-box

Intriguing project! Unfortunately, I can't seem to get the current code base to do...anything for sure.

I'm running the latest Turnkey Linux stable in VirtualBox on a win32 platform. I successfully built node.js, and can run the basic example from nodejs.org and get it to serve a page to a client browser on win32.

But when I replace that with the example in the node-postgres readme, which I modified only to match my system's connection parameters, it runs, but nothing happens--no console output aside from the initial newline. Hitting it from a web browser says it can't connect (which makes sense, since it's not running a server.) If I add "console.log('start')" as the first line and similarly for the last line, both get logged one after another immediately when I run it, but still, no apparent database action.

So I tried the example app at https://github.com/brianc/node-postgres/wiki/Example . First of all, I had to change all the places where it said ?() to say function(), because I was getting syntax errors (were they supposed to be "?"? If so, is there some other missing dependency or setup step?) After that, it ran, but same thing again just sitting there after a newline, except that hitting it from a web browser results in a timeout (even if I change the port to 80 in the example code, or do :3000 in the url.) If I do the start/end console log statements, they too come out immediately one after another upon startup.

If I change the connection code to the wrong host, I get the appropriate error. If I instead change to the wrong port, user, password, or database, and I get no error but still no results. Lastly, I can connect to my postgres database using psql from the same Linux command-line I'm running node.js from.

Not sure what else to try...any hints?

automatic date parsing ... is this really good?

At some point it can be good to convert timestamp values to natural Date javascript objects, but currently it will bring problems more than joy.
Easier is to bring example than describe it:

q = workerDB.query('select '
  + ' current_timestamp AS d1,'
  + ' current_timestamp::timestamp(0) AS d2,'
  + ' current_timestamp :: text AS t');
q.on('row', function (r) {
   util.puts(JSON.stringify(r, null, 3));
   util.puts(r.d1.valueOf());
   util.puts(r.d2.valueOf());
   util.puts(r.d1.toString());
   util.puts(r.d2.toString());
 });

Will output:
{
"d1": "2011-01-21T22:59:45.424Z",
"d2": "2011-01-22T00:59:45.000Z",
"t": "2011-01-22 00:59:45.424245+02"
}
1295650785424
1295657985000
Sat Jan 22 2011 00:59:45 GMT+0200 (EET)
Sat Jan 22 2011 02:59:45 GMT+0200 (EET)
2011-01-22 00:59:45.424245+02

As you can see -- time can change depending in what timezone you are.
Don't get me wrong -- I really like this feature but I put system in live without checking enough and ... boom :)
I think using Date object should be configurable and default should be off (and should be very clearly documentated).
Or turned on ONLY with timestamps with timezones but then it is confusing for user.

PQsendQuery error with native bindings

Hi there,

Firstly, great library. Thanks a lot!

We're experiencing intermittent 'PQsendQuery returned error code' errors when we use the native bindings (possibly some sort of race condition?). I'm happy to help pinpoint the error further, and would really appreciate a point in the right direction.

Here's the full error we get:

Wrror: PQsendQuery returned error code
at Connection._pulseQueryQueue (/Users/simon/app/node_modules/pg/lib/native/index.js:59:10)
at Connection.query (/Users/simon/app/node_modules/pg/lib/native/index.js:26:8)
--- on into our app stack

We don't get the error when we use the non-native bindings.

Connection example using /var/run/postgres

I can connect with "tcp://username:password@localhost/cdbname" but I'd rather use /var/run/postgres (unless there's another way to avoid supplying a password). However:

var pg = require('pg').native;
var client = new pg.Client("/var/run/postgres");
client.connect();

throws:

Error: Need to use node to do async DNS on host
    at /usr/local/lib/node/.npm/pg/0.4.0/package/lib/native.js:29:15
    at Connection.connect (/usr/local/lib/node/.npm/pg/0.4.0/package/lib/native.js:43:3)

Any suggestions?

prepare statement w/o values

Hi!

How do I prepare a statement w/o binding values and executing it, just for future use as named query? The absense of values key in configuration parameters causes unhandled error event so far

TIA,
--Vladimir

Build error + hardcoded homedir

Just saw this while building node-postgres via npm. The package seems to have installed correctly despite the error, but I thought it was worth mentioning since there's a reference to /home/bmc in there.

$ sudo -u root npm --color false install 'pg@==0.3.2' 

npm info it worked if it ends with ok
npm info using [email protected]
npm info using [email protected]
npm info fetch http://registry.npmjs.org/pg/-/pg-0.3.2.tgz
npm info calculating sha1 /tmp/npm-1300347060249/1300347060249-0.926142128650099/tmp.tgz
npm info shasum 19959db07a0761b9a446f7ccea0b65b61103100b
npm info calculating sha1 /usr/local/lib/node/.npm/.cache/pg/0.3.2/package.tgz
npm info shasum 019443c489ddf65ebdcb2010928535e44d05e280
npm info preinstall [email protected]
npm info install [email protected]
Checking for program g++ or c++          : /usr/bin/g++
Checking for program cpp                 : /usr/bin/cpp
Checking for program ar                  : /usr/bin/ar
Checking for program ranlib              : /usr/bin/ranlib
Checking for g++                         : ok
Checking for node path                   : not found
Checking for node prefix                 : ok /usr/local
Checking for program pg_config           : /usr/bin/pg_config
'configure' finished successfully (0.133s)
Waf: Entering directory `/home/bmc/dev/node-postgres/build'
Waf: Leaving directory `/home/bmc/dev/node-postgres/build'
Traceback (most recent call last):
  File "/usr/local/bin/node-waf", line 16, in <module>
    Scripting.prepare(t, os.getcwd(), VERSION, wafdir)
  File "/usr/local/bin/../lib/node/wafadmin/Scripting.py", line 145, in prepare
    prepare_impl(t, cwd, ver, wafdir)
  File "/usr/local/bin/../lib/node/wafadmin/Scripting.py", line 135, in prepare_impl
    main()
  File "/usr/local/bin/../lib/node/wafadmin/Scripting.py", line 188, in main
    fun(ctx)
  File "/usr/local/bin/../lib/node/wafadmin/Scripting.py", line 386, in build
    return build_impl(bld)
  File "/usr/local/bin/../lib/node/wafadmin/Scripting.py", line 405, in build_impl
    bld.compile()
  File "/usr/local/bin/../lib/node/wafadmin/Build.py", line 268, in compile
    os.chdir(self.bldnode.abspath())
OSError: [Errno 2] No such file or directory: '/home/bmc/dev/node-postgres/build'
npm info postinstall [email protected]
npm info preactivate [email protected]
npm info activate [email protected]
npm info postactivate [email protected]
npm info build Success: [email protected]
npm ok

SSL / TLS

Hi,

im working on a project where I need SSL connection to PostgreSQL.
Could you implement this functionality?

Thanks
Paul from Germany

Long-running transaction within a pooled client

What happens when I issue several queries in a long-running transaction within a pooled client?

E.g.

query('BEGIN');
// do something else for a long time
query('COMMIT');

When the system has gone away to process the "do something else", there are no outstanding queries in the client, so shouldn't the client end up issuing the drain event and get checked back into the pool?

If this is an issue, are there workarounds/fixes etc.?

NULL for numeric fields -> NaN

Hi!

On the same setup as in https://gist.github.com/929626 :

create table foo(id bigint, num bigint);
insert into foo(id) values(1);

...
db.query('select * from foo').on('row', function(row){
console.log(row); // ---> {id: 1, num: NaN}
});

Can you confirm the issue?

TIA,
--Vladimir

Feature request: write data to db

Nice library; it works pretty well with very few issues.

One feature I am missing is the ability to write data to the db. It seems that this library can only execute queries against existing data.

My approach in perl is to set up a copy statement, then write data, then execute the statement. I would think this approach would work well in node. Set up a copy statement that returns a stream, load chunks of data into the stream, then trigger an end event.

(I know a patch with this functionality would be better than a feature request, but at the moment I can't do more than this, sorry.)

overloaded client.end

ability to pass true to client.end to have the method wait until the client's internal query queue is emptied before disconnection

client.query('alskdjf')
client.end() //disconnects before query is finished

versus

client.query('lkajsdf');
client.end(true) //disconnects once query is finished

Connection pool broken in Node 0.5.6?

This problem only came up with Node 0.5.6. I send a request to a postgresql database, wait a minute, and then send another request. It breaks.

Try the following code:

var pg = require('pg'),
    util = require('util');
var connectstring = 'pg://postgres:[email protected]/postgres';

function getTheTime() {
  pg.connect(connectstring, function (err, client) {
    client.query('SELECT now() as when', function (err, result) {
      util.log('The postres time is: ' + result.rows[0].when);
    });
  });
}

getTheTime();
setTimeout(getTheTime, 60 * 1000);

The first getTheTime works fine, and the second getTheTime throws the following error:

node.js:203
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: read UNKNOWN
    at errnoException (net_uv.js:557:11)
    at TCP.onread (net_uv.js:326:20)

I am running Node 0.5.6 on Windows. It only happens with a delay between the two requests. Sending requests repeatedly works fine. Therefore, some new feature in Node 0.5.6 must be dropping the connection?

SQL Injection

Hey guys,

Just curious about constructing parameterized queries.....usually this entails some level of mitigation against sql injection attacks. Is that also true for this module? Thanks!

unix socket

better support & documentation for connecting to PostgreSQL server via unix socket

array parser?

Are there any plans to parse arrays? We get arrays from our database and would like to work with arrays in nodejs too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.