This error occurs when a PostgreSQL database's character encoding does not match the LC_CTYPE and LC_COLLATE locale settings. PostgreSQL requires that database encodings are compatible with the system locale. For non-C/POSIX locales, only one character set will work correctly with each locale.
PostgreSQL enforces a strict compatibility requirement between database character sets and locale settings. Each database has an LC_CTYPE (character classification) and LC_COLLATE (sort order) locale setting that determines which character set is allowed. For C or POSIX locale, any character set is acceptable, but for other system locales (like en_US.UTF-8), only a matching character set is permitted. When you attempt to create a database with a character encoding that is incompatible with its locale, PostgreSQL raises this error. This typically happens during database creation with CREATE DATABASE or when initializing a new cluster with initdb.
First, determine which locales are installed on your operating system:
locale -aThis lists all available locales. Look for the locale you want to use (e.g., en_US.UTF-8, C, POSIX). If your desired locale is not listed, you will need to install it.
If your desired locale is not available, install it:
Ubuntu/Debian:
sudo locale-gen en_US.UTF-8
sudo update-localeCentOS/RHEL/Fedora:
sudo localedef -i en_US -f UTF-8 en_US.UTF-8After installation, verify the locale is available:
locale -a | grep en_USWhen creating a new database, explicitly specify both the encoding and locale settings to ensure compatibility:
CREATE DATABASE my_database
ENCODING 'UTF8'
LC_COLLATE 'en_US.UTF-8'
LC_CTYPE 'en_US.UTF-8';The encoding must match the character set implied by the locale. For UTF-8 locales, use ENCODING 'UTF8'. For LATIN1 locales, use ENCODING 'LATIN1'.
To verify the database was created with correct settings:
SELECT datname, encoding, datcollate, datctype FROM pg_database WHERE datname = 'my_database';If you cannot resolve the locale compatibility issue, use the C or POSIX locale, which accepts any character set:
CREATE DATABASE my_database
ENCODING 'UTF8'
LC_COLLATE 'C'
LC_CTYPE 'C';Note: C/POSIX locale uses byte-by-byte sorting and does not perform locale-aware operations like case-insensitive matching. For production systems, this is a workaround only.
PostgreSQL 10+ supports ICU (International Components for Unicode) as an alternative to OS-provided locales. ICU is independent of the operating system locale:
CREATE DATABASE my_database
ENCODING 'UTF8'
LOCALE_PROVIDER 'icu'
LOCALE 'en_US';ICU provides consistent collation and character classification across platforms. Check if your PostgreSQL installation supports ICU:
SHOW icu_validation_level;If ICU is available and you have control over database creation, this is the recommended solution.
If you are restoring a database dump (pg_dump), you may need to recreate the database with matching locale:
# Drop existing database if necessary
psql -U postgres -c "DROP DATABASE IF EXISTS my_database;"
# Recreate with correct settings
psql -U postgres -c "CREATE DATABASE my_database
ENCODING 'UTF8'
LC_COLLATE 'en_US.UTF-8'
LC_CTYPE 'en_US.UTF-8';"
# Restore the dump
psql -U postgres -d my_database < my_database.sqlAlternatively, use pg_restore with the -C flag to recreate the database:
pg_restore -C -d postgres my_database.dumpPostgreSQL stores locale settings at database creation and they cannot be changed without recreating the database. LC_COLLATE and LC_CTYPE affect string sorting, case conversion, and character classification throughout the database. If you change the system locale after PostgreSQL cluster initialization, existing databases retain their original locale settings. For multi-language support or consistency across platforms, ICU provider (PostgreSQL 10+) is preferred over OS locales because it does not depend on system locale availability. When migrating databases between servers with different locale configurations, always explicitly specify the target encoding and locale to avoid incompatibility. Test character set and locale compatibility in a staging environment before applying to production.
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)