Node.js 从带有NodeJS的表中选择*//错误:{错误:关系“mytable”不存在
我在从PostgreSQL数据库执行SELECT查询时遇到以下错误Node.js 从带有NodeJS的表中选择*//错误:{错误:关系“mytable”不存在,node.js,postgresql,select,search-path,Node.js,Postgresql,Select,Search Path,我在从PostgreSQL数据库执行SELECT查询时遇到以下错误 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_mo
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' }
搜索路径已设置为:子网计算,公共
我用用户postgres创建了表,因此我是模式子网\u calc和表空闲\u子网的所有者
我的虚拟节点代码如下所示:
const pg=require('pg');
常数cs=postgres://postgres:password@anydb.rds.amazonaws.com:5432/subnet_calculator';
const client=新的pg.client(cs);
client.connect();
client.query('SELECT*FROM subnet\u calc.free\u subnets',函数(结果){
控制台日志(结果);
});
可能是什么问题
提前感谢您的帮助!您需要如下设置搜索路径:
db.query("SET search_path TO 'subnet_calc';")
然后执行查询:
const query = `SELECT * FROM free_subnets`
db.query(query)
我可以解决这个问题。这是我的失败,表是在DB postgres中创建的,而不是在subnet_calculator中创建的
我将结束此问题。如果内容只是文本,请避免发布截图。复制/粘贴它,以便更容易搜索和检查。请发布文本,而不是图像到文本的链接。请查看页面。嗨,VLAZ和Dave Newton,我添加了文本而不是截图。嗨,Raj,谢谢你的回复。不幸的是,它不起作用。A实际上,SEARCH_PATH已设置为该值。您是如何设置搜索路径的?我的建议是尝试单独设置它,然后运行查询。查看您得到的结果。```const pg=require('pg');const cs='1!'postgres://postgres:password@anyhost:5432/anydb';const client=new pg.client(cs);client.connect();client.query(“将搜索路径设置为‘subnet_calc’”)client.query('SELECT*from free_subnets;')。然后(res=>console.log(res.rows[0])。catch(e=>console.error(e.stack))``我不明白你的意思。我已经用上面提到的代码尝试了它,但它不起作用。这有点悲伤和有趣,但也是一个合理的原因。祝你好运!
const query = `SELECT * FROM free_subnets`
db.query(query)