Generate DDL programmatically on Postgresql Generate DDL programmatically on Postgresql postgresql postgresql

Generate DDL programmatically on Postgresql


Use pg_dump with this options:

pg_dump -U user_name -h host database -s -t table_or_view_names -f table_or_view_names.sql

Description:

-s or --schema-only : Dump only ddl / the object definitions (schema), without data.-t or --table Dump :  Dump only tables (or views or sequences) matching table

Examples:

-- dump each ddl table elon build.$ pg_dump -U elon -h localhost -s -t spacex -t tesla -t solarcity -t boring > companies.sql

Sorry if out of topic. Just wanna help who googling "psql dump ddl" and got this thread.


You can use the pg_dump command to dump the contents of the database (both schema and data). The --schema-only switch will dump only the DDL for your table(s).


The answer is to check the source code for pg_dump and follow the switches it uses to generate the DDL. Somewhere inside the code there's a number of queries used to retrieve the metadata used to generate the DDL.