|
| 1 | +--- |
| 2 | +title: Set up a logical streaming replica |
| 3 | +description: Use Xata's streaming replication to keep your database continuously synchronized with real-time changes. |
| 4 | +--- |
| 5 | + |
| 6 | +This guide shows you how to set up continuous logical streaming replication from your production PostgreSQL database to Xata, enabling real-time data synchronization with optional anonymization. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## 1. Prerequisites |
| 11 | + |
| 12 | +- A Xata account ([sign up here](https://console.xata.io)) |
| 13 | +- The [Xata CLI](/cli) installed: |
| 14 | + ```bash |
| 15 | + curl -fsSL https://xata.io/install.sh | bash |
| 16 | + ``` |
| 17 | +- A PostgreSQL database with: |
| 18 | + - Logical replication enabled |
| 19 | + - Role with permissions to create a replication slow (`xata clone stream` command does that automatically) |
| 20 | + - Network connectivity from Xata to your database |
| 21 | + |
| 22 | +## 2. Enable logical replication on source database |
| 23 | + |
| 24 | +First, ensure your source PostgreSQL database has logical replication enabled. You'll need to set these parameters: |
| 25 | + |
| 26 | +```sql |
| 27 | +-- Check current settings |
| 28 | +SHOW wal_level; |
| 29 | +SHOW max_replication_slots; |
| 30 | +SHOW max_wal_senders; |
| 31 | +``` |
| 32 | + |
| 33 | +If not already configured, update your PostgreSQL configuration: |
| 34 | + |
| 35 | +```sql |
| 36 | +ALTER SYSTEM SET wal_level = logical; |
| 37 | +ALTER SYSTEM SET max_replication_slots = 10; |
| 38 | +ALTER SYSTEM SET max_wal_senders = 10; |
| 39 | +``` |
| 40 | + |
| 41 | +Restart your PostgreSQL instance for the changes to take effect. |
| 42 | + |
| 43 | +## 3. Create a Xata project and branch |
| 44 | + |
| 45 | +In the Console, create a new project and then click the **Create main branch** button to create the PostgreSQL instance. |
| 46 | + |
| 47 | +For streaming replication, consider using at least 1 replica to ensure high availability during continuous synchronization. Select an instance size that can handle your expected write throughput. |
| 48 | + |
| 49 | +> **Note:** Streaming replication maintains a persistent connection to your source database. Ensure your network allows stable, long-lived connections between Xata and your PostgreSQL instance. |
| 50 | +
|
| 51 | +## 4. Configure the Xata CLI |
| 52 | + |
| 53 | +Authenticate the CLI by running: |
| 54 | + |
| 55 | +```sh |
| 56 | +xata auth login |
| 57 | +``` |
| 58 | + |
| 59 | +Initialize the project by running: |
| 60 | + |
| 61 | +```sh |
| 62 | +xata init |
| 63 | +``` |
| 64 | + |
| 65 | +## 5. Configure streaming replication |
| 66 | + |
| 67 | +Generate a configuration for the streaming process: |
| 68 | + |
| 69 | +```bash |
| 70 | +xata clone config --source-url $CONN_STRING |
| 71 | +``` |
| 72 | + |
| 73 | +Where `CONN_STRING` is your PostgreSQL connection string with replication permissions. |
| 74 | + |
| 75 | +The configuration prompt will ask you to: |
| 76 | + |
| 77 | +- Select tables to replicate |
| 78 | +- Set up transformation pipelines i.e. anonymization rules |
| 79 | + |
| 80 | +This creates a configuration file at `.xata/clone.yaml` that you can further customize. |
| 81 | + |
| 82 | +## 6. Initialize and start streaming |
| 83 | + |
| 84 | +```bash |
| 85 | +xata clone stream --source-url $CONN_STRING |
| 86 | +``` |
| 87 | + |
| 88 | +This command will: |
| 89 | + |
| 90 | +- Create an initial snapshot of your specified tables |
| 91 | +- Set up the streaming pipeline |
| 92 | +- Begin continuous replication |
| 93 | + |
| 94 | +## 7. Advanced configuration |
| 95 | + |
| 96 | +### Filtering specific tables |
| 97 | + |
| 98 | +To stream only specific tables, use the `--filter-tables` flag: |
| 99 | + |
| 100 | +```bash |
| 101 | +xata clone stream --source-url $CONN_STRING \ |
| 102 | + --filter-tables "users.*,orders.*,products.*" |
| 103 | +``` |
| 104 | + |
| 105 | +If this option is not specified it defaults to `*.*` |
| 106 | + |
| 107 | +### Custom transformations |
| 108 | + |
| 109 | +Edit your `.xata/clone.yaml` file to add custom transformations: |
| 110 | + |
| 111 | +```yaml |
| 112 | +transforms: |
| 113 | + - table: users |
| 114 | + columns: |
| 115 | + - name: email |
| 116 | + transformer: mask_email |
| 117 | + - name: phone |
| 118 | + transformer: redact |
| 119 | + - table: orders |
| 120 | + columns: |
| 121 | + - name: credit_card |
| 122 | + transformer: mask_credit_card |
| 123 | +``` |
| 124 | +
|
| 125 | +### Running with Docker |
| 126 | +
|
| 127 | +For production deployments, consider running the streaming process in a containerized environment: |
| 128 | +
|
| 129 | +```bash |
| 130 | +docker run -d \ |
| 131 | + --name xata-stream \ |
| 132 | + --restart unless-stopped \ |
| 133 | + -v $(pwd)/.xata:/config \ |
| 134 | + xata/cli clone stream \ |
| 135 | + --source-url $CONN_STRING |
| 136 | +``` |
| 137 | + |
| 138 | +## 10. Handling failures and recovery |
| 139 | + |
| 140 | +If the streaming connection is interrupted, the replication slot ensures no data is lost. Simply restart the streaming command: |
| 141 | + |
| 142 | +```bash |
| 143 | +xata clone stream --source-url $CONN_STRING |
| 144 | +``` |
| 145 | + |
| 146 | +The process will resume from where it left off, catching up with any changes that occurred during the downtime. |
| 147 | +However, if the too much lag accumulates then the Postgres server might slow down as it has to do both catching up on the lag and its normal operations. |
| 148 | + |
| 149 | +If you terminate the `xata clone stream` process and do not wish to run streaming replication again, clean up the replication slot and |
| 150 | +other `pgstream` objects using `xata stream destroy` command. |
| 151 | + |
| 152 | +Not cleaning up the replication slot will cause the WAL to be aggregated continuously and that would lead to full disk space. Use options like `max_slot_wal_keep_size` |
| 153 | +to keep the max WAL size in check. |
| 154 | + |
| 155 | +## Summary |
| 156 | + |
| 157 | +- You now have real-time streaming replication (Postgres's logical replication) from your PostgreSQL database to Xata |
| 158 | +- Changes in your source database are automatically synchronized |
| 159 | +- Your data can be anonymized in transit using configurable transformers |
| 160 | +- The replication slot ensures no data loss during network interruptions |
| 161 | + |
| 162 | +For more details on advanced streaming configurations and monitoring, see the [clone command documentation](/cli/clone). |
0 commit comments