Wednesday, 7 June 2017

Connection Terminated when querying postgres

I am unable to query my database using pg. I am ssh'd to a remote machine(where the DB is) running Ubuntu, the same as my machine, and hosting a server using express to display the information i get from the database in JSON format to an unmodified web page. The exact error i get is:

Error: Connection terminated
at Connection.<anonymous> (@mydir/node_modules/pg/lib/client.js:194:17)
at emitNone (events.js:105:13)
at Connection.emit (events.js:207:7)
at Socket.<anonymous> (@mydir/node_modules/pg/lib/connection.js:141:10)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)

My Script:

const express = require('express');
const router = express();
const path = require('path');
const connectionString = 'postgres://@connectionInfo';
const pg = require('pg');




router.get('/', (request, response, next) =>{
    const content = [];

    pg.connect(connectionString, (err, client, done)=> {
        if(err){
            done();
            console.log(err);
            return response.status(500); //send a 500 if we cant connect.
        }

        const query = client.query('SELECT * FROM @databse'); //queries go here

        query.on('row', (row) =>{
                content.push(row);

        });


        query.on('end', () =>{

            done();
            return response.json(content); //returns the content in json format

        });


    });

});

module.exports = router;



via lwatkins

No comments:

Post a Comment