Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. -- Function: db_to_csv(text)
  2.  
  3. -- DROP FUNCTION db_to_csv(text);
  4.  
  5. CREATE OR REPLACE FUNCTION db_to_csv(path text)
  6. RETURNS void AS
  7. $BODY$
  8. declare
  9. tables RECORD;
  10. statement TEXT;
  11. begin
  12. FOR tables IN
  13. SELECT (table_schema || '.' || table_name) AS schema_table
  14. FROM information_schema.tables t INNER JOIN information_schema.schemata s
  15. ON s.schema_name = t.table_schema
  16. WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
  17. AND t.table_type NOT IN ('VIEW')
  18. ORDER BY schema_table
  19. LOOP
  20. statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER ENCODING ''UTF8''';
  21. EXECUTE statement;
  22. END LOOP;
  23. return;
  24. end;
  25. $BODY$
  26. LANGUAGE plpgsql VOLATILE
  27. COST 100;
  28. ALTER FUNCTION db_to_csv(text)
  29. OWNER TO postgres;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement