pg_dump在对象上没有注释?[英] pg_dump without comments on objects?

本文是小编为大家收集整理的关于pg_dump在对象上没有注释?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

是否有一种方法可以执行PG_DUMP并在表/视图和列上排除评论?

我广泛使用对命令的注释来描述所有对象,并且通常在其中包含新线以进行清晰的描述,例如:

COMMENT ON TABLE mytable1 IS 'MAIN TABLE...
NOTES:
1. ...
2. ...
3. ...
';

但是,由于转储中也有新线,因此我不能简单地使用grep -v'评论"命令"删除评论.

其他方法可以快速从转储中删除这些评论?

推荐答案

使用--no-comments选项.

示例:

$ pg_dump --no-comments [database] > dump.sql

参考: app-pgdump.html

其他推荐答案

afaik,pg_dump和pg_restore均无删除COMMENT s的选项.但是,如果您使用二进制转储格式,例如:

 $ pg_dump -Fc <your connection> -f /path/to/backup.dump

您可以提取TOC条目并进行编辑:

 $ pg_restore -l -f /path/to/backup.toc /path/to/backup.dump

以上将提取一个toc文件并将其保存在/path/to/backup.toc中,然后您可以找到每行带有COMMENT输入并删除或评论.如果您不使用对象上的奇怪名称,那么简单的sed可以解决问题,以COMMENT s评论行(半隆开始评论):

$ sed -i 's/^\(.* COMMENT .*\)/;\1/g' bar.toc

使用此新的TOC文件,您现在可以使用pg_restore还原您的转储(使用-L选项):

$ pg_restore -L /path/to/backup.toc -d <your database> /path/to/backup.dump

其他推荐答案

我实际上会使用两个阶段的转储进行此操作.

  1. 转储并恢复DB,或从CREATE DATABASE WITH TEMPLATE或CREATE DATABASE WITH TEMPLATE

  2. 的旧DB中创建一个新的DB
  3. 运行以下命令

    Delete from pg_description;
    
  4. 转储并还原该数据库.这将确保您没有任何烦人的依赖关系.

本文地址:https://www.itbaoku.cn/post/1763878.html

问题描述

Is there a way to perform a pg_dump and exclude the COMMENT ON for tables/views and columns ?

I use extensively the COMMENT ON command to describe all objects, and often include newlines in them for clearer descriptions, e.g.:

COMMENT ON TABLE mytable1 IS 'MAIN TABLE...
NOTES:
1. ...
2. ...
3. ...
';

However, since there are newlines in the dump as well, I cannot simply remove the comments with a grep -v 'COMMENT ON' command.

Any other way to quickly remove these COMMENT ON from the dump ?

推荐答案

Use the --no-comments option.

Example:

$ pg_dump --no-comments [database] > dump.sql

References: https://www.postgresql.org/docs/12/app-pgdump.html

其他推荐答案

AFAIK, neither pg_dump nor pg_restore have options to remove COMMENTs. But, if you use a binary dump format like:

 $ pg_dump -Fc <your connection> -f /path/to/backup.dump

you could extract the TOC entry and edit it:

 $ pg_restore -l -f /path/to/backup.toc /path/to/backup.dump

The above will extract a TOC file and save it at /path/to/backup.toc, then you could find each line with COMMENT entry and remove or comment it. If you don't use strange names on your objects, a simple sed would solve the problem, to comment the lines with COMMENTs you could do this (a semicolon starts a comment):

$ sed -i 's/^\(.* COMMENT .*\)/;\1/g' bar.toc

With this new TOC file, you can now use pg_restore to restore your dump (with -L option):

$ pg_restore -L /path/to/backup.toc -d <your database> /path/to/backup.dump

其他推荐答案

I would actually do this with a two-stage dump and restore.

  1. Dump and restore the db as is or create a new db from the old one with createdb -T or CREATE DATABASE WITH TEMPLATE

  2. Run the following command

    Delete from pg_description;
    
  3. Dump and restore that database. That will ensure you don't have any annoying dependencies floating around.