42P12
PostgreSQLERRORNotableSyntax Error or Access Rule ViolationHIGH confidence
invalid database definition
What this means
SQLSTATE 42P12 is raised when CREATE DATABASE or ALTER DATABASE is given options that are invalid or contradictory — for example, an encoding that is incompatible with the selected locale.
Why it happens
- 1CREATE DATABASE with an encoding incompatible with the specified LC_COLLATE or LC_CTYPE locale
- 2Invalid option combination in CREATE DATABASE or ALTER DATABASE
How to reproduce
CREATE DATABASE with incompatible encoding and locale.
trigger — this will error
trigger — this will error
CREATE DATABASE mydb ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'en_US.latin1';
expected output
ERROR: encoding "UTF8" does not match locale "en_US.latin1"
Fix
Use a compatible encoding and locale combination
WHEN When creating a database.
Use a compatible encoding and locale combination
CREATE DATABASE mydb ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8' TEMPLATE template0;
Why this works
UTF-8 encoding requires a UTF-8 locale. Always use template0 when specifying encoding/locale to avoid cloning an incompatible template database.
Sources
Official documentation ↗
Class 42 — Syntax Error or Access Rule Violation (Postgres-specific)
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev