Skip to content

Instantly share code, notes, and snippets.

@slabad
Last active June 12, 2020 19:00
Show Gist options
  • Save slabad/d7892f275572700d6a6173dfd5a22f0f to your computer and use it in GitHub Desktop.
Save slabad/d7892f275572700d6a6173dfd5a22f0f to your computer and use it in GitHub Desktop.
Dynamic Table Creation #psql #postgres
DO
$$
BEGIN
EXECUTE format('CREATE TABLE %I AS SELECT * FROM backup', 'backup_' || to_char(CURRENT_DATE,'yyyy-mm-dd'));
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION fn_hello(v_table text)
returns void
AS $$
BEGIN
EXECUTE format('create table rollback.%I (id int, name text)', v_table || '_' || to_char(CURRENT_DATE, 'yyyymmdd'));
END;
$$
LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment