Guest User

Untitled

a guest
Dec 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. /*
  2. 1. Log in to psql as a superuser
  3. 2. CHMOD target directory to 777
  4. 3. Execute via SELECT db_to_csv('/path/to/output');
  5. 4. Use complete file path.
  6. */
  7.  
  8. CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
  9. declare
  10. tables RECORD;
  11. statement TEXT;
  12. begin
  13. FOR tables IN
  14. SELECT (table_schema || '.' || table_name) AS schema_table
  15. FROM information_schema.tables t INNER JOIN information_schema.schemata s
  16. ON s.schema_name = t.table_schema
  17. WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
  18. AND t.table_type NOT IN ('VIEW')
  19. ORDER BY schema_table
  20. LOOP
  21. statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER';
  22. EXECUTE statement;
  23. END LOOP;
  24. return;
  25. end;
  26. $$ LANGUAGE plpgsql;
Add Comment
Please, Sign In to add comment