All Products
Search
Document Center

ApsaraDB RDS:Convert data between plaintext and ciphertext

Last Updated:Mar 28, 2026

When the Always confidential database feature is enabled on an ApsaraDB RDS for PostgreSQL instance, you can convert an existing column between plaintext and ciphertext in place using ALTER TABLE ... ALTER COLUMN.

Prerequisites

Before you begin, verify that the following conditions are met.

For all conversions:

For plaintext-to-ciphertext conversions only:

  • A keyname is available — either the database default keyname (|<User>|<Database>|) or a custom keyname constructed using encdb.keyname_generate and encdb.dek_generate (see the examples below).

Syntax

ALTER TABLE <table_name>
    ALTER COLUMN <column_name> [SET DATA] TYPE <target_data_type>
    USING <conversion_function>(<column_name>[, '<keyname>']);

The behavior depends on the conversion direction:

If the column is currently...And you want to...Then...
PlaintextEncrypt itSpecify the target ciphertext type and the encrypt function. Omit keyname to use the database default key, or include keyname to use a custom key.
CiphertextDecrypt itSpecify the target plaintext type and encdb.decrypt. Omit keyname from the USING clause.

Supported data types

The following table lists the plaintext and ciphertext type pairs and their corresponding conversion functions.

Plaintext typeCiphertext typeEncrypt functionDecrypt function
int4enc_int4encdb.enc_int4_encryptencdb.decrypt
int8enc_int8encdb.enc_int8_encryptencdb.decrypt
float4enc_float4encdb.enc_float4_encryptencdb.decrypt
float8enc_float8encdb.enc_float8_encryptencdb.decrypt
numericenc_decimalencdb.enc_decimal_encryptencdb.decrypt
textenc_textencdb.enc_text_encryptencdb.decrypt
timestampenc_timestampencdb.enc_timestamp_encryptencdb.decrypt

Examples

Encrypt a column using a custom key

Use this approach when you need to control the encryption algorithm, method, or key binding.

  1. Construct a keyname for the target column:

    SELECT encdb.keyname_generate('<user_name>', '<database_name>', '<schema_name>', '<table_name>', '<column_name>');
  2. Generate a data encryption key (DEK) for the keyname:

    SELECT encdb.dek_generate('<keyname>'[, '<json_params>']);

    The json_params argument is optional. When provided, it must be a valid JSON object. The following key-value pairs are supported:

    KeyAccepted valuesDefaultNotes
    algorithmAES_128_GCM, AES_128_ECB, AES_128_CTR, AES_128_CBC, SM4_128_CBC, SM4_128_ECB, SM4_128_CTR, CLWW_OREAES_128_GCMCLWW_ORE implements Order-Revealing Encryption (ORE). For more information, see Practical Order-Revealing Encryption with Limited Leakage.
    policyDEFAULTDEFAULTReserved. The value is fixed.
    flagsRND, DETRNDRND: random encryption. DET: deterministic encryption.
    mekidMaster encryption key (MEK) IDCurrent user's MEK IDSpecifies which MEK to use when generating the DEK.
  3. Convert the column from plaintext to ciphertext. The following example converts an int4 column to enc_int4:

    ALTER TABLE <table_name>
        ALTER COLUMN <column_name> SET DATA TYPE enc_int4
        USING encdb.enc_int4_encrypt(<column_name>, '<keyname>');

Encrypt a column using the database default key

Use this approach for a simpler setup when you do not need a custom keyname. The default keyname for the current database is |<User>|<Database>|.

ALTER TABLE <table_name>
    ALTER COLUMN <column_name> SET DATA TYPE enc_int4
    USING encdb.enc_int4_encrypt(<column_name>);

Decrypt a column

Use this approach to convert a ciphertext column back to plaintext. The encdb.decrypt function is used for all supported ciphertext types.

ALTER TABLE <table_name>
    ALTER COLUMN <column_name> SET DATA TYPE int4
    USING encdb.decrypt(<column_name>, '<keyname>');

Troubleshooting

`ERROR: xxxx generate dek for keyname errno:f70a0000`

Cause: Your client has not connected to the database using EncDB SDK.

Fix: Connect using EncDB SDK at least once, then retry. For connection instructions, see Use the Always confidential database feature from a client.

`ERROR: xxxx errno:fa030000` or `ERROR: xxxx error:fa020000`

Cause: No authorization record exists for your account, or your account is not authorized to perform this operation.

Fix: Authorize your account using a BCL, then retry. For authorization instructions, see Manage authorization.