A function for converting an encoding's internal identifier to a name

pg_encoding_to_char() is a  system function for converting an encoding's internal identifier to a human-readble name.

pg_encoding_to_char() was added in PostgreSQL 7.0.


pg_encoding_to_char ( integer ) → name

In system catalog relations, references to individual encodings are stored as integers, as defined in the C enum pg_enc in src/include/mb/pg_wchar.h (note there is no guarantee these values will remain stable across PostgreSQL releases). pg_encoding_to_char() can be used to convert these integer values to a human-readable format.

The following system catalog tables contain encoding references as integers:

Prior to PostgreSQL 15, this function was not referenced in the PostgreSQL documentation.

Change history


Basic usage example for pg_encoding_to_char():

postgres=# SELECT pg_encoding_to_char(1);
(1 row)

A more practical example converting the encoding column in pg_database to a human-readable value:

postgres=# SELECT datname, pg_encoding_to_char(encoding) AS encoding
             FROM pg_database ORDER BY 1;
  datname  | encoding 
 foo       | EUC_JP
 postgres  | UTF8
 template0 | UTF8
 template1 | UTF8
(4 rows)

An empty string is returned if an invalid encoding number is provided:

postgres=# SELECT pg_encoding_to_char(-1), pg_encoding_to_char(-1) IS NULL;
 pg_encoding_to_char | ?column? 
                     | f
(1 row)


Locale and character set handling, System function

See also