CREATE_CONVERSION(7)
๐ CREATE_CONVERSION โ okay bestie, let's build a whole encoding converter ๐
So this is page CREATE CONVERSION(7) from the PostgreSQL 18.6 Documentation, and yes we are doing encoding conversions today, get comfy. ๐ง
โจ NAME โจ
CREATE_CONVERSION โ bestie, it defines a brand new encoding conversion, like literally teaching your database how to translate between two character sets ๐
๐ธ SYNOPSIS ๐ธ
CREATE [ DEFAULT ] CONVERSION name
FOR source_encoding TO dest_encoding FROM function_name
That's the whole vibe. Short. Iconic. Does exactly one thing and ate. ๐
๐ฆ DESCRIPTION ๐ฆ
Okay so CREATE CONVERSION defines a new conversion between two character set encodings. That's it, that's the whole job, and it's lowkey powerful.
Now, conversions marked DEFAULT? Those are the ones that 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 going from encoding A to B, and one going from encoding B to A. Both. No skipping, it's a two-way street fr fr. ๐ซถ
And to even be allowed to create a conversion in the first place, you need:
EXECUTEprivilege on the functionCREATEprivilege on the destination schema
That's the permission vibe check and you gotta pass it. โจ
๐ PARAMETERS ๐
DEFAULTโ this clause is you saying "yes, this one, this is THE default conversion for this particular source-to-destination encoding pair." Heads up though: there should be only one default encoding in a schema for the encoding pair. One. Singular. Don't be greedy. ๐nameโ the name of the conversion. It can be schema-qualified, and if it isn't, the conversion gets defined in the current schema. Also the conversion name must be unique within a schema โ no duplicates, we don't do that here. ๐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 that actually does the converting. The function name can be schema-qualified, and if it isn't, the function will be looked up in the path.
The function must have this exact signature, no cap:
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;
And the return value? That's the number of source bytes that were successfully converted. But listen โ if that last argument is false, the function must throw an error on invalid input, and then the return value is always equal to the source string length. Always. Periodt. โญ
๐ NOTES ๐
Okay important things, pay attention bestie, this part matters.
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. ๐
Use DROP CONVERSION to remove user-defined conversions. That's your undo button.
And the privileges required to create a conversion might be changed in a future release. So like, bookmark this page, iykyk. ๐
๐ EXAMPLES ๐
Wanna create a conversion from encoding UTF8 to LATIN1 using myfunc? Say less:
CREATE CONVERSION myconv FOR 'UTF8' TO 'LATIN1' FROM myfunc;
Look at her go. Main character energy fr fr. ๐ฅ
โญ COMPATIBILITY โญ
CREATE CONVERSION is a PostgreSQL extension. There is no CREATE CONVERSION statement in the SQL standard โ BUT, the standard does have a CREATE TRANSLATION statement that is very similar in purpose and syntax. So like, cousins basically. Not the same, but spiritually adjacent. ๐ธ
๐ SEE ALSO ๐
ALTER CONVERSION(ALTER_CONVERSION(7))CREATE FUNCTION(CREATE_FUNCTION(7))DROP CONVERSION(DROP_CONVERSION(7))
Go read those too, they're all in the group chat. ๐ซถ
Okay that's the whole page, you're officially educated on encoding conversions, I'm so proud of you. This was PostgreSQL 18.6, dated 2026, and this page is CREATE CONVERSION(7). Now go forth and convert some encodings responsibly bestie, mwah ๐โจ๐
๐ฆ where this page comes from
- alpine 3.23: postgresql18-doc 18.6-r0 ยท PostgreSQL
- alpine 3.24: postgresql18-doc 18.6-r0 ยท PostgreSQL
- arch rolling: postgresql 18.6-4 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 43: postgresql 18.6-1.fc43 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- fedora 44: postgresql 18.6-1.fc44 ยท PostgreSQL
- nixos 26.05: postgresql 18.6
- ubuntu resolute: postgresql-client-18 18.6-0ubuntu0.26.04.1 ยท PostgreSQL
- ubuntu stonking: postgresql-client-18 18.6-3build2 ยท PostgreSQL
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms