1
votes

I've created a table with the name of user in postgres which contains id, name, family columns (id is auto-increment). The query which I want to insert into the postgres likes the following:

var config = {
    user: 'username',
    database: 'databaseName',
    password: 'password',
    host: 'localhost',
    port: 5432,
    max: 10,
    idleTimeoutMillis: 30000
};

const pool = new postgre.Pool(config);
let query = "INSERT INTO user(name, family) VALUES ('name', 'family')";
pool.query(query, [], function (err, result) {
    if (err) {
        return console.error("error running query", err);
    }
    console.log("done!");        
});

But, I've got the following error:

Syntax error near user

Also, I have tried different approach to send the request liked the following:

const connectionString = "http://localhost:5432/databaseName?username=username&password=password";
const client = new postgre.Client(connectionString);
client.connect();
const query = client.query("INSERT INTO user(id, name, family) VALUES ('name', 'family')");
query.then(x => { console.log("done!")});
query.catch(x => { console.log(x)});

But I've got the following error:

role \"UserName\" does not exist

Also, the UserName (name of the server) in the body of the error is not the same as the username of the database.

I've checked the all possible fallacies such as the correctness of the username and password.

Therefore, the question is what is the problem?

2

2 Answers

1
votes

All of these problems come from the table name. As mentioned in this post, the name of user is a reserved name in postgres. Therefore, these errors will be passed if change the name of the user table such as users.

1
votes

user is a reserved word in PostgreSQL that refers to the currently logged in user. For that reason, in order to be able to use a table with name user, the name must always be inside double quotes - "user".