43

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 ?

1
  • what's an empty json document? "" contrains a string {} contains an object null contains a null ?
    – Jasen
    May 19, 2018 at 2:58

2 Answers 2

79

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

create table amsterdam
(
   id       integer primary key, 
   payload  jsonb not null default '{}'::jsonb
);
2
  • 1
    How to set a default value other than {} ? .e.g [1,2,3] ?
    – Eric
    Aug 27, 2022 at 3:43
  • How to define default value for jsonb[] ?
    – Abhay
    Jun 20, 2023 at 9:33
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;
2
  • 2
    How is this different from the existing accepted answer?
    – mustaccio
    Jul 4, 2019 at 18:25
  • 3
    The captain here! alter vs create Nov 10, 2019 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.