pg-promise set schema NodeJs

你。 提交于 2019-12-12 17:08:47

问题


I am using pg-promise to work with nodeJS and postgres, but when I try to query my User table, it retrieves the superUser. I think it is related to the fact that I don't have a schema set yet, so the code searches on the default 1. How can I set the schema on the code?

I tried this:

var express = require('express');
var app = express();
const PORT = 8080;

var pgp = require('pg-promise')(/*options*/)
var db = pgp('postgres://postgres:randompassword@localhost:5432/appname')

app.listen(PORT,function() {
    console.log("listening to port: " + PORT);
})

db.any('SELECT * FROM User')
    .then(function(data) {
        console.log(data);
    })
    .catch(function(error) {
        console.log(error);
    });

回答1:


Your problem is not the schema, it is the table name.

User (case-insensitive) is a reserved reference to the currently logged in database user.

The best is not to use a table with such name, or use plurified - Users, then you won't have any problem.

But if you must use the table with name User then you will always have to put it in double quotes - "Users" (case-sensitive), so the server can tell the difference.



来源:https://stackoverflow.com/questions/43920483/pg-promise-set-schema-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!