Skip to content

Instantly share code, notes, and snippets.

@ssaid
Created March 10, 2023 17:54
Show Gist options
  • Save ssaid/e711082f9a757e8730ed6ca2ee996387 to your computer and use it in GitHub Desktop.
Save ssaid/e711082f9a757e8730ed6ca2ee996387 to your computer and use it in GitHub Desktop.
Dump data and schema from PostgreSQL DB
#!/bin/bash
DBNAME="dbname"
tables=$(psql -d $DBNAME -t -c "SELECT table_name FROM information_schema.tables WHERE table_schema='public' and table_type='BASE TABLE'"|cut -d' ' -f2)
while IFS= read -r table; do
pg_dump --schema-only -t $table $DBNAME > $table.schema
pg_dump --data-only -t $table $DBNAME > $table.data
done <<< "$tables"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment