Skip to content

Error message when running code #153

linnhege opened this Issue Jul 20, 2012 · 15 comments

4 participants



I working on setting up my first node.js and postgres app and I keep getting this error message when I try to run my code:

D:\jobb\app>node web.js

client.query('SELECT first_name FROM employees', function(err, result) {
    TypeError: Cannot call method 'query' of null
    at D:\jobb\app\web.js:16:12
    at adjustCallback (D:\jobb\app\node_modules\pg\node_modules\generi
    at (D:\jobb\app\node_modules\pg\node_modul
    at pools.(anonymous function).genericPool.Pool.create.connectError (D:\jobb\
    at g (events.js:185:14)
    at EventEmitter.emit (events.js:88:17)
    at p.connect (D:\jobb\app\node_modules\pg\lib\client.js:106:12)
    at EventEmitter.emit (events.js:88:17)
    at Socket.p.connect (D:\jobb\app\node_modules\pg\lib\connection.js
    at Socket.EventEmitter.emit (events.js:88:17)

Here is my web.js code:

var express = require('express')
    , routes = require('./routes')
    , app = express.createServer(express.logger())
    , pg = require('pg')
    , conString = "pg://user:pass@host:port/dbname"
    , port = process.env.PORT || 3000
    , client;

pg.connect(conString, function(err, client) {
    client.query('SELECT first_name FROM employees', function(err, result) {
        if(err) {
        else {
            console.log("first name: ", result.rows[0].first_name);

Am I doing something wrong or is there a bug in your pg-code?

booo commented Jul 20, 2012

There seams to be an error while you are connecting to your database. Please check for an error in the pg.connect function.


Its a red line under 'events' in the line:
var EventEmitter = require('events').EventEmitter;

Its in the file node_modules\pg\lib\index.js

brianc commented Jul 25, 2012

You're experiencing an issue connecting to your database. You need to handle the issue in your code. If you handle the error and still think it's related to node-postgres please let us know.

Do this:

pg.connect(conString, function(err, client) {
  if(err) throw err;



Did that and got the error message "error: invalid startup packet layout: expected terminator as last byte".

I looked around a bit and found a similar issue here.

I implemented the fix, but then I got a new error message:
"error: no pg_hba.conf entry for host "host", user "username", database "dbname", SSL off"

I saw in this issue that you recommended to try using require('pg').native, so I did that. It did make the error message go away, but I got a new one instead:

Error: Cannot find module 'D:\jobb\app\node_modules\pg\lib/../build/de
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (D:\jobb\app\node_modules\pg\lib\native.js:5
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)

That error message got referenced in this issue, but the only fix that came up there was to upgrade to node v0.8.1, which is the version I have.

Here is my dependencies:

"dependencies": {
   "pg": "0.5.4",
   "express": "2.5.x"
"engines": {
    "node": "0.8.x",
    "npm":  "1.1.x"

I'm at a loss here.

brianc commented Jul 26, 2012

what version of postgres are you using? The invalid terminator at last byte is an old and definitely fixed bug. Also, if you're running node v0.8.1 don't use pg version 0.5.4. Use the most recent version from npm.

brianc commented Jul 26, 2012

Yeah, just checked. pg 0.5.4 is extremely out of date. Please upgrade to either "pg": "*" or "pg": "0.8.1"

@brianc brianc closed this Jul 26, 2012

Ah, I thought that was the newest version since that was the version npm installed. Updated to 0.8.1 now, but it didnt really help in any other way than to give me a bit more detailed error message:

    if(err) throw err;
error: no pg_hba.conf entry for host "host", user "username", data
base "dbname", SSL off
    at p.parseE (D:\jobb\app\node_modules\pg\lib\connection.js:412:11)

    at p.parseMessage (D:\jobb\app\node_modules\pg\lib\connection.js:2
    at Socket.p.connect (D:\jobb\app\node_modules\pg\lib\connection.js
    at Socket.EventEmitter.emit (events.js:88:17)
    at TCP.onread (net.js:403:14)

There is red lines under the words inside the apostrophes in the first lines in the file D:\jobb\app\node_modules\pg\lib\connection.js, does that tell you anything?

var net = require('net');
var crypto = require('crypto');
var EventEmitter = require('events').EventEmitter;
var util = require('util');

Also, the connect function (pg.connect(conString, function(err, client) {}) goes to the file D:\jobb\app\node_modules\pg\test\integration\connection\test-helper.js, should it do that? It doesnt seem logical to use a function in a test folder.

I got an error message when I installed pg:

D:\jobb\app>npm install pg
npm WARN package.json app@0.0.1 No file found!
npm WARN package.json jade@0.26.3 No file found!
npm http GET
npm http 304
npm http GET
npm http 304

> pg@0.8.1 install D:\jobb\app\node_modules\pg
> node-gyp rebuild || (exit 0)

D:\jobb\app\node_modules\pg>node "C:\Program Files\nodejs\node_modules
\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild
gyp http GET
gyp http 200
gyp http GET
gyp http GET
gyp http 200
gyp http 200
c:\users\linn hege\.node-gyp\0.8.4\src\node_object_wrap.h(57): warning C4251: '
node::ObjectWrap::handle_' : class 'v8::Persistent<T>' needs to have dll-interf
ace to be used by clients of class 'node::ObjectWrap' [D:\jobb\app\no
..\src\ warning C4800: 'int' : forcing value to bool 'true' or
'false' (performance warning) [D:\jobb\app\node_modules\pg\build\bind
..\src\ error C2065: 'EV_ERROR' : undeclared identifier [D:\job
..\src\ error C2065: 'EV_READ' : undeclared identifier [D:\jobb
..\src\ error C2065: 'EV_WRITE' : undeclared identifier [D:\job
gyp ERR! build error
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\
gyp ERR! stack     at ChildProcess.EventEmitter.emit (events.js:91:17)
gyp ERR! stack     at Process._handle.onexit (child_process.js:674:10)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modu
les\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd D:\jobb\app\node_modules\pg
gyp ERR! node -v v0.8.4
gyp ERR! node-gyp -v v0.6.1
gyp ERR! not ok
pg@0.8.1 node_modules\pg
+-- generic-pool@1.0.9

But you did write in issue 146 that as long as pure JavaScript bindings was used it wasnt a problem.

And to answer your question, I have postgres version 9.1 installed.

booo commented Jul 27, 2012

@linnhege are you sure you are using the right connection string? Have you configured postgres for the user you use in the connection string?

@brianc brianc reopened this Jul 27, 2012
brianc commented Jul 27, 2012

That error message means it cannot find your credentials in your pg_hba.conf file -- meaning you're using the wrong credentials to log in.

What version of node are you using?

Do you have a configuration entry for the user not requiring SSL? I am still working on getting SSL support into the pure javascript bindings.

Can you gist the code causing an issue?


I'm using Heroku so SSL is required for all connections. I copy/pasted the credentials from the connection settings on Heroku's site so they should be correct. It works perfectly fine when I connect to the database with pgAdmin and psql.

I'm using v0.8.4 of Node.

Is this fine?

There I tried to just use your example code, which gave me this error message:

Error! { [error: no pg_hba.conf entry for host "some-ip-I-dont-recognize", user "username", database "dbname", SSL off]
  length: 159,
  name: 'error',
  severity: 'FATAL',
  code: '28000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'auth.c',
  line: '483',
  routine: 'ClientAuthentication' }
freewil commented Aug 15, 2012

I'm using Heroku so SSL is required for all connections.

node-postgres doesn't support SSL yet


Oh, that sucks.

Brian, do you think this functionality will be implementet soon, or should I start looking for other solutions?

brianc commented Aug 15, 2012

It's number 1 on my list of things to support. Been really busy past 2 weeks w/ personal stuff -- hopefully this weekend I'll have it in. In the mean time node-postgres does support SSL if you use the native bindings. Just change require('pg') to require('pg').native and you'll be off to the races.

brianc commented Aug 15, 2012

this, in particular, is what I plan on merging in - really I just need to test it more extensively (i.e. integration tests)


booo commented Aug 28, 2014

This should be implemented by now. I'm closing the ticket therefore.

@booo booo closed this Aug 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.