I have a timestamp with timezone
column in postgressql, named created
, that the data are saved in UTC.
I am using pg-promise 8.4.4 and I want to select the data, but set a different timezone for every client.
The logic is that my angular app uses the node.js endpoints as an api and I guess I can set the timezone while creating a DB connection, but that would be good just for my app.
What if another app, from another timezone, wants to hit the same node endpoint? How can they set the timezone?
So, in SQL I can do something like
set timezone to 'europe/athens';
select created from table;
But, how do I do this in pg-promise, for different clients?
I could do something like
cmsuser.generalQuery(`select firstname, created AT TIME ZONE '+02' from table WHERE AND activated = $1 ORDER BY created`, [true])
.then((resolved)=>{
res.json({success:true, activated:resolved});
})
and make the +02
part dynamic?
The cmsuser.generalQuery
is
const generalQuery = (text, params) => {
const s = pgp.as.format(text, params);
return db.any(s)
}
There are two problems with this approach right now
-1- With the above query, without having the timezone part as dynamic, I get the firstname
, but not the created
, that is the date. No errors in the console.
-2- Setting the timezone per query is not a smart thing to do.
What can I do here?
Thank you