postgresql 为特定的表和条目 Postgres 创建数据库转储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7359827/
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-09-10 23:05:29  来源:igfitidea点击:

Creating a database dump for specific tables and entries Postgres

postgresql

提问by Elitmiar

I have a database with hundreds of tables, what I need to do is export specified tables and insert statements for the data to one sql file.

我有一个包含数百个表的数据库,我需要做的是将数据的指定表和插入语句导出到一个 sql 文件中。

The only statement I know can achieve this is

我知道可以实现这一目标的唯一声明是

pg_dump -D -a -t zones_seq interway > /tmp/zones_seq.sql

Should I run this statement for each and every table or is there a way to run a similar statement to export all selected tables into one big sql big. The pg_dump above does not export the table schema only inserts, I need both

我应该为每个表运行这个语句,还是有一种方法可以运行类似的语句来将所有选定的表导出到一个大的 sql big 中。上面的 pg_dump 不导出表模式只插入,我需要两者

Any help will be appreciated.

任何帮助将不胜感激。

回答by a_horse_with_no_name

Right from the manual: "Multiple tables can be selected by writing multiple -t switches"

直接来自手册:“可以通过编写多个 -t 开关来选择多个表

So you need to list all of your tables

所以你需要列出你所有的表

pg_dump --column-inserts -a -t zones_seq -t interway -t table_3 ... > /tmp/zones_seq.sql  

Note that if you have several table with the same prefix (or suffix) you can also use wildcards to select them with the -tparameter:

请注意,如果您有多个具有相同前缀(或后缀)的表,您还可以使用通配符通过-t参数选择它们:

"Also, the table parameter is interpreted as a pattern according to the same rules used by psql's \d commands"

"此外,根据 psql 的 \d 命令使用的相同规则,表参数被解释为模式"

回答by Jothikanth

If those specific tables match a particular regex, You can use the regex in -t option in pg_dump.

如果这些特定的表匹配特定的正则表达式,您可以在 pg_dump 中的 -t 选项中使用正则表达式。

pg_dump -D -a -t zones_seq -t interway -t "<regex>" -f /tmp/zones_seq.sql <DBNAME>

For example to dump tables which started with "test", you can use

例如转储以“test”开头的表,您可以使用

pg_dump -D -a -t zones_seq -t interway -t "^test*" -f /tmp/zones_seq.sql <DBNAME>