Generate DDL programmatically on Postgresql

ruipacheco picture ruipacheco · Dec 11, 2009 · Viewed 61.6k times · Source

How can I generate the DDL of a table programmatically on Postgresql? Is there a system query or command to do it? Googling the issue returned no pointers.

Answer

Brain90 picture Brain90 · Feb 14, 2012

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 that 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.