51

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 ?

asked May 17, 2018 at 8:45
1
  • what's an empty json document? "" contrains a string {} contains an object null contains a null ? Commented May 19, 2018 at 2:58

2 Answers 2

88

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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.