I'm trying to store dates (without times) in a PostgreSQL array.
For context, let's say these are dates on which a supermarket will be closed:
['2022-12-24', '2022-12-25', '2022-12-26']
The table is called opening_times
and there is a column called closed_days
which has a type of date[]
:
table.specificType('closed_days', 'date[]').defaultTo([]).notNullable()
However, when I UPDATE
this field using SET closed_days = '{"2022-10-16"}'
, it seems PostgreSQL is converting it into a full ISO date and time string like this: ["2022-10-15T23:00:00.000Z"]
According to the PostgreSQL docs (section 8.5), the date
type is supposed to have a resolution of 1 day, but somehow it is still storing the time. Elsewhere in the database, fields of type date
do indeed have a granularity of 1 day (time is not stored), but in these instances I am not using an array.
--
Additional information
- I am running PostgreSQL 14.2 inside a Docker container (
psql
is also running inside the container) - The type of the column in Beekeeper Studio shows as
_date
, but you can see the ORM code above that was used to create the field using typedate[]
, so I assume_date
is just another notation for the same. - In
psql
, running\d opening_times
shows that the column has a type ofdate[]
. - The result of
select array['2022-07-28'::date]
is["2022-07-27T23:00:00.000Z"]
when run in Beekeeper Studio. When the same query is run inpsql
I get{2022-07-28}
.
select array['2022-07-28'::date]; {07/28/2022}
. What client are you using? What is theclosed_days
type in the database(my bet is timestamp(tz)[])? Add answers as update to question. – Adrian Klaver_date
, but you can see the ORM code above that was used to create the field using typedate[]
, so I assume_date
is just another notation for the same. Could you explain the purpose of theSELECT
query? I'll update the question. – Obvious_Grapefruitpsql
do\d <table_name>
to find it. The select shows that an array of dates will bedate[]
or it's alias_date
vsselect array['2022-07-28'::date]::timestamptz[]; {"07/28/2022 00:00:00 PDT"}
which I believe is what is happening. Thedate[]
is updating atimestamptz[]
. – Adrian KlaverISO
datestyle :-) dbfiddle.uk/… – Bergipsql
the type shows asdate[]
. I ran theSELECT
query and the result was["2022-07-27T23:00:00.000Z"]
. – Obvious_Grapefruit