42

I am unable to find in the documentation how to create a JSONB column in PostgreSQL that has a DEFAULT value of an empty json document.

How the above can be stated in the CREATE TABLE definition ?

nskalis
  • 1,611
  • 4
  • 13
  • 12
  • what's an empty json document? "" contrains a string {} contains an object null contains a null ? – Jasen May 19 '18 at 02:58

2 Answers2

77

That's the same as with any other default value:

create table amsterdam
(
   id       integer primary key, 
   payload  jsonb not null default '{}'::jsonb
);
22

If you are altering an already existing table, then the syntax is as follows:

ALTER TABLE my_table ADD COLUMN my_column JSONB NOT NULL DEFAULT '{}'::jsonb;
enomad
  • 337
  • 2
  • 3