node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Error: Stream unexpectedly ended during query execution

Open EdaCZ opened this issue 12 years ago • 2 comments

Hi.

When I connect to PgSQL database as in doc:

conString = "postgres://USER:PASS@HOST/DB"

client = new pg.Client(conString)

client.connect (err) ->
    if err
        return console.error 'could not connect to postgres', err

    client.query('SELECT 1', (err, result) ->
        if err
            return console.error('error running query', err)

        console.log(result)
    )
    client.end()

it already connects to database, but it throws error on query execution:

error running query [Error: Stream unexpectedly ended during query execution]

Credentials is right, in PHP it works well and if they would have been wrong, it would throw me connection error.

I tryed googling but it appears that I am the first one, which is searching for this error.

(node-postgres 2.11)

Any ideas? Thx.

EdaCZ avatar Jan 13 '14 14:01 EdaCZ

You are ending the client before the query returns. The call to client.query is non-blocking, so it will not necessarily finish before the next line is executed. Try moving the call to client.end() inside the client.query callback function.

benighted avatar Jan 13 '14 19:01 benighted

Thank you. It helped :smile:

EdaCZ avatar Jan 14 '14 08:01 EdaCZ