I can not create a Type of VArray
in PostgreSQL.
I have this code in Oracle for the creation of the type:
TYPE FIELDS IS VARRAY(14) OF VARCHAR2(256);
and this is the code that I put in PostgreSQL PgAdmin:
CREATE TYPE FIELDS AS varchar(256)[];
Is there any other ways to create this type?
2
Answers
Theoretically you don’t need to do anything to use a type as an array.
Every type in postgres has its corresponding array type (see https://www.postgresql.org/docs/current/sql-createtype.html#SQL-CREATETYPE-ARRAY).
So, to use a
varchar(256)
array in a table, you could:If you need to add more constraints to a type, you can look at
create domain
as well.As an aside, I’ve read many times that
text
is a better type thanvarchar(N)
(if it’s just about disk space optimization).If you want to guarantee a max text length, you could always use a
check (length(value) < 255)
on your column definition.You cannot declare a new type as an array, but you can create a domain over this type and use that like a data type:
It is often not a good idea to use arrays as column data type.