pg-promise set schema NodeJs

2019-07-07 14:57发布

问题:

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.