. 6 min read
Hey Gemini, can you give me a step by step guide to replicating two databases in two different AWS accounts with WAL?
Here’s a step-by-step guide to setting up PostgreSQL replication across two AWS accounts using WAL shipping:
Assumptions:
Steps:
Account A (Primary Database):
Configure PostgreSQL for Replication:
postgresql.conf (usually located in /var/lib/pgsql/data):
wal_level = replica (or higher) to enable WAL archiving.max_wal_senders = <number> (e.g., 5) to allow connections from standby servers.wal_keep_segments = <number> (e.g., 1024) to retain enough WAL segments for replication.CREATE USER <replication_user> WITH REPLICATION PASSWORD '<strong_password>';
pg_hba.conf to allow replication connections from the standby server’s IP address or CIDR block:
host replication <replication_user> <standby_server_ip_address/cidr_block> trust
Set up WAL Archiving to S3:
aws_s3 extension for pg_basebackup (refer to PostgreSQL documentation for installation instructions).archive_command in postgresql.conf to archive WAL segments to the S3 bucket:
archive_mode = on
archive_command = 'pg_basebackup -b -D s3://<your-s3-bucket-name>/wal_archives/%f --format=tar --access-key-id=<your-access-key-id> --secret-access-key=<your-secret-access-key> --region=<your-s3-region>'
Account B (Standby Database):
Create an IAM Role for Cross-Account Access:
Configure the Standby Server:
pg_basebackup with the aws_s3 extension and the IAM role ARN from step 1 to restore from the S3 bucket.postgresql.conf on the standby:
hot_standby = on to allow read-only queries on the standby.recovery.conf (if it doesn’t exist) in the data directory and add:
standby_mode = 'on'
primary_conninfo = 'host=<primary_server_endpoint> port=5432 user=<replication_user> password=<strong_password>'
restore_command = 'pg_basebackup -b -D s3://<your-s3-bucket-name>/wal_archives/%f --format=tar --access-key-id=<your-access-key-id> --secret-access-key=<your-secret-access-key> --region=<your-s3-region>'
Verification:
SELECT pg_is_in_recovery(); query on the standby server. It should return true.Security Considerations:
This guide provides a basic framework for setting up cross-account replication with WAL shipping. You can customize it further based on your specific requirements and security needs. Remember to consult the PostgreSQL and AWS documentation for detailed instructions and best practices.
Here’s a step-by-step guide to set up WAL replication between a Crunchy Bridge source database and an AWS RDS instance as the destination:
Assumptions:
Steps:
1. Configure Crunchy Bridge (Source):
wal_level parameter is set to logical and max_replication_slots is at least 1. These settings control the level of detail captured in the WAL and allow for creating replication slots.REPLICATION privilege. Make note of the username and password.2. Configure AWS RDS (Destination):
wal_receiver_create_temp_slot to on (this allows RDS to create temporary slots for initial replication setup).wal_receiver_status_interval to a lower value (e.g., 10) for faster replication monitoring.psql to connect to your RDS instance as the master user.3. Initiate Replication from RDS:
pg_basebackup with the -X stream option to stream the backup over the network:
pg_basebackup -h <crunchy-bridge-host> -p <crunchy-bridge-port> -U <replication-user> -D /path/to/backup/directory -X stream -P -W -S <slot-name>
recovery.conf file in the data directory of your RDS instance. Add the following lines, replacing placeholders with actual values:
standby_mode = 'on'
primary_conninfo = 'host=<crunchy-bridge-host> port=<crunchy-bridge-port> user=<replication-user> password=<replication-user-password> application_name=<descriptive-name>'
recovery_target_timeline = 'latest'
4. Monitor Replication:
Additional Tips:
SSL/TLS: For enhanced security, configure SSL/TLS encryption for the replication connection between Crunchy Bridge and your RDS instance.
Monitoring: Set up monitoring for replication lag to ensure your RDS instance stays in sync with your Crunchy Bridge database.
Testing: Periodically test your failover scenario by promoting your RDS instance to a standalone primary to ensure everything works as expected.
Important Notes:
Crunchy Bridge Limitations: Contact Crunchy Bridge support to confirm their recommended approach for setting up replication to external services and any limitations that might apply.
AWS RDS Restrictions: Be mindful of AWS RDS restrictions; for example, you cannot directly modify the pg_hba.conf file on an RDS instance.
Security Best Practices: Always follow security best practices, including using strong passwords, least privilege principles, and encrypting sensitive data in transit and at rest.