Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate DDL programmatically on Postgresql

Tags:

postgresql

ddl

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.

like image 442
ruipacheco Avatar asked Dec 10 '09 23:12

ruipacheco


People also ask

How do I create a DDL script in PostgreSQL?

The utility pg_dump is used to dump the DDL and/or data of the schema and tables. with -s (–schema-only) and -t (–table) option to generate DDL of required tables. The option -s dictates that only DDL has to be generated and the option -t dictates that we want DDL of only tables to be generated.

What is DDL in PostgreSQL?

The DDL (Data Definition Language) commands are used to define the database. Example: CREATE, DROP, ALTER, TRUNCATE, COMMENT, RENAME. The DML (Data Manipulation Language) commands deal with the manipulation of data present in the database. Example: SELECT, INSERT, UPDATE, DELETE.

Does Postgres support DDL?

PostgreSQL. PostgreSQL supports transactional DDL: all DDL commands except “high-caliber” operations aimed at creation and deletion of such objects as DATABASE, TABLESPACE, CLUSTER. PostgreSQL supports multi-level transactions on save points level. Unlike standard SQL, PostgreSQL supports homonymous save points.


1 Answers

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.

like image 128
Brain90 Avatar answered Oct 04 '22 01:10

Brain90