Home > Back-end >  Is it possible to rename a table with the actual timestamp in the tablename within a query
Is it possible to rename a table with the actual timestamp in the tablename within a query

Time:04-22

select (now();

just like so.. but it doesn't works:

ALTER TABLE old_name  RENAME TO new_name || select now();

CodePudding user response:

You can try to use RENAME and SET DEFAULT

ALTER TABLE T
  RENAME old_name TO new_name;
  
ALTER TABLE T
ALTER COLUMN new_name  
  SET DEFAULT now();

sqlfiddle

CodePudding user response:

You need a dynamic command in a function or do block, e.g.:

do $$
begin
    execute format('alter table old_name rename to "new_name_%s"', now());
end $$;
  • Related