AWS Builder Center

From Unencrypted to Encrypted: A 5-Minute Migration Story for Multi-TB RDS PostgreSQL Instance

Learn how to migrate an unencrypted multi-terabyte RDS PostgreSQL database to an encrypted instance with less than 5 minutes of downtime using AWS DMS homogeneous migration. This real-world customer story demonstrates how native PostgreSQL tools and logical replication overcome the challenge that RDS instances can only be encrypted at creation time, not afterward.

Introduction

While working with a customer recently, I encountered a common yet challenging scenario: they needed to encrypt their multi-terabyte Amazon RDS for PostgreSQL database, but their business requirements demanded near-zero downtime. This article shares the solution that worked exceptionally well, leveraging AWS Database Migration Service (DMS) homogeneous migration.

The Challenge

Amazon RDS has a fundamental limitation: you can only encrypt a database instance when you create it, not after it's already running. For organizations with unencrypted databases containing sensitive data, this presents a significant operational challenge.

Considered Approaches

Before settling on our solution, we evaluated several migration approaches:
  • Snapshot and Restore: While straightforward, this method requires extended downtime proportional to your database size. For our customer's multi-terabyte database, restoration could take hours, unacceptable for their 24/7 operations.
  • Standard AWS DMS Migration: Traditional DMS migrations work well for heterogeneous migrations, but PostgreSQL's complex data types (ENUMs, arrays, composite types) can cause compatibility issues during the migration process.
  • Blue/Green Deployments: Amazon RDS offers blue/green deployments for PostgreSQL, but this feature doesn't currently support migrating from unencrypted to encrypted instances.

The Recommended Solution: DMS Homogeneous Migration

DMS homogeneous migration emerged as the ideal solution because it uses native PostgreSQL tools combined with logical replication for continuous data synchronization. This approach provides full feature compatibility while minimizing downtime to just the cutover window.


Why it works: DMS homogeneous migration uses native PostgreSQL tools ( pg_dump ,  pg_restore ) for the initial load and PostgreSQL's built-in logical replication for ongoing changes. This ensures:
  • Full data type compatibility: Native tools handle all PostgreSQL data types correctly
  • Minimal downtime: Continuous replication keeps databases synchronized
  • Zero data loss: Transactional consistency throughout the migration



Implementation

Migration Process Overview

The migration consists of three phases: Setup, Migration, and Cutover.

Phase 1: Setup

1. Enable Logical Replication on Source Database
Logical replication is essential for capturing ongoing changes. Configure these parameters in your source database:
-- Verify current settings
SELECT name, setting
FROM pg_settings
WHERE name IN ('wal_level', 'max_replication_slots', 'max_wal_senders');

-- Required settings (set via parameter group):
-- wal_level = logical
-- max_replication_slots >= 5 (adjust based on number of tasks)
-- max_wal_senders >= 5
For RDS PostgreSQL, set  rds.logical_replication = 1  in your custom parameter group and reboot the instance.
2. Provision Encrypted Target RDS Instance
Create a new RDS PostgreSQL instance with encryption enabled:
  • Use the same PostgreSQL version as your source
  • Select appropriate KMS key for encryption
  • Match or exceed source instance specifications
  • Place in the same VPC for optimal network performance
3. Configure DMS Replication Infrastructure
Set up the following DMS components:
  • Subnet group: Define subnets for DMS replication instance
  • Instance profile: Configure IAM roles and permissions
  • Data providers: Create source and target database connections
  • Migration project: Link all components together

Phase 2: Migration Phases

Full Load: Initial Data Transfer
DMS homogeneous migration uses  pg_dump  to read schema objects and data from your source database:
pg_dump → Serverless DMS environment → pg_restore → Target database
This phase transfers your complete database schema and initial data set. The serverless nature means AWS automatically scales resources based on your database size.
CDC: Continuous Replication via Logical Replication
After the full load completes, DMS automatically switches to a publisher-subscriber model using PostgreSQL's native logical replication:
  • Publisher (source): Creates a publication for tables being migrated
  • Subscriber (target): Subscribes to the publication and applies changes in real-time
  • Replication slot: Maintains the position in the WAL stream to ensure no data loss
