How to fix Invalid input syntax for UUID in PostgreSQL
This error occurs when you attempt to insert or query a UUID column with a malformed value. PostgreSQL expects UUIDs in the standard 8-4-4-4-12 hexadecimal format, and will reject any value that doesn't match valid UUID syntax.
Quick fix
Fixes Invalid input syntax for UUID
Valid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
Valid: A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11 (uppercase also accepted)
Valid: a0eebc999c0b4ef8bb6d6bb9bd380a11 (no hyphens)
Invalid: a0eebc99-9c0b-4ef8-bb6d (too short)
Invalid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11-extra (extra characters)- Verify the UUID format is correct
- Remove any extra quotes or braces from the value
- Cast the value explicitly to UUID type
- Validate UUIDs in your application before insertion
Valid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11Valid: A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11 (uppercase also accepted)Valid: a0eebc999c0b4ef8bb6d6bb9bd380a11 (no hyphens)Invalid: a0eebc99-9c0b-4ef8-bb6d (too short)Invalid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11-extra (extra characters)Invalid input syntax for UUID
What this error means
PostgreSQL uses the UUID data type to store Universally Unique Identifiers as defined by RFC 9562. When you try to insert a value into a UUID column or use it in a WHERE clause, PostgreSQL validates that the value conforms to strict formatting rules. The "Invalid input syntax for UUID" error indicates the provided value fails this validation check, typically due to malformed characters, incorrect hyphen placement, or values that aren't hexadecimal digits.
Typical symptoms
- INSERT or UPDATE statements fail with "invalid input syntax for type uuid" error
- SELECT queries with UUID comparison fail when filter values are incorrect
- ORM queries fail when UUID parameters are passed as strings in wrong format
- Migration scripts fail when trying to populate UUID columns with legacy data
Common causes
- UUID value is missing or has extra characters (e.g., missing hyphens or malformed hex)
- Value contains non-hexadecimal characters or is too short/long
- Two UUIDs are concatenated together instead of being separate values
- Extra quotes or braces are added around the UUID value by the application
- ORM serialization error that wraps the UUID in JSON instead of passing it as a string
- Legacy data format doesn't match PostgreSQL UUID expectations
How to fix "Invalid input syntax for UUID"
PostgreSQL accepts UUIDs in the standard format (8-4-4-4-12 hexadecimal digits):
Valid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
Valid: A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11 (uppercase also accepted)
Valid: a0eebc999c0b4ef8bb6d6bb9bd380a11 (no hyphens)
Invalid: a0eebc99-9c0b-4ef8-bb6d (too short)
Invalid: a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11-extra (extra characters)Check the value you're inserting against one of these formats.
If you're building SQL manually, ensure you're not double-quoting the UUID:
-- WRONG:
INSERT INTO users (id, name) VALUES ('"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"', 'John');
-- CORRECT:
INSERT INTO users (id, name) VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'John');If using an ORM, verify it's not wrapping the UUID in extra serialization.
If you're passing a string that might not parse automatically, use an explicit cast:
SELECT * FROM users WHERE id = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid;
INSERT INTO users (id, name) VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid, 'John');The ::uuid cast tells PostgreSQL to validate and convert the string to UUID type.
Use a UUID validation library in your application language:
Node.js/JavaScript:
import { validate as uuidValidate } from "uuid";
const userInput = req.body.id;
if (!uuidValidate(userInput)) {
throw new Error('Invalid UUID format');
}
await prisma.user.create({ data: { id: userInput, name: 'John' } });Python:
from uuid import UUID
try:
UUID(user_input)
except ValueError:
raise Exception('Invalid UUID format')This catches format errors before they reach the database.
If using TypeORM, Sequelize, or similar ORMs, ensure UUID columns are properly typed:
TypeORM:
import { Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
}Prisma:
model User {
id String @id @default(cuid())
name String
}Verify your ORM configuration matches your database schema.
Instead of generating UUIDs in your application, let PostgreSQL handle it:
-- For UUID v4 (random):
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL
);
-- Insert without specifying id:
INSERT INTO users (name) VALUES ('John');
-- For older PostgreSQL versions (< 13), use uuid-ossp extension:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
name text NOT NULL
);This eliminates manual UUID formatting errors.
Advanced notes
PostgreSQL UUID validation is strict by design. RFC 9562 defines multiple UUID versions (v1-v7), but PostgreSQL's type system only validates the format, not the version. If you're migrating legacy data, you may need to normalize values using string functions like REGEXP_REPLACE() before casting to UUID. For distributed systems, UUIDs provide better uniqueness guarantees than serial sequences. The gen_random_uuid() function (PostgreSQL 13+) generates RFC 9562 compliant v4 UUIDs without requiring additional extensions.
Frequently asked questions
- What does "Invalid input syntax for UUID" mean?
- PostgreSQL uses the UUID data type to store Universally Unique Identifiers as defined by RFC 9562. When you try to insert a value into a UUID column or use it in a WHERE clause, PostgreSQL validates that the value conforms to strict formatting rules. The "Invalid input syntax for UUID" error indicates the provided value fails this validation check, typically due to malformed characters, incorrect hyphen placement, or values that aren't hexadecimal digits.
- How do I fix "Invalid input syntax for UUID"?
- This error occurs when you attempt to insert or query a UUID column with a malformed value. PostgreSQL expects UUIDs in the standard 8-4-4-4-12 hexadecimal format, and will reject any value that doesn't match valid UUID syntax.
- What causes "Invalid input syntax for UUID"?
- UUID value is missing or has extra characters (e.g., missing hyphens or malformed hex) Value contains non-hexadecimal characters or is too short/long Two UUIDs are concatenated together instead of being separate values
- Is the "Invalid input syntax for UUID" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Docs - UUID Type(Official Docs)
- ๐PostgreSQL Docs - UUID Functions(Official Docs)