FluentDBFluentDB
PostgreSQL errors

PostgreSQL: invalid input syntax for type uuid

ERRORSQLSTATE 22P02invalid_text_representation
ERROR: invalid input syntax for type uuid: "<value>"

A value sent to a uuid column is not a valid UUID, often an empty string, a truncated value, or an integer id. Validate and format UUIDs before insert, and convert empty strings to NULL.

What this error means

Postgres uuid columns accept only well-formed UUIDs, 32 hex digits in the standard grouping. Anything else, including an empty string or a numeric id, fails this conversion, and the message quotes the offending value.

It commonly comes from a form field or API that sends an empty string or a wrong id type.

How to fix it

01

An empty string reaches the uuid column

Convert an empty string to NULL before insert.

sql
INSERT INTO t (id) VALUES (NULLIF($1, '')::uuid);
02

The value is malformed

Validate the UUID format in the application before sending it.

03

You need to generate one

Use gen_random_uuid() on Postgres 13+, or uuid_generate_v4() from uuid-ossp on older versions.

sql
INSERT INTO t (id) VALUES (gen_random_uuid());

How to avoid it next time

  • Validate UUIDs at the application boundary.
  • Map empty form fields to null before they reach the database.

Frequently asked questions

Why does an empty string fail?

An empty string is not a UUID, and Postgres will not coerce it to NULL automatically. Use NULLIF(value, '') to convert it.

How do I generate a UUID in Postgres?

gen_random_uuid() is built in on Postgres 13 and later. Before that, use uuid_generate_v4() from the uuid-ossp extension.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.