postgresql:如何定义具有默认值的 JSONB 列

nsk*_*lis 26 postgresql json

我无法在文档中找到如何JSONB在 PostgreSQL 中创建一个DEFAULT值为空 json 文档的列。

如何在CREATE TABLE定义中说明上述内容?

a_h*_*ame 46

这与任何其他默认值相同:

create table amsterdam
(
   id       integer primary key, 
   payload  jsonb not null default '{}'::jsonb
);
Run Code Online (Sandbox Code Playgroud)


小智 16

如果您要更改已存在的表,则语法如下:

alter table TABLE add column COLUMN jsonb not null default '{}'::jsonb;
Run Code Online (Sandbox Code Playgroud)

  • 这与现有的公认答案有何不同? (2认同)
  • 船长来了!`alter` 与 `create` (2认同)