Skip to content

Instantly share code, notes, and snippets.

@ndastur
Last active February 23, 2020 01:39
Show Gist options
  • Save ndastur/732117eabe9b8895389986a43dabc3c3 to your computer and use it in GitHub Desktop.
Save ndastur/732117eabe9b8895389986a43dabc3c3 to your computer and use it in GitHub Desktop.
Output list of Postgresql tables and their fields on a single bash line
PGUSER=<user> PGPASSWORD=<pw> PGHOST=<host> PGDATABASE=<database> bash -c 'psql -Atc "select tablename from pg_tables where schemaname='\''public'\''" $PGDATABASE | while read TBL; do echo \"$TBL\": { \"keyFields\": [ ; psql -c "COPY (select * from public.$TBL where false) TO STDOUT WITH (FORMAT CSV, HEADER, DELIMITER '\'','\'')" $PGDATABASE | sed '\''s/[^,]*/"&"/g'\'' ; echo ] }, ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment