Question: How can you replicate only one database in PostgreSQL?
Answer
PostgreSQL's built-in replication solutions, such as streaming replication or logical replication, are designed with a cluster-wide approach, meaning they replicate all databases within a PostgreSQL cluster (instance). However, if you need to replicate only a single database, your best approach is to use logical replication.
Logical Replication
Logical replication allows you to replicate data at the granularity of tables. This means you could effectively replicate all tables from one database, achieving database-level replication. Here's how you can set it up:
On the Source Server (Publisher):
- Enable Logical Replication: Edit
postgresql.conf
and setwal_level = logical
. - Configure Client Authentication: Edit
pg_hba.conf
to allow connections from the subscriber server. - Create Publication:
```sql
CREATE PUBLICATION my_publication FOR ALL TABLES;
```
Note: Execute this command for each database you want to replicate.
On the Destination Server (Subscriber):
- Create Subscription:
Assuming the publication was created on a source database namedsource_db
, you would execute:
```sql
CREATE SUBSCRIPTION my_subscription
CONNECTION 'host=source_host port=5432 dbname=source_db user=replica_user password=replica_pass'
PUBLICATION my_publication WITH (copy_data = true);
```
Execute this command in the destination database where you wish to replicate the source database's tables.
Considerations
- DDL Statements: Logical replication does not automatically replicate schema changes. You must manually apply DDL statements (e.g.,
CREATE TABLE
,ALTER TABLE
) on the subscriber database. - Selective Replication: If there are specific tables you do not wish to replicate, you can create a publication that only includes the desired tables instead of using
FOR ALL TABLES
. - Performance Impact: Logical replication can be more resource-intensive than physical replication. Test and monitor performance impacts in your environment.
While PostgreSQL does not natively support single-database physical replication out of the box, logical replication provides a flexible alternative that can meet similar needs with table-level granularity.
Was this content helpful?
Other Common PostgreSQL Questions (and Answers)
- How do you manage Postgres replication lag?
- How can I limit the number of rows updated in a PostgreSQL query?
- How does sharding work in PostgreSQL?
- How do you limit the number of rows deleted in PostgreSQL?
- How do you use the PARTITION OVER clause in PostgreSQL?
- What are PostgreSQL replication slots and how do they work?
- How can you partition an existing table in PostgreSQL?
- How do you partition a table by multiple columns in PostgreSQL?
- How do you check the replication status in PostgreSQL?
- What are the scaling limits of PostgreSQL?
- How do you scale Azure PostgreSQL?
- How do you use the limit clause in PostgreSQL to get the top N rows of a query result?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost