Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with timestamp in postgres using Spring Data: column $COLUMN_NAME is of type timestamp without time zone but expression is of type bytea

Using the following:

Oracle JDK 1.8.0._171

Spring boot 1.5.9-RELEASE

Postgresql 9.6

Postgres driver: 42.2.4

Getting the error from following definition:

Table column: sql meal_time TIMESTAMP DEFAULT now() NOT NULL

Entity attribute definition: java @Column(name = "meal_time", nullable=false) private Instant mealTime = Instant.now();

Whenever I try to flush or query the entity throws the error:

Caused by: org.postgresql.util.PSQLException: ERROR: column "meal_time" is of type timestamp without time zone but expression is of type bytea
  Hint: You will need to rewrite or cast the expression.

I could write a converter for this field but there should be a standard way and it feels like I am missing something as looking around for examples I found very close implementations that actually work.

like image 429
groo Avatar asked Jul 23 '18 21:07

groo


People also ask

What is timestamp without timezone in PostgreSQL?

The TIMESTAMP (also known as TIMESTAMP WITHOUT TIME ZONE ) and TIMESTAMPTZ (also known as TIMESTAMP WITH TIME ZONE ) types stored as a 64-bit integer as a microsecond offset since 1970-01-01 in CRDB and as a 64-bit integer microsecond offset since 2000-01-01 in PostgreSQL (by default).

What is timestamp data type in PostgreSQL?

The PostgreSQL Timestamp data type is used to store the time and date values for a specified column. We used different TIMESTAMP functions, for example, NOW(), CURRENT_TIMESTAMP, CURRENT_TIME, TIMEOFDAY(), and timezone(zone, timestamp) to enhance and handle the TIME and DATE value from the particular table.

How does PostgreSQL store timestamp?

The timestamptz datatype is a time zone-aware date and time data type. PostgreSQL stores the timestamptz in UTC value. When you insert a value into a timestamptz column, PostgreSQL converts the timestamptz value into a UTC value and stores the UTC value in the table.

Does Postgres store dates as UTC?

PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in UTC . They are converted to local time in the zone specified by the TimeZone configuration parameter before being displayed to the client.


1 Answers

After some research I've found this page, Using Java 8 Date and Time classes from official postgres documentation and changed the property type to be LocalDateTime instead of Instant:

java @Column(name = "meal_time", nullable = false) private LocalDateTime mealTime = LocalDateTime.now();

This fixed the issue and was an acceptable solution in this specific project.

like image 61
groo Avatar answered Oct 23 '22 05:10

groo