I have a MySQL query that needs to be converted over to PostgreSQL. As I understand, PostgreSQL has no UTC_TIMESTAMP() or DATE_ADD() functions available. After reading the docs, I'm not entirely certain which date functions solve my needs in this context. I'm a bit new to PostgreSQL so apologies if my question is noobish!
Here's the query:
INSERT INTO snippets (title, content, created, expires)
VALUES(?, ?, UTC_TIMESTAMP(), DATE_ADD(UTC_TIMESTAMP(), INTERVAL ? DAY))
For reference, here is my snippets model:
CREATE TABLE snippets (
id BIGSERIAL NOT NULL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP(0) NOT NULL,
expires TIMESTAMP(0) NOT NULL
);
CodePudding user response:
I've not done much with postgresql either, but maybe something like:
INSERT INTO snippets (title, content, created, expires)
VALUES('test', 'stuff', (now() at time zone 'utc'), (now() at time zone 'utc' interval '1 day' * 3))
Which gives:
id title content created expires
1 test stuff 2022-11-23 13:02:13 2022-11-26 13:02:13
Then to restore the parameterized version of the query:
INSERT INTO snippets (title, content, created, expires)
VALUES(?, ?, (now() at time zone 'utc'), (now() at time zone 'utc' interval '1 day' * ?))
I was not sure how to test the parameterized equivalent, but here's a dbfiddle example with the output from the first query.