This error occurs when a CREATE DATABASE or ALTER DATABASE statement contains invalid syntax or parameters. Fix by verifying your database definition syntax and ensuring all options are valid.
The 42P12 error indicates that PostgreSQL encountered a syntax or structural problem with a database creation or modification statement. This belongs to the "42" error class (Syntax Error or Access Rule Violation) and means your CREATE DATABASE or ALTER DATABASE command has malformed parameters, invalid option combinations, or incorrect syntax that violates PostgreSQL standards.
Verify your CREATE DATABASE statement matches PostgreSQL syntax. The basic form is:
CREATE DATABASE database_name
WITH
OWNER = owner_name
ENCODING = encoding_name
LOCALE = locale_name
TEMPLATE = template_name;Ensure all options are valid and spelled correctly.
Check that your ENCODING and LOCALE values are supported:
# List available encodings
psql -c "SELECT name FROM pg_catalog.pg_encoding_list()"
# List available locales (on Linux)
locale -aUse only valid encoding names like UTF8, LATIN1, and locale strings like en_US.UTF-8.
If using a TEMPLATE, ensure the template database exists:
-- List available templates
SELECT datname FROM pg_database WHERE datistemplate = true;
-- Create database with valid template
CREATE DATABASE mydb TEMPLATE template0 ENCODING 'UTF8';Common templates are template0 and template1.
Remove conflicting or duplicate options:
-- WRONG: both LOCALE and LCCOLLATE specified incorrectly
CREATE DATABASE mydb LOCALE 'en_US' LCCOLLATE 'en_US';
-- CORRECT: use LOCALE or individual LC_* options
CREATE DATABASE mydb LOCALE 'en_US.UTF-8';Consult your PostgreSQL version documentation for the correct option names.
Start with the simplest valid CREATE DATABASE statement and add options one by one:
-- Minimal: should always work
CREATE DATABASE testdb;
-- Add options gradually
CREATE DATABASE testdb ENCODING 'UTF8';
CREATE DATABASE testdb ENCODING 'UTF8' LOCALE 'C';This helps identify which option is causing the error.
Error messages and supported options vary by PostgreSQL version. Check the official documentation:
- PostgreSQL 15+: https://www.postgresql.org/docs/current/sql-createdatabase.html
- PostgreSQL 13: https://www.postgresql.org/docs/13/sql-createdatabase.html
- PostgreSQL 12: https://www.postgresql.org/docs/12/sql-createdatabase.html
Ensure your options match the documentation for your specific version.
In PostgreSQL, the difference between LOCALE, LC_COLLATE, and LC_CTYPE can cause confusion. LOCALE is a shorthand that sets both LC_COLLATE and LC_CTYPE together (available in PostgreSQL 10+). If you are using an older version, you may need to use LC_COLLATE and LC_CTYPE separately. Additionally, the TEMPLATE parameter is sensitive to the state of the template database—if template0 is marked as invalid, you cannot use it as a template. Use "UPDATE pg_database SET datistemplate = false WHERE datname = 'template0'" to fix this (requires superuser privileges).
0LP01: invalid_grant_operation
How to fix "Invalid grant operation" (0LP01) in PostgreSQL
aggregate functions are not allowed in WHERE clause
How to fix "aggregate functions are not allowed in WHERE clause" in PostgreSQL
2200L: not_an_xml_document
How to fix "2200L: not_an_xml_document" in PostgreSQL
ERROR: ambiguous_parameter
42P08: Ambiguous parameter error
2201F: invalid_argument_for_power_function
Invalid argument for power function (2201F)