Empty array as PostgreSQL array column default value

SqlPostgresql

Sql Problem Overview


I have a defined an array field in postgresql 9.4 database:

character varying(64)[]

Can I have an empty array e.g. {} for default value of that field? What will be the syntax for setting so?

I'm getting following error in case of setting just brackets {}:

SQL error:

ERROR:  syntax error at or near "{"
LINE 1: ...public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}
                                                                     ^

In statement:
ALTER TABLE "public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}

Sql Solutions


Solution 1 - Sql

You need to use the explicit array initializer and cast that to the correct type:

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history SET DEFAULT array[]::varchar[];

Solution 2 - Sql

I tested both the accepted answer and the one from the comments. They both work.
I'll graduate the comments to an answer as it's my preferred syntax. 

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history SET DEFAULT '{}';

Solution 3 - Sql

It threw an error where it can not find SET. This worked for me.

ALTER TABLE public.accounts 
    ALTER COLUMN pwd_history DEFAULT array[]::varchar[];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHettView Question on Stackoverflow
Solution 1 - Sqla_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - SqlGollyJerView Answer on Stackoverflow
Solution 3 - SqlBurakView Answer on Stackoverflow