1

I get the following error during my SELECT query from PostgreSQL DB

ERROR: { error: relation "free_subnets" does not exist
    at Connection.parseE (/home/ec2-user/environment/node_modules/pg/lib/connection.js:604:11)
    at Connection.parseMessage (/home/ec2-user/environment/node_modules/pg/lib/connection.js:401:19)
    at Socket.<anonymous> (/home/ec2-user/environment/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  name: 'error',
  length: 103,
  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: 'parse_relation.c',
  line: '874',
  routine: 'parserOpenTable' }

If I use psql command and do a select there, it works.

\dt provides me the relation:


   Schema    |     Name     | Type  |  Owner   
-------------+--------------+-------+----------
 subnet_calc | free_subnets | table | postgres

Search_path has been set to: subnet_calc, public

I've created the table with user postgres, so I am the owner of the schema subnet_calc and the table free_subnets.

My dummy nodejs code is the following:

const pg = require('pg');


const cs = 'postgres://postgres:password@anydb.rds.amazonaws.com:5432/subnet_calculator';

const client = new pg.Client(cs);
client.connect();

client.query('SELECT * FROM subnet_calc.free_subnets', function(result) {
  console.log(result);
});

What could be the issue?

Thanks for your help in advance!

N P
  • 21
  • 3
  • Please avoid posting screenshots if the content is just text. Copy/paste it, so it's easier to search for and examine. – VLAZ Feb 17 '20 at 17:01
  • Please post text, not links of images to text. Please see the [How to Ask](https://stackoverflow.com/help/how-to-ask) page. – Dave Newton Feb 17 '20 at 17:01
  • Hi VLAZ and Dave Newton, I've added texts instead of screenshots. – N P Feb 17 '20 at 17:14

2 Answers2

1

I could resolve the issue. It was my failure, the tables were created in DB postgres not in subnet_calculator.

I will close this question.

N P
  • 21
  • 3
0

you need to set search path for it like this:

db.query("SET search_path TO 'subnet_calc';")

and then do the query :

const query = `SELECT * FROM free_subnets`

db.query(query)
Raj Verma
  • 1,050
  • 1
  • 7
  • 19
  • Hi Raj,Thanks for your reply. Unfortunately, it does not work. Actually, SEARCH_PATH was already set to that. – N P Feb 17 '20 at 18:38
  • how did you set the search path? What i'm suggesting is try setting it separately and then run the query. See what results you get. – Raj Verma Feb 18 '20 at 04:55
  • ``` const pg = require('pg'); const cs = 'postgres://postgres:password@anyhost:5432/anydb'; const client = new pg.Client(cs); client.connect(); client.query("SET search_path TO 'subnet_calc';") client .query('SELECT * from free_subnets;') .then(res => console.log(res.rows[0])) .catch(e => console.error(e.stack)) ``` I don't get your point. I've tried it and with the above mentioned code and it does not work. – N P Feb 18 '20 at 07:12