Skip to content

Create encrypted permanent tables over TDS

Prepare a database key on PG, create a table from a SQL Server client and verify the table, indexes and out-of-line values.

A SQL Server client can create an encrypted permanent table on Mimus with an ordinary CREATE TABLE. The DBA supplies the key and the creation default. Applications then use their usual INSERT, UPDATE and SELECT. A migration can also select encryption for a single transaction. This guide covers the 19.0.1 candidate with the four accepted changes, tested over TDS 7.4 on Debian 13 x86-64 image a3b80a5a99a1. Identify the build and patches

Prepare the key in the physical database

Start with an initialized Babelfish instance. The PG administrator uses psql to connect to physical database mimus on port 5432. The TDS client connects to logical database vault on port 1433. The example assumes deployment login lab_deploy maps to a user with permission to create tables in vault. Preload pg_tde and babelfishpg_tds, and install the pg_tde extension in public in the physical database. Instance initialization · Login and schema mapping

For a new test database, run the following on PG. First create /var/lib/mimus-keys outside PGDATA on the server, owned by the database service account with mode 0700. A database with a principal key already configured can use its existing key. Both the production and recovery systems need access to the same keys. Key providers and recovery

CREATE EXTENSION IF NOT EXISTS pg_tde WITH SCHEMA public;
SELECT public.pg_tde_add_database_key_provider_file(
    'lab_file', '/var/lib/mimus-keys/vault.per');
SELECT public.pg_tde_create_key_using_database_key_provider('vault_key', 'lab_file');
SELECT public.pg_tde_set_key_using_database_key_provider('vault_key', 'lab_file');

Create encrypted tables by default with ordinary T-SQL

If the application uses temporary tables, the PG administrator can give runtime connections the heap default and require encryption on the deployment login with tde_heap. These settings take effect on new physical connections. Recycle the affected connections in any pool.

ALTER DATABASE mimus SET default_table_access_method = heap;
ALTER DATABASE mimus SET pg_tde.enforce_encryption = off;
ALTER ROLE lab_deploy IN DATABASE mimus SET default_table_access_method = tde_heap;
ALTER ROLE lab_deploy IN DATABASE mimus SET pg_tde.enforce_encryption = on;

Reconnect to vault as lab_deploy and send the following TDS batch. The first query shows the logical database and the login’s effective default. An application login granted DML privileges on the resulting table can read and write it with its own default set to heap.

SELECT DB_NAME(), pg_catalog.current_setting('default_table_access_method');
CREATE TABLE dbo.invoice (id int PRIMARY KEY, ref varchar(100) NOT NULL, body varchar(max));
CREATE UNIQUE INDEX ix_invoice_ref ON dbo.invoice(ref);
INSERT INTO dbo.invoice VALUES (1, 'A001', 'open');
UPDATE dbo.invoice SET body = 'paid' WHERE id = 1;
SELECT body FROM dbo.invoice WHERE ref = 'A001';

Expect vault, tde_heap and a final value of paid. To require every application table to go through encrypted deployment, reserve permanent DDL privileges for the deployment login and grant the runtime login only the DML privileges it needs. A login with permission to create tables can override the AM default when enforcement is off.

For a physical database dedicated to encrypted tables, the PG administrator can apply both settings at database scope. New tables default to encryption and requests for heap are rejected. The settings apply across the logical databases mapped into that physical database. Existing plaintext tables require a separate rewrite. Use the mixed configuration above for connections that need T-SQL temporary objects. Convert an existing table · Settings for temporary objects

ALTER DATABASE mimus SET default_table_access_method = tde_heap;
ALTER DATABASE mimus SET pg_tde.enforce_encryption = on;

Select one table in a mixed database

On a deployment connection with default heap and enforcement off, create dbo.chosen in a dedicated transaction. Turn implicit transactions off, finish any open transaction, and send the complete script as one TDS batch. The same file is installed in the image at /usr/mimus-19/share/mimus/examples/tds-encrypted-table.sql. Download the tested deployment script

After BEGIN TRANSACTION, the script calls this PostgreSQL configuration function over TDS. The 'true' argument limits the setting to the transaction. Other tables created in that transaction inherit the same setting. Commit or rollback restores the connection’s previous default.

SELECT pg_catalog.set_config('default_table_access_method', 'tde_heap', 'true');

The script locates the table through sys.tables and sys.schemas. It commits once the table, primary-key index, unique index, TOAST table and TOAST index are all present and encrypted. An open transaction or implicit transactions turned on yields error 50000 at entry, leaving the caller in control of its transaction. A missing or plaintext member rolls back the deployment with error 51001. Adapt both the table name and the expected indexes when using the script for another schema.

Verify the physical storage from PG

After deployment, run this query as the PG administrator in mimus. In the example’s single-db mapping, vault.dbo maps to PG schema dbo. Substitute dbo.invoice for dbo.chosen to check the role-default example. For another mapping, look up the physical schema first. Look up a schema

WITH root AS (
    SELECT oid, reltoastrelid FROM pg_class WHERE oid = 'dbo.chosen'::regclass
), members(kind, oid) AS (
    SELECT 'table', oid FROM root
    UNION ALL SELECT 'toast', reltoastrelid FROM root WHERE reltoastrelid <> 0
    UNION ALL
    SELECT CASE WHEN i.indrelid = r.reltoastrelid THEN 'toast_index'
                WHEN i.indisprimary THEN 'primary_key' ELSE 'index' END, i.indexrelid
    FROM root r JOIN pg_index i ON i.indrelid IN (r.oid, r.reltoastrelid)
)
SELECT m.kind, c.oid::regclass AS relation, a.amname,
       public.pg_tde_is_encrypted(c.oid) AS encrypted
FROM members m JOIN pg_class c ON c.oid = m.oid JOIN pg_am a ON a.oid = c.relam
ORDER BY m.kind;

Both sample tables should return five rows, all with encrypted true. The table and TOAST table use tde_heap. The three indexes use btree. During acceptance a 32,018-byte varchar(max) value in dbo.chosen occupied 17 TOAST chunks. After a restart, a new TDS connection read the same digest and committed an update visible to the next connection.

Migrate SQL Server TDE scripts

SQL Server protects its database encryption key (DEK) with a certificate or asymmetric key and enables TDE with ALTER DATABASE … SET ENCRYPTION ON. On Mimus, manage the provider and principal key through PG, check each physical relation, and configure WAL encryption separately. A TDS CREATE TABLE with WITH (ENCRYPTION=ON) or OFF fails before creating the table. SQL Server certificate and DEK commands and sys.dm_database_encryption_keys cannot manage or verify pg_tde storage. Microsoft’s TDE reference · WAL encryption on Mimus