Sumit Srivastava
Sumit Srivastava

Reputation: 73

Node.js and PostgresSQL with Express

I've written a code to fetch the data from postgres database using express and NODE.js. the database is connected with the backend code but still it is showing error like relation does not exit. Please check the code and error that I got:

var express = require('express');
var pg = require("pg");
var app = express();

var connectionString = "postgres://postgres:postgres@localhost:5432/testDatabase?currentSchema=testSchema";

app.get('/', function(req, res, next) {
    pg.connect(connectionString, function(err, client, done) {
        if (err) {
            console.log("Not able to get Connection " + err);
            res.status(400).send(err);
        } else {
            console.log("Connected.!");
        }

        const
        query = {
            // give the query a unique name
            name : 'fetch-user-details',
            text : 'SELECT * FROM loginTable',
        }

        client.query(query, function(err, result) {
            done(); // closing the connection;
            if (err) {
                console.log(err);
                res.status(400).send(err);
            }
            res.status(200).send(result.rows);
        });
    });
});

app.listen(4000, function() {
    console.log('Server is running.. on Port 4000');
});

and the error response that I got is like:

Server is running.. on Port 4000
Connected.!
{ error: relation "logintable" does not exist
    at Connection.parseE (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:554:11)
    at Connection.parseMessage (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:381:17)
    at Socket.<anonymous> (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:117:22)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at TCP.onread (net.js:594:20)
  name: 'error',
  length: 120,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: '15',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'src\\backend\\parser\\parse_relation.c',
  line: '986',
  routine: 'parserOpenTable' }

Upvotes: 1

Views: 146

Answers (1)

Naor Tedgi
Naor Tedgi

Reputation: 5717

just run this db dump at my Postgres

CREATE SCHEMA "testSchema"; 
ALTER SCHEMA "testSchema" OWNER TO postgres; 
SET search_path = "testSchema", pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false; 
CREATE TABLE "loginTable" ( "ID" numeric NOT NULL, "Name" text ); 
ALTER TABLE "loginTable" OWNER TO postgres; 

this was the problem

change your query to this

 query = {
            // give the query a unique name
            name : 'fetch-user-details',
            text : `SELECT * FROM "testSchema".logintable`,
        }

you should mention the schema name

tasted on

  • Ubuntu
  • postgres 10.6
  • pg: 6.4.2
  • node v10.14.1

Upvotes: 1

Related Questions