简体   繁体   English

是否可以指示pg_dump创建“IF NOT EXISTS”表?

[英]Can pg_dump be instructed to create tables with “IF NOT EXISTS”?

As per the title. 根据标题。

I've looked at the man page and it doesn't seem like there is any command-line argument to include the "IF NOT EXISTS" clause in the "CREATE TABLE" statements. 我查看了手册页,似乎没有任何命令行参数在“CREATE TABLE”语句中包含“IF NOT EXISTS”子句。

No. The closest that pg_dump comes to this in terms of a built-in option is --if-exists , which only works in conjunction with --clean , meaning it only applies to things like DROP commands. 没有。就内置选项而言, pg_dump与此最接近的是--if-exists ,它只与--clean一起使用,这意味着它只适用于DROP命令之类的东西。

If you want to add this sort of thing, I think your best bet would be to post-process the dumps (assuming you are dumping to pure SQL and not binary format). 如果你想添加这类东西,我认为最好的办法就是对转储进行后期处理 (假设您要转储到纯SQL而不是二进制格式)。 To cover all the variety of cases, you would technically need a regex that covers this portion of the CREATE TABLE command grammar: 要涵盖所有各种情况,您在技术上需要一个覆盖CREATE TABLE命令语法的这一部分的正则表达式:

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE
       [ IF NOT EXISTS ] table_name

However a simpler regex will likely cover most cases (for example, I don't think you'd ever be dumping temp tables, pretty much definitionally, so that part can be ignored). 然而,一个更简单的正则表达式可能会涵盖大多数情况(例如,我不认为你曾经倾向于转储临时表,几乎是定义,因此可以忽略该部分)。 Similarly, GLOBAL and LOCAL are deprecated, so those can perhaps also be safely ignored, depending on your data. 同样,不推荐使用GLOBALLOCAL ,因此根据您的数据,也可以安全地忽略它们。

pg_dump doc for reference. pg_dump doc供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM