postgresql 仅导出 Postgres 中的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8473955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:28:10  来源:igfitidea点击:

Export only views in Postgres

postgresql

提问by Psyche

Is there any way to export only the views from a Postgres schema?

有没有办法只从 Postgres 模式导出视图?

I'm using Postgres 8.4.

我正在使用 Postgres 8.4。

Thank you.

谢谢你。

采纳答案by Jan Marek

If you have every view prefixed by certain prefix, you can use this command:

如果每个视图都带有某个前缀,则可以使用以下命令:

pg_dump -s -t 'prefix*' dbname > db.dump

or you can use -t switch as many as possible with names of views... See manpage for pg_dump, on the end are examples...

或者您可以使用 -t 切换尽可能多的视图名称...请参阅 pg_dump 的联机帮助页,最后是示例...

回答by araqnid

There's no direct flag to do this, but using our favourite query-the-schema-to-generate-a-command technique:

没有直接的标志可以做到这一点,但使用我们最喜欢的 query-the-schema-to-generate-a-command 技术:

select string_agg( '-t ' || quote_ident(nspname) || '.' || quote_ident(relname), ' ' )
  from pg_class join pg_namespace on pg_namespace.oid = pg_class.relnamespace
  where relkind = 'v' and not (nspname ~ '^pg_' or nspname = 'information_schema');

This will generate a string that can be used with a pg_dump command, e.g.:

这将生成一个可以与 pg_dump 命令一起使用的字符串,例如:

 -t media.duplicated_component -t adv.advert_view_distribution 

Which you could then splice into a command line directly:

然后您可以直接拼接到命令行中:

pg_dump $(psql -c "select string_agg(...etc...)" db) db