I want to truncate a table in PostgreSQL 13 and then insert rows to the same table within a single transaction, so that when I execute the SQL command it will TRUNCATE the table and if during INSERTS anything fails truncated data should be rollback.
Below is the command which I want in a single transaction.
TRUNCATE TABLE public.truninsdemo;
INSERT INTO public.truninsdemo (id, name)
VALUES (1, 'Scott'), (2, 'John');
UPDATE
I don't want to execute TRUNCATE and INSERT statements as 2 separate commands, separated by a semicolon. I want to achieve results using a single command may be CTE, but not sure how.