PG
PRO
42P12ERRORTier 2 — Caution✅ HIGH confidence

invalid database definition

Category: Syntax Error or Access Rule ViolationVersions: All Postgres versions

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

  1. 1CREATE DATABASE with an encoding incompatible with the specified LC_COLLATE or LC_CTYPE locale
  2. 2Invalid option combination in CREATE DATABASE or ALTER DATABASE

How to reproduce

CREATE DATABASE with incompatible encoding and locale.

trigger — this will ERROR
CREATE DATABASE mydb ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'en_US.latin1';
ERROR: encoding "UTF8" does not match locale "en_US.latin1"

Fix 1: Use a compatible encoding and locale combination

When creating a database.

fix
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 docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 42 — Syntax Error or Access Rule Violation (Postgres-specific)

Confidence assessment

✅ HIGH confidence

Postgres-specific. Stable across versions.

See also

📄 Reference pages

CREATE DATABASEEncodingLocale
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →