如何在表名中包含当前时间 - postgresql

时间:2017-01-31 21:04:35

标签: sql postgresql backup

有没有办法可以更改此sql语句,以便生成的表具有作为表名的一部分烘焙的时间戳:

CREATE TABLE public.mytesttable (LIKE  public.tabletobackup INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES);

所以而不仅仅是

 mytesttable

我想要像

这样的东西
 mytesttable_20170131151515

我想在同一个数据库中备份一个表...我已被要求在表名中包含当前日期/时间。

任何提示将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

像这里一样使用动态sql:

vao=# do $$ begin execute format('create table "date_%s" (i int)',now()::date); end; $$;
DO
vao=# \dt+ "date_2017-01-31"
                        List of relations
 Schema |      Name       | Type  | Owner |  Size   | Description
--------+-----------------+-------+-------+---------+-------------
 public | date_2017-01-31 | table | vao   | 0 bytes |
(1 row)