如何在 Postgresql 上以编程方式生成表的 DDL?是否有系统查询或命令来执行此操作?谷歌搜索这个问题没有返回任何指针。
问问题
77237 次
6 回答
48
pg_dump
与此选项一起使用:
pg_dump -U user_name -h host database -s -t table_or_view_names -f table_or_view_names.sql
描述:
-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
例子:
-- dump each ddl table elon build.
$ pg_dump -U elon -h localhost -s -t spacex -t tesla -t solarcity -t boring > companies.sql
对不起,如果超出主题。只是想帮助那些在谷歌上搜索“psql dump ddl”并得到这个线程的人。
于 2012-02-14T03:18:51.783 回答
24
您可以使用该pg_dump
命令转储数据库的内容(模式和数据)。该--schema-only
开关将只转储您的表的 DDL。
于 2009-12-10T23:30:46.213 回答
3
答案是检查 pg_dump 的源代码并遵循它用来生成 DDL 的开关。在代码的某处,有许多查询用于检索用于生成 DDL 的元数据。
于 2010-01-27T11:23:31.450 回答
3
为什么对 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)
于 2010-01-27T12:55:13.103 回答
2
这是一篇关于如何从信息模式中获取元信息的好文章, http://www.alberton.info/postgresql_meta_info.html。
于 2010-08-09T08:35:32.833 回答
2
我保存了 4 个函数来部分模拟pg_dump -s
行为。基于\d+
元命令。用法会很相似:
\pset format unaligned
select get_ddl_t(schemaname,tablename) as "--" from pg_tables where tableowner <> 'postgres';
当然,您必须事先创建函数。
于 2017-03-06T16:15:54.300 回答