Erratum: the client_encoding post said that SQL_ASCII “disables encoding validation entirely,” and described setting client_encoding = 'UTF8' on an SQL_ASCII server as telling PostgreSQL “trust me, the bytes are UTF-8.” Neither is right. A session that declares a client encoding such as UTF8 has everything it sends and receives checked against it; what SQL_ASCII turns off is conversion. Details below.

server_encoding was misnamed by the patch that introduced it. Tatsuo Ishii’s multibyte work for PostgreSQL 6.4, committed in July 1998, did two things that matter here: it moved the choice of encoding from compile time to initdb and createdb, and it added SHOW server_encoding. The variable was named for the arrangement the same commit had just ended. There has not been a server encoding since. What the parameter reports is pg_database.encoding for whichever database you are connected to, and the database next door is free to give a different answer.

It is a preset parameter, context internal. SET, ALTER SYSTEM, and ALTER DATABASE ... SET all get parameter "server_encoding" cannot be changed; a line in postgresql.conf keeps the server from starting; setting it through PGOPTIONS gets the connection refused. It has no default in the usual sense, either. The compiled-in boot value is SQL_ASCII, and connection startup replaces it with the database’s encoding once the backend has found its row in pg_database. A physical replication connection never attaches to a database, so nothing replaces it: psql "replication=true" -c "SHOW server_encoding" answers SQL_ASCII on a cluster where every database is UTF8. The real value is fixed by CREATE DATABASE, and there is no ALTER DATABASE ... ENCODING.

Every client hears the value at connection start, in the same ParameterStatus batch as integer_datetimes and thanks to the same patch: Oliver Jowett asked for both in August 2004 with the JDBC driver in mind, and both shipped in 8.0. His argument for this one was SQL_ASCII. In 2004, a driver that asked an SQL_ASCII database for Unicode got whatever bytes happened to be in there, and he wanted the driver to be able to see that coming (he would have preferred that the server refuse the combination outright; since 9.4 it does, one bad row at a time). Twenty-two years later, pgjdbc does not look at the value, and neither do psycopg, Npgsql, node-postgres, asyncpg, or pgx. (libpqxx does, and for his reason: it asks at connect time whether the server is SQL_ASCII.) Inside the PostgreSQL distribution, the only reader is psql 19, which labels the queries behind its backslash commands with a comment, translates the label, and sends the translated version only when server_encoding and client_encoding match. That check was committed four weeks before 19.0 shipped.

Things a cluster has only one of

The documentation says clients ordinarily need only worry about client_encoding, which is true of clients. It is not true of whoever runs a cluster with two encodings in it, because a cluster has exactly one each of several things, and none of them has an encoding.

The shared catalogs are the first. pg_authid and pg_database are the same tables from every database, and a name goes into them as bytes in the encoding of whichever database the creating session was connected to. Run CREATE ROLE "josé" in a LATIN1 database and the catalog holds 6a 6f 73 e9. Seen from a UTF8 database in the same cluster, that is not a string. psql drops the byte and shows you jos. psycopg raises UnicodeDecodeError on SELECT rolname FROM pg_roles. A UTF-8 client cannot log in as the role, because the name in the startup packet is matched byte for byte. And pg_dumpall, run the default way, fails, which means pg_upgrade does:

1$ pg_upgrade --check ...
2*Clusters are compatible*
3
4$ pg_upgrade ...
5Creating dump of global objects
6*failure*
7
8$ tail pg_upgrade_utility.log
9pg_dumpall: error: query failed: ERROR: invalid byte sequence for encoding "UTF8": 0xc0 0x20

That is 18.7 to 19.0; the check passes and the upgrade does not. The bytes in the message are not the bytes in the catalog, either. Since the CVE-2025-1094 fix in February 2025, the client-side quoting functions that cannot report an error replace an invalid sequence with one the server is guaranteed to reject, and for UTF-8 that is 0xc0 0x20. The simplest fix is to rename the role from a database whose encoding matches its bytes (the same ALTER ROLE ... RENAME TO issued from the UTF8 database reports that the role does not exist), and then to keep role and database names in ASCII, which I would do even on a cluster with one encoding.

The server log is the second. Each backend writes messages in its own database’s encoding into the one file, so the same statement containing naïve is logged as 6e 61 c3 af 76 65 by a UTF8 database and 6e 61 ef 76 65 by a LATIN1 one. file calls the result ISO-8859 text. With jsonlog, the LATIN1 line is not UTF-8, so it is not JSON, and Python’s json module refuses it.

pg_stat_activity is the third. Query text sits in shared memory in the encoding of the backend that ran it, and is handed to readers in other databases unconverted. A monitoring agent connected to postgres gets bytes that are invalid for its own connection whenever a query in a database with a different encoding contains a non-ASCII character: psql drops them, psycopg raises an exception, and in the other direction (a LATIN1 reader, a UTF8 backend) you get mojibake and no error at all. None of this is news. When application_name was added in 2009, Tom Lane insisted that it be limited to ASCII because it would travel from one backend to another without conversion, and Andres Freund asked in reply whether that wasn’t just as true of pg_stat_activity. It was. On 19.0, it still is. (pg_stat_statements tries harder: it records each entry’s encoding and converts on the way out. The price is that one statement the reader’s encoding cannot represent makes the entire view fail from that database.)

Thirty-four values, one of them right

PostgreSQL 19 took one of the possible values away. MULE_INTERNAL is gone, and pg_upgrade has a new check that stops at any database using it and recommends dumping and restoring as UTF8. That leaves 34 server encodings. Start by finding out which ones you have:

1SELECT datname, pg_encoding_to_char(encoding) AS encoding
2 FROM pg_database
3 ORDER BY 2, 1;

SHOW server_encoding answers for one database; this answers for the cluster. Every row should say UTF8.

If a row says SQL_ASCII, the usual cause is an initdb that ran with no locale in its environment, which still produces SQL_ASCII in 19.0. The documentation calls that setting “a declaration of ignorance about the encoding,” which I think undersells it. An SQL_ASCII database does validate, and has in both directions since 9.4: a session that declares client_encoding = 'UTF8' has everything it sends and receives checked as UTF-8 (the server’s own default, SQL_ASCII, accepts anything). What it cannot do is make two sessions agree. A UTF-8 client and a Latin-1 client can each insert naïve into the same table; both succeed; length() returns 6 for one row and 5 for the other; and the next time the UTF-8 client selects the whole table, it gets invalid byte sequence for encoding "UTF8".

The encoding of an existing database cannot be changed, so the way out leads through a new database: pg_dump and restore, or logical replication, as in “Speaking in Tongues”. Logical replication does the conversion without being asked, because the subscriber’s connection requests its own database’s encoding; a LATIN1 publisher feeding a UTF8 subscriber simply works. An SQL_ASCII publisher stops at the first row that is not valid UTF-8, which is the correct behavior and the start of your cleanup list. (The reverse direction stalls on the first character the target cannot represent; a UTF8 publisher and a LATIN1 subscriber get along until someone types a euro sign.)

What does not work is the shortcut: UPDATE pg_database SET encoding = pg_char_to_encoding('UTF8') WHERE datname = .... That changes the label and none of the data. The database then reports UTF8, hands the old bytes to clients who believe it, says that a five-character Latin-1 naïve has a length() of 3, and produces a pg_dump that will not restore into a database that was UTF8 from birth. Laurenz Albe’s reply to someone who had done this to a WIN1252 database is the whole review: “Congratulations, you have broken your database.”

server_encoding tells you what one database believes about its own bytes. Whether the cluster agrees with itself is the pg_database query, and the only good result is the same four characters on every row.