I use pg module to connect postgres database to node.js project.
const pg = require('pg')
const pool = pg.Pool(
{
"user":"postgres",
"password":"password",
"host": "localhost",
"db" : "db1"
});
pool.connect();
pool.on('connect',(client)=>
{
client.query("insert into table1(id, name) values(1, "item1")")
client.query("select * from table 1",(err, res)=>{
if( err) throw err;
console.log(res);
});
}
)
My sql code is in a file called script.sql. My code is very similar to above and I want to read from script.sql file rather than putting sql code inside query function like client.query("insert into table1(id, name) values(1, 'item1')") . Is there a way to do it in pg module or do you suggest an effective way to read from script in node-postgres.
readFileSync
before the server started orreadFile
after the server started.