CREATE_CONVERSION(7pg14)

πŸ“¦ version 14.24, in opensuse-leap 16.0

✨ CREATE_CONVERSION ✨

okay bestie, so this is the CREATE_CONVERSION(7) page from the PostgreSQL 14.24 docs, and it's literally about teaching your database a whole new way to translate between character set encodings πŸ’… like a little bilingual friend for your strings, no cap.


🌸 SYNOPSIS 🌸

CREATE [ DEFAULT ] CONVERSION name
    FOR source_encoding TO dest_encoding FROM function_name

πŸ’– DESCRIPTION πŸ’–

CREATE CONVERSION defines a new conversion between two character set encodings. that's it, that's the whole vibe β€” you're literally registering a translator πŸ¦‹

Conversions that are marked DEFAULT can be used for automatic encoding conversion between client and server. and here's the catch bestie, to support that usage, two conversions must be defined: one from encoding A to B, and one from encoding B to A. it's giving mutuals, it's giving "you follow me I follow you" 🫢

also, to be able to create a conversion, you must have EXECUTE privilege on the function and CREATE privilege on the destination schema. permission check, slay, we love a secure queen πŸ‘‘


πŸŽ€ PARAMETERS πŸŽ€

  • DEFAULT β€” this clause indicates that this conversion is the default for this particular source to destination encoding. lowkey important: there should be only one default encoding in a schema for the encoding pair. one. singular. main character energy only 🌟
  • name β€” the name of the conversion. it can be schema-qualified, and if it's not, the conversion is defined in the current schema. the conversion name must be unique within a schema, so no stealing names, that's not cute πŸ™ˆ
  • source_encoding β€” the source encoding name. where you're coming from πŸ’«
  • dest_encoding β€” the destination encoding name. where you're going ✈️
  • function_name β€” the function used to perform the conversion. the function name can be schema-qualified; if it's not, the function will be looked up in the path. (the search path, bestie, it goes looking πŸ”)

the function must have the following signature:

conv_proc(
    integer,  -- source encoding ID
    integer,  -- destination encoding ID
    cstring,  -- source string (null terminated C string)
    internal, -- destination (fill with a null terminated C string)
    integer,  -- source string length
    boolean   -- if true, don't throw an error if conversion fails
) RETURNS integer;

the return value is the number of source bytes that were successfully converted. and if the last argument is false, the function must throw an error on invalid input, and the return value is always equal to the source string length. so like, no silent failures when that boolean is false β€” it has to yell 😭


πŸ¦‹ NOTES πŸ¦‹

Neither the source nor the destination encoding can be SQL_ASCII, as the server's behavior for cases involving the SQL_ASCII "encoding" is hard-wired. so yeah, that one's off the table fr fr πŸ’€

use DROP CONVERSION to remove user-defined conversions. cleanup era 🧹

and a lil heads up: the privileges required to create a conversion might be changed in a future release. so don't get too attached to the current rules, they could get a glow-up πŸ‘€


🌈 EXAMPLES 🌈

to create a conversion from encoding UTF8 to LATIN1 using myfunc:

CREATE CONVERSION myconv FOR 'UTF8' TO 'LATIN1' FROM myfunc;

look at her go, iconic, ate and left no crumbs πŸ”₯


⭐ COMPATIBILITY ⭐

CREATE CONVERSION is a PostgreSQL extension. there is no CREATE CONVERSION statement in the SQL standard, but there is a CREATE TRANSLATION statement that is very similar in purpose and syntax. so like, the SQL standard has a cousin with a different name, iykyk πŸ’œ


πŸ’œ SEE ALSO πŸ’œ


okay that's the whole page bestie, go forth and convert πŸ§šβ€β™€οΈπŸ’–

sign-off szn: this lil masterpiece comes from the PostgreSQL 14.24 Documentation, dated 2026 β€” CREATE CONVERSION(7), and honestly? she served. periodt. πŸ’…πŸŽ€

πŸ“¦ where this page comes from