Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 17th, 2017 47 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top