An encoding mismatch occurs when the server encoding, client encoding, or database collation settings do not align, preventing proper character handling and data transfer. Resolve by standardizing encodings to UTF-8 across your PostgreSQL configuration.
PostgreSQL stores data in a specific character encoding (like UTF-8 or LATIN1) that is set when the database is created and cannot be changed afterwards. When the client (application or psql) uses a different encoding than the server, or when locale settings conflict with the database encoding, PostgreSQL cannot properly convert or validate characters. This causes errors during data insertion, retrieval, or migration. Common mismatches include incompatible character conversions, invalid byte sequences, and locale-encoding conflicts.
First, verify what encodings are in use on your PostgreSQL instance:
SHOW server_encoding;
SHOW client_encoding;
SHOW lc_ctype;
SHOW lc_collate;For a specific database, query the system catalog:
SELECT datname, pg_encoding_to_char(encoding), datcollate, datctype
FROM pg_database
WHERE datname = 'your_database_name';When creating a new database, always specify UTF-8 encoding with compatible locales:
CREATE DATABASE my_app_db
WITH ENCODING = 'UTF8'
LC_COLLATE = 'C.UTF-8'
LC_CTYPE = 'C.UTF-8'
TEMPLATE = template0;UTF-8 is the modern standard and supports all Unicode characters. Using C.UTF-8 locale avoids locale-specific sorting issues.
If you must use a different client encoding, set it explicitly before importing or exporting data:
SET client_encoding TO 'UTF8';Or use the environment variable when connecting:
export PGCLIENTENCODING=UTF8
psql -d your_database_nameIn psql, you can also use:
\encoding UTF8If importing from a file with different encoding, convert it first using iconv:
# Convert from ISO-8859-1 to UTF-8
iconv -f ISO-8859-1 -t UTF-8 input_file.csv -o input_file_utf8.csvThen import the converted file:
COPY your_table (column1, column2) FROM STDIN WITH (FORMAT CSV, DELIMITER ',');If your database has the wrong encoding, you must dump and restore it:
# Dump with explicit client encoding matching the actual data
pg_dump --client-encoding=ISO-8859-1 your_database_name > dump.sqlEdit dump.sql and change the client_encoding line to UTF-8:
SET client_encoding = 'UTF8';Then create a new database with correct encoding and restore:
creatdb new_database_name --encoding UTF8 --lc-collate C.UTF-8 --lc-ctype C.UTF-8
psql new_database_name < dump.sqlIf template1 has incorrect encoding and is causing new database creation to fail, you must rebuild it from template0:
UPDATE pg_database SET datistemplate = false WHERE datname = 'template1';
DROP DATABASE template1;
CREATE DATABASE template1
WITH OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'C.UTF-8'
LC_CTYPE = 'C.UTF-8'
TEMPLATE = template0;
UPDATE pg_database SET datistemplate = true WHERE datname = 'template1';PostgreSQL supports multiple single-byte (ISO-8859 series) and multi-byte encodings (UTF-8, EUC_JP, SJIS). SQL_ASCII is a special "catch-all" encoding that accepts any byte sequence without validation; avoid it unless you have a specific reason and understand the data integrity risks. If you have data with mixed or corrupted encodings, you can use the convert() function to attempt conversion, or tools like pg_restore with the --no-owner and --no-privileges flags may help with problematic migrations. For international applications, UTF-8 is the universally recommended choice as it can represent all Unicode characters. Character conversion from one encoding to another is only possible if PostgreSQL has a defined conversion function for that pair; some uncommon encoding combinations may not be supported.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL