CREATE_CONVERSION(7pg17)

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

✨ CREATE_CONVERSION πŸ’… β€” bestie, let's define a whole new encoding conversion ✨

okay so this is the CREATE_CONVERSION(7) page from the PostgreSQL 17.11 documentation, and honestly? it's giving "niche but powerful" and i'm obsessed. buckle up, bestie πŸŽ€


✨ SYNOPSIS ✨

like, here's the whole vibe in one line:

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

🌸 DESCRIPTION 🌸

CREATE CONVERSION defines a brand-new conversion between two character set encodings. slay. that's literally the whole point πŸ’–

conversions that are marked DEFAULT can be used for automatic encoding conversion between client and server. but here's the catch, bestie: to support that usage, you need two conversions defined β€” one from encoding A to B, and one from encoding B to A. it's giving "you can't just do one direction and call it a day" 😭

to be able to create a conversion, you must have EXECUTE privilege on the function and CREATE privilege on the destination schema. no cap, both. πŸ’…


πŸ¦‹ 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. not two, not three, just one 🫢
  • name β€” the name of the conversion. it can be schema-qualified. if it's not, the conversion is defined in the current schema. and the conversion name must be unique within a schema β€” no duplicates allowed, periodt ✨
  • source_encoding β€” the source encoding name. that's it. that's the girl πŸ’…
  • dest_encoding β€” the destination encoding name. also that's it, she's serving 🌈
  • 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 function must have the following signature, and yes i'm copying it exactly because this part is sacred:

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 here's the drama: 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. don't forget that part or it's gonna be a whole mess 😭


🧚 NOTES 🧚

neither the source nor the destination encoding can be SQL_ASCII, because the server's behavior for cases involving the SQL_ASCII "encoding" is hard-wired. so like, don't even try it bestie, it's not gonna work πŸ™ˆ

use DROP CONVERSION to remove user-defined conversions. clean up after yourself, it's the main character thing to do πŸ’–

the privileges required to create a conversion might be changed in a future release. so like, stay tuned i guess? πŸ‘‘


πŸ“ EXAMPLES πŸ“

to create a conversion from encoding UTF8 to LATIN1 using myfunc, you literally just do this:

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

ate and left no crumbs πŸ’…βœ¨


πŸ’€ COMPATIBILITY πŸ’€

CREATE CONVERSION is a PostgreSQL extension. there is no CREATE CONVERSION statement in the SQL standard. BUT there's a CREATE TRANSLATION statement that is very similar in purpose and syntax. so like, your fave is a little bit extra and off-script, and honestly? iconic behavior πŸ‘‘


πŸ’œ SEE ALSO πŸ’œ


and that's the whole page, bestie 🫢 go forth and convert encodings responsibly. this has been CREATE CONVERSION(7) from the PostgreSQL 17.11 documentation, dated 2026 β€” catch you on the next one, mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from