i have created a custom Postgres type with :
CREATE TYPE new_type AS (new_date timestamp, some_int bigint);
i have a table that store arrays of new_type like:
CREATE TABLE new_table (
table_id uuid primary key,
new_type_list new_type[] not null
)
and i insert data in this table with something like this:
INSERT INTO new_table VALUES (
'*inApplicationGeneratedRandomUUID*',
ARRAY[[NOW()::timestamp, '146252'::bigint],
[NOW()::timestamp, '526685'::bigint]]::new_type[]
)
and i get this error
ERROR: cannot cast type timestamp without time zone to new_type
What am I missing? I've also tried array syntax that uses {} but nothing better.
PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.
Array Type. PostgreSQL gives the opportunity to define a column of a table as a variable length single or multidimensional array. Arrays of any built-in or user-defined base type, enum type, or composite type can be created. We will focus on one data type in particular, the Array of text, text[].
PostgreSQL Array type PL/pgSQL in PostgreSQL allows us to declare a variable as an ARRAY type. This ARRAY can be either a base or a custom type. For example, if we want to store a list of PINCODE values, then, we can declare the variable as v_pincode INT[].
An updatable view may contain both updatable and non-updatable columns. If you try to insert or update a non-updatable column, PostgreSQL will raise an error.
The easiest way would probably be:
INSERT INTO new_table VALUES (
'9fd92c53-d0d8-4aba-8925-1bd648d565f2'::uuid,
ARRAY[ row(now(), 146252)::new_type,
row(now(), 526685)::new_type
] );
Note that you have to cast the row
type to ::new_type
.
As an alternative, you could also write:
INSERT INTO new_table VALUES (
'9fd92c53-d0d8-4aba-7925-1ad648d565f2'::uuid,
ARRAY['("now", 146252)'::new_type,
'("now", 526685)'::new_type
] );
Check PostgreSQL documentation about Composite Value Input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With