This phase keeps your databases synchronized while you validate the migration.
Pre-Cutover Validation
Before switching over:
  1. Verify replication lag is minimal (< 1 second)
  1. Validate data integrity using row counts and checksums
  1. Test critical application queries against the target database
  1. Prepare rollback procedure
Cutover Execution
The actual cutover window for our customer was less than 5 minutes:
  1. Pause application traffic (2 minutes)
  1. Wait for replication lag to reach zero (1-2 minutes)
  1. Update connection strings to point to encrypted instance (1 minute)
  1. Resume application traffic (immediate)
Post-Cutover
  • Monitor application performance and error rates
  • Keep the source database running for 24-48 hours as a safety net
  • Once confident, stop DMS replication and decommission the old instance



Key Advantages

Native PostgreSQL Tool Compatibility

By using  pg_dump ,  pg_restore , and logical replication, DMS homogeneous migration handles all PostgreSQL data types flawlessly:
  • ENUMs: Preserved exactly as defined
  • Arrays: Multi-dimensional arrays migrate correctly
  • Composite types: Custom types maintain their structure
  • JSON/JSONB: Complex JSON documents transfer intact
  • PostGIS types: Geospatial data migrates successfully
No application code changes required, my customer's database works identically on the encrypted instance.

Continuous Replication Minimizes Downtime

Unlike snapshot-based approaches that require hours of downtime, continuous replication keeps databases synchronized in real-time. Downtime is limited to:
  • Brief application pause (1-2 minutes)
  • Connection string update (< 1 minute)
  • Application restart (immediate)
Total downtime: < 5 minutes for multi-terabyte databases.

Full Data Fidelity

PostgreSQL's logical replication ensures transactional consistency:
  • Changes are applied in the same order as the source
  • Transactions are preserved atomically
  • No data loss during the migration process



Results and Conclusion

Migration Outcomes

Our customer's migration achieved exceptional results:
  • Downtime: Less than 5 minutes for a multi-terabyte database
  • Data integrity: Zero data loss, all data types migrated successfully
  • Encryption: Full encryption at rest using AWS KMS
  • Business impact: Minimal disruption to 24/7 operations

Key Takeaway

DMS homogeneous migration is the recommended approach for production PostgreSQL encryption migrations. It combines the reliability of native PostgreSQL tools with the flexibility of continuous replication, delivering minimal downtime without sacrificing data integrity.
This solution is particularly valuable when:
  • Database size makes snapshot-restore impractical (> 1 TB)
  • Downtime requirements are stringent (< 15 minutes)
  • Complex PostgreSQL data types are in use
  • Regulatory compliance requires encryption at rest

Best Practices

Based on my experience, follow these guidelines for successful migrations:
1. Test in Non-Production First
  • Run complete migration in staging environment
  • Validate all data types and application queries
  • Measure actual migration times
2. Validate Data Integrity Before Cutover
  • Compare row counts for all key tables
  • Run application-specific validation queries
  • Test critical business workflows on target database
3. Schedule Cutover During Low-Traffic Periods
  • Choose maintenance windows with minimal user activity
  • Communicate clearly with stakeholders about timing
  • Have rollback procedures documented and tested
Disclaimer:
While DMS homogeneous migrations are designed to migrate data between equivalent database engines (PostgreSQL and MySQL) on Amazon RDS, Amazon Aurora, or Amazon EC2 using native engine tools, this approach is not officially documented by AWS as a method for migrating from unencrypted to encrypted RDS instances; however, it has been successfully validated in this customer implementation and should be thoroughly tested before production use.



Additional Resources

By leveraging DMS homogeneous migration, you can achieve the seemingly impossible: encrypting large production PostgreSQL databases with near-zero downtime and complete data fidelity.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article