创建 table,以 postgresql 中的数字开头

Create table, starting with digit in postgresql

你能给我建议在 postgresql 中创建以数字开头的 table 吗?

使用双引号,例如:

t=# create table "42 Might be not The be$t idea" (i serial);
CREATE TABLE
t=# \d+ "42 Might be not The be$t idea"
                                             Table "public.42 Might be not The be$t idea"
 Column |  Type   |                                  Modifiers                                  | Storage | Stats target | Descript
ion
--------+---------+-----------------------------------------------------------------------------+---------+--------------+---------
----
 i      | integer | not null default nextval('"42 Might be not The be$t idea_i_seq"'::regclass) | plain   |              |

请仔细看看它导致了什么。通常使用混合大小写、特殊字符和从数字开始的关系是一种不好的做法。尽管 Postgres 理解并使用这样的关系名称这一事实,但您仍有可能遇到其他软件的错误。

如果没有经验,您很可能搬起石头砸自己的脚。例如 pg_dump -t "badName" 将不起作用。 Bash 会将双引号理解为自己的引号 - 它就是这样工作的。所以你必须指定 pg_dump -t '"badName"' 才能找到 table。如果您只是找不到 table,那么您很幸运。灾难是当你在同一个模式中有 badnameBadname

它可行并不意味着您应该开始使用它。