Skip to content

Instantly share code, notes, and snippets.

@rocksfrow
rocksfrow / clone_schema_fn.sql
Last active July 13, 2022 10:19
enhanced postgresql clone_schema() function with trigger + foreign key support
CREATE OR REPLACE FUNCTION clone_schema(source_schema text, dest_schema text) RETURNS void AS
$$
DECLARE
object text;
buffer text;
default_ text;
column_ text;
constraint_name_ text;
constraint_def_ text;
@rocksfrow
rocksfrow / fetch_schema_grants_fn.sql
Last active August 29, 2015 14:04
plPgsql function to output grant SQL to give user access to entire schema
create or replace function fetch_schema_grants(_user text, _schema text) RETURNS TABLE(query text) as
$func$
begin
RETURN QUERY select 'grant all on schema '||_schema||' to '||_user||';' union all select 'grant all on sequence '||sequence_schema||'.'||sequence_name||' to '||_user||';' from information_schema.sequences where sequence_schema=_schema union all select 'grant all on '||schemaname||'.'||tablename||' to '||_user||';' from pg_tables where schemaname=_schema;
end
$func$ language plpgsql;