I’m getting a syntax error for the date created_on for Postgresql Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of I’m getting a syntax error for the date created_on for Postgresql without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to insert the date it was created on into a table and I’m getting an error, here is the code:


const cs = 'site';

const client = new Pool({
  cs,
  user: 'user',
  host: 'host',
  database: 'database',
  password: 'password',
  port: 5432,
  ssl: {
    rejectUnauthorized: false
  }
});
//to get rid of the T
var d = new Date().toISOString().replace(/T/, " ");
//to get rid of the .Z something
var e= d.substr(0, 19);
console.log(e);
const value= `INSERT INTO price(created_on,something1, something2)VALUES(${e},1,1)`;
client.query(
 value,
  (err, res) => {
    console.log(err, res);
    client.end();
  }
);

when I look at the result in the console it says 2021-03-14 16:25:05, yet it gives me a

error: syntax error at or near “16”

What am I doing wrong?

Answer

you need to quote the date value :

const value= `INSERT INTO price(created_on,something1, something2)VALUES('${e}',1,1)`;
We are here to answer your question about I’m getting a syntax error for the date created_on for Postgresql - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji