FluentDBFluentDB
PostgreSQL errors

PostgreSQL: cannot cast type

ERRORSQLSTATE 42846cannot_coerce
ERROR: cannot cast type <source> to <target>

Postgres has no cast between the two types you asked to convert, such as boolean to timestamp. Convert through a compatible type, use a proper conversion function, or reconsider the column type.

What this error means

A cast tells Postgres to convert a value from one type to another, but only defined casts are allowed. When no cast exists between the source and target types, it raises this error.

This differs from 'invalid input syntax', which means the cast exists but the value is malformed. Here the cast itself is not allowed at all.

How to fix it

01

There is no direct cast

Route through text when that is semantically valid.

sql
SELECT some_value::text::target_type FROM t;
02

You need a real conversion

Use a purpose-built function instead of a cast, for example to_timestamp or to_char.

03

The column type is wrong for the data

If you keep needing an impossible cast, the column may be modeled with the wrong type. Reconsider the schema.

How to avoid it next time

  • Model columns with the type the data actually is, so casts stay simple.
  • Prefer explicit conversion functions over chained casts when the meaning matters.

Frequently asked questions

How is this different from 'invalid input syntax'?

Invalid input syntax means the cast is allowed but the value is malformed. Cannot cast type means no cast exists between those two types at all.

Can I cast anything through text?

Often, but not always meaningfully. text is a common bridge, but make sure the conversion makes sense for your data.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.