Joh*_*ohn 16 sql postgresql ddl
我需要PostgreSQL数据库对象的创建脚本.
我无法访问pg_dump.所以我必须通过SQL查询获得所有内容.我怎么能这样做?
a_h*_*ame 36
要获得函数的定义,请使用pg_get_functiondef()
:
select pg_get_functiondef(oid)
from pg_proc
where proname = 'foo';
Run Code Online (Sandbox Code Playgroud)
有类似的函数来检索索引,视图,规则等的定义.有关详细信息,请参阅手册:http://www.postgresql.org/docs/current/static/functions-info.html
获取用户类型的定义有点棘手.您需要查询information_schema.attributes
:
select attribute_name, data_type
from information_schema.attributes
where udt_schema = 'public'
and udt_name = 'footype'
order by ordinal_postion;
Run Code Online (Sandbox Code Playgroud)
从那里你需要重新组装create type
声明.
有关更多详细信息,您需要阅读系统目录的文档:http://www.postgresql.org/docs/current/static/catalogs.html
但是information_schema
如果他们返回相同的信息,你应该更喜欢它们.
这是使用 pg_get_functiondef 的完整示例查询:
WITH funcs AS (
SELECT
n.nspname AS schema
,proname AS sproc_name
,proargnames AS arg_names
,t.typname AS return_type
,d.description
,pg_get_functiondef(p.oid) as definition
FROM pg_proc p
JOIN pg_type t on p.prorettype = t.oid
JOIN pg_description d on p.oid = d.objoid
JOIN pg_namespace n on n.oid = p.pronamespace
WHERE n.nspname = 'some_schema_name_here'
)
SELECT *
FROM funcs
;;
Run Code Online (Sandbox Code Playgroud)
请注意,您显然应该指定架构名称(如果您正在使用该架构,则应指定“public”)
归档时间: |
|
查看次数: |
24549 次 |
最近记录: |