Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function: db_to_csv(text)
- -- DROP FUNCTION db_to_csv(text);
- CREATE OR REPLACE FUNCTION db_to_csv(path text)
- RETURNS void AS
- $BODY$
- declare
- tables RECORD;
- statement TEXT;
- begin
- FOR tables IN
- SELECT (table_schema || '.' || table_name) AS schema_table
- FROM information_schema.tables t INNER JOIN information_schema.schemata s
- ON s.schema_name = t.table_schema
- WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
- AND t.table_type NOT IN ('VIEW')
- ORDER BY schema_table
- LOOP
- statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER ENCODING ''UTF8''';
- EXECUTE statement;
- END LOOP;
- return;
- end;
- $BODY$
- LANGUAGE plpgsql VOLATILE
- COST 100;
- ALTER FUNCTION db_to_csv(text)
- OWNER TO postgres;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement