如何在Postgresql上以编程方式生成表的DDL?是否有系统查询或命令来执行此操作?谷歌搜索问题没有返回指针.
Bra*_*n90 35
pg_dump与此命令一起使用:
pg_dump -U user_name -h host database -s -t table_or_view_names -f table_or_view_names.sql
Run Code Online (Sandbox Code Playgroud)
描述:
-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
Run Code Online (Sandbox Code Playgroud)
例子:
-- dump each ddl table that elon build.
$ pg_dump -U elon -h localhost -s -t spacex -t tesla -t solarcity -t boring > companies.sql
Run Code Online (Sandbox Code Playgroud)
对不起,如果超出主题.只想帮助谁搜索"psql dump ddl"并重定向到此线程.
为什么 shell 到 psql 不算“以编程方式”?它会很好地转储整个模式。
无论如何,您可以从information_schema获取数据类型(以及更多)(此处引用的 8.4 文档,但这不是一个新功能):
=# select column_name, data_type from information_schema.columns
-# where table_name = 'config';
column_name | data_type
--------------------+-----------
id | integer
default_printer_id | integer
master_host_enable | boolean
(3 rows)
Run Code Online (Sandbox Code Playgroud)
我保存了 4 个函数来部分模拟pg_dump -s行为。基于\d+元命令。用法是类似的:
\pset format unaligned
select get_ddl_t(schemaname,tablename) as "--" from pg_tables where tableowner <> 'postgres';
Run Code Online (Sandbox Code Playgroud)
当然,您必须事先创建函数。
| 归档时间: |
|
| 查看次数: |
47083 次 |
| 最近记录: |