PostgreSQL error 42P12, "invalid database definition," occurs when a SQL statement attempts to create or alter a database using a definition that PostgreSQL's parser or catalog considers structurally invalid. In practice, it is a rare error that almost always points to a malformed CREATE DATABASE or ALTER DATABASE statement, a corrupted system catalog, or a mismatch between the server's expectations and the supplied options—not to a problem with your application's data.
What the Error Actually Means
PostgreSQL organizes its error codes into classes. Class 42 covers "syntax error or access rule violation," and 42P12 is the specific code for an invalid database definition. The message typically appears as:
ERROR: invalid database definition
SQL state: 42P12
Unlike more familiar errors such as 42P01 (undefined table) or 42P07 (duplicate table), 42P12 is not commonly seen in day-to-day application work. It is raised by the server when it cannot reconcile the parameters of a database definition with what the catalog and parser require. This can happen during CREATE DATABASE, ALTER DATABASE, or when the server reads a damaged or inconsistent pg_database row.
PostgreSQL's documentation lists 42P12 as part of the 42Pxx family but does not attach a long explanatory note to it, which is one reason the error confuses people who encounter it. The practical meaning is: the database definition you supplied, or the one the server found on disk, does not satisfy PostgreSQL's internal rules for a valid database.
Common Causes
In real deployments, 42P12 tends to arise from a small set of situations. The most frequent are:
- Malformed CREATE DATABASE options. For example, specifying an invalid
ENCODING, aLC_COLLATEorLC_CTYPEthat is incompatible with the chosen encoding, or aTEMPLATEdatabase that does not exist or is not marked as a template. - Invalid ALTER DATABASE statements. Changing a database's connection limit to a negative value, or setting a configuration parameter with a malformed value, can trigger the error.
- Corrupted or manually edited catalog rows. Directly modifying
pg_databasewithUPDATEorDELETEbypasses validation and can leave a row that the server later rejects. - Version mismatches during upgrade. Running an older client or a script written for a different PostgreSQL major version against a newer server can produce option combinations the server does not accept.
- Damaged data files. Filesystem corruption, an interrupted write, or a failed restore can leave the catalog in a state that produces 42P12 when the database is accessed.
A common misconception is that 42P12 means the database is missing or cannot be connected to. In fact, the error is about the definition being invalid, not about the database being absent. A missing database produces a different error (typically 3D000, "invalid catalog name").
How to Diagnose the Problem
Because 42P12 is unusual, diagnosis should be systematic. The goal is to determine whether the problem is in the statement you sent or in the catalog the server is reading.
- Capture the exact statement. Enable
log_statement = 'all'or checklog_min_error_statementso the failing SQL appears in the server log alongside the error. - Reproduce in isolation. Run the same
CREATE DATABASEorALTER DATABASEinpsqlagainst the same server. If it fails there too, the statement is the problem. - Inspect the catalog. Query
SELECT * FROM pg_database WHERE datname = 'your_db';and check the encoding, collation, ctype, and connection limit columns for values that look wrong or inconsistent. - Check server logs. Look for earlier errors—disk full, I/O errors, or failed checkpoints—that might indicate corruption rather than a bad statement.
- Test with a minimal statement. Try
CREATE DATABASE test_db;with no options. If that succeeds, the problem is one of the options you supplied.
Fixing a Bad Statement
If the diagnosis points to the statement, the fix is usually straightforward. Compare your options against the server's supported values:
- Ensure
ENCODINGis a valid PostgreSQL encoding name such asUTF8orLATIN1. - Ensure
LC_COLLATEandLC_CTYPEare installed on the server and compatible with the encoding. On many systems,UTF8requires a UTF-8 locale. - Ensure the
TEMPLATEdatabase exists and hasdatistemplate = trueif you intend to use it as a template. - Ensure
CONNECTION LIMITis zero or a positive integer. - Ensure any
ALTER DATABASE ... SETparameters use valid names and values.
If the statement is valid but still fails, check whether the server's locale or encoding support has changed—for example, after an operating system upgrade that removed a locale the database was created with.
Fixing Catalog or Corruption Issues
If a minimal statement succeeds but queries against an existing database fail with 42P12, the catalog row itself may be damaged. In that case:
- Take a filesystem-level backup before making changes.
- Run
pg_dumpallif the server is still readable, to preserve as much data as possible. - Use
pg_resetwalonly as a last resort and only with expert guidance—it can cause data loss. - Restore from a known-good backup or rebuild the cluster with
initdband reload data.
Never edit pg_database directly with SQL; use ALTER DATABASE or DROP DATABASE so that the server validates changes.
Prevention and Best Practices
Most 42P12 errors are avoidable with a few habits:
- Use version-controlled migration scripts and test them against a staging server of the same major version.
- Avoid hard-coding locale and encoding options unless you have verified them on the target server.
- Never modify system catalogs directly.
- Monitor disk space and I/O errors; corruption often begins with a failed write.
- Keep regular logical and physical backups so that recovery does not depend on repairing a damaged catalog.
Related Error Codes Worth Knowing
Understanding neighboring codes helps distinguish 42P12 from lookalikes:
- 3D000 — invalid catalog name: the database does not exist.
- 42P01 — undefined table: the table does not exist.
- 42P07 — duplicate table: the object already exists.
- 42P04 — duplicate database: the database already exists.
- 58P01 — undefined file: a required file is missing, often a locale or configuration file.
If you see 42P12 together with 58P01, suspect a missing locale or encoding file rather than a bad SQL statement.
Frequently Asked Questions
Is error 42P12 caused by my application code?
Usually not directly. It is raised by the server when a database definition is invalid, which most often comes from a migration script, an administrative command, or a damaged catalog. Application queries that merely connect to a database rarely trigger it.
Can I ignore 42P12 if the database still works?
No. Even if some connections succeed, an invalid database definition indicates an inconsistency that can cause failures during backup, restore, upgrade, or replication. It should be investigated and corrected.
Does 42P12 mean my data is lost?
Not necessarily. The error concerns the database definition, not the data files themselves. In many cases the data remains intact and can be recovered by fixing the definition or restoring from backup.
Why does CREATE DATABASE fail with 42P12 on a new server?
The most common reason is that the requested locale or encoding is not installed on the new server. Check the server's available locales and use one that exists, or install the missing locale package.
Can a PostgreSQL upgrade cause 42P12?
It can, particularly if the new server lacks a locale or encoding that the old cluster used. Running pg_upgrade without verifying locale availability is a known way to encounter definition-related errors.
Summary
PostgreSQL error 42P12 is a signal that a database definition—either one you supplied or one stored in the catalog—does not meet the server's validity rules. It is uncommon, and it is almost always traceable to a specific statement, a locale or encoding mismatch, or catalog corruption. Systematic diagnosis, careful use of catalog views, and disciplined backup practices resolve it in most cases.