Created
November 19, 2013 13:17
-
-
Save jllodra/7545222 to your computer and use it in GitHub Desktop.
PostgreSQL: create one .csv per db table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO LANGUAGE plpgsql $$ | |
declare | |
tables RECORD; | |
statement TEXT; | |
path TEXT := '/home/user/destination_dir'; | |
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', 'configuration') | |
ORDER BY schema_table | |
LOOP | |
statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '';'' CSV HEADER'; | |
EXECUTE statement; | |
END LOOP; | |
return; | |
end; | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment