Skip to content

Instantly share code, notes, and snippets.

@dctalbot
Last active November 19, 2023 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dctalbot/b37f8ae6ab03380c3bbf8bdef9041764 to your computer and use it in GitHub Desktop.
Save dctalbot/b37f8ae6ab03380c3bbf8bdef9041764 to your computer and use it in GitHub Desktop.
A psql command that dumps a postgres database into CSV files, 1 per table.
psql -Atc "select tablename from pg_tables where schemaname='$SCHEMA'" $DB |\
while read TBL; do
psql -c "COPY $SCHEMA.$TBL TO STDOUT WITH CSV HEADER" $DB > $TBL.csv
done
@dctalbot
Copy link
Author

Of course you need to set the right credentials info via a connection string or environment variables e.g. export PGPASSWORD=foo. Plus you can set SCHEMA and DB, for the schema/database pair you want to dump. Hope this helps someone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment