---
title: Replicate data from Lakebase
description: Publish changes from Lakebase to an external Postgres subscriber with logical replication, using Lakebase as the publisher.
canonical_url: https://docs.databricks.com/aws/en/oltp/projects/logical-replication-publication
---

# Replicate data from Lakebase

This guide shows you how to publish changes from a Lakebase database to an external Postgres subscriber using logical replication. Lakebase acts as the publisher: it defines a publication, and the external subscriber connects in to receive the changes.

## <a id="prereqs" />Prerequisites

* A Lakebase project with the database you want to publish from, containing the tables you want to publish.
* A Lakebase Postgres role that is a member of the `databricks_superuser` role, such as the [project owner's role](https://docs.databricks.com/aws/en/oltp/projects/postgres-roles.md#roles), to create the publication. To grant `databricks_superuser` to another role, see [Create Postgres roles](https://docs.databricks.com/aws/en/oltp/projects/postgres-roles.md#create) or [Update a role](https://docs.databricks.com/aws/en/oltp/projects/postgres-roles.md#update-role). This is the role you connect as, not the role the subscriber uses. You create the subscriber's replication role in the [first step](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-publication.md#create-role).
* Password connections enabled on the project. The subscriber connects with a native Postgres password role, and password connections are disabled by default for new projects. See [Manage password connections](https://docs.databricks.com/aws/en/oltp/projects/manage-roles.md#block-connections).
* An external Postgres database to act as the subscriber. The subscriber can also be a Lakebase database in a different region. Replication between Lakebase databases in the same region is not supported.

Logical replication is supported in Lakebase, which runs at `wal_level = logical` by default, so there is no per-project step to turn on publishing.

### <a id="create-role" />Create a replication role

Create a dedicated role for the external subscriber to connect as. The subscriber connects with a Postgres username and password, so create a **native Postgres password role**, not an OAuth role. OAuth roles authenticate with short-lived Databricks tokens, which the password-based subscription connection string cannot use. The role must have the `REPLICATION` attribute. Roles you create in the Lakebase App, CLI, or API have this attribute, whether or not you grant them `databricks_superuser`. In the Lakebase App **Add role** > **Password** tab, enter a role name. You do not need to select `databricks_superuser`. See [Create a native Postgres password role](https://docs.databricks.com/aws/en/oltp/projects/postgres-roles.md#create-native).

To set the password yourself, create the role with SQL instead. Include the `REPLICATION` attribute, because roles created with SQL do not receive it automatically:

```sql title="PostgreSQL"
CREATE ROLE replication_user WITH LOGIN REPLICATION PASSWORD '<password>';
```

If the role does not own the tables you replicate, grant it read access:

```sql title="PostgreSQL"
GRANT USAGE ON SCHEMA public TO replication_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO replication_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO replication_user;
```

### <a id="publish" />Create a publication

Connect to the Lakebase database as a member of the `databricks_superuser` role, then create a publication for the tables you want to replicate. Each table must have a `PRIMARY KEY` or a replica identity set. See [Replica identity](https://www.postgresql.org/docs/current/logical-replication-publication.html#LOGICAL-REPLICATION-PUBLICATION-REPLICA-IDENTITY) in the Postgres documentation.

```sql title="PostgreSQL"
CREATE PUBLICATION my_publication FOR TABLE orders, customers;
```

### <a id="ingress" />Allow the subscriber to connect

The external subscriber connects in to Lakebase through the Databricks edge proxy, which enforces the workspace IP access list. Allow the subscriber's IP address in the workspace IP access list so it can reach your Lakebase database. See [Manage the IP access list](https://docs.databricks.com/aws/en/security/network/front-end/ip-access-list.md).

Private Link is not supported for the subscriber connection. The subscriber connects over the public Lakebase endpoint.

### <a id="subscribe" />Subscribe from the external database

On the external Postgres database, create the target tables with the same names and columns as the tables you publish. Create them empty: the initial copy fails on duplicate keys or constraint violations if a target table already contains rows. You can create the tables manually, or dump the schema from Lakebase and load it into the subscriber. See [Dump the schema only](https://docs.databricks.com/aws/en/oltp/projects/pg-dump-restore.md#schema-only).

Then, as a role that can create subscriptions on the external database, create a subscription that connects to Lakebase:

```sql title="PostgreSQL"
CREATE SUBSCRIPTION my_subscription
CONNECTION 'host=<lakebase-host> port=5432 dbname=<lakebase-db> user=replication_user password=<password> sslmode=verify-full sslrootcert=system'
PUBLICATION my_publication;
```

Use a direct connection for the subscriber. Logical replication requires a persistent connection, so `<lakebase-host>` must be your direct endpoint hostname, not the `-pooler` connection pooler hostname. See [Connection pooling](https://docs.databricks.com/aws/en/oltp/projects/connection-pooling.md).

> [!NOTE]
>
> Lakebase enforces TLS on all connections, so traffic is always encrypted with no action needed. For the strongest protection, connect with `sslmode=verify-full`, which also verifies the Lakebase server certificate against your system's root store. Lakebase uses a publicly trusted certificate, so no extra certificate is needed. The weaker `sslmode=require` encrypts but does not verify the server.

### <a id="verify" />Verify replication

On the external subscriber, confirm the existing rows arrived:

```sql title="PostgreSQL"
SELECT COUNT(*) FROM orders;
```

Insert or update a row on Lakebase, then query the subscriber again. The change appears on the subscriber within a few seconds.

Check subscription status and lag on the subscriber:

```sql title="PostgreSQL"
SELECT subname, received_lsn, latest_end_lsn, last_msg_receipt_time
FROM pg_catalog.pg_stat_subscription;
```

To confirm the initial copy has finished, run the per-table sync status query in [Monitor replication](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-manage.md#monitor) on the subscriber and wait until every table reads `Ready (fully live)`.

The subscriber's connection maintains a replication slot on the Lakebase publisher. While a subscriber is actively replicating, the Lakebase compute does not scale to zero. See [Logical replication and scale to zero](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-manage.md#scale-zero). If the subscriber stops consuming changes, the slot becomes inactive and Lakebase removes it after about 40 hours. See [Automatic removal of inactive slots](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-manage.md#auto-slot-removal).

## Next steps

* **Manage replication**: Monitor publications and slots, and troubleshoot. See [Manage and monitor logical replication](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-manage.md).
* **Migrate into Lakebase**: Replicate an external Postgres database into Lakebase. See [Replicate data into Lakebase](https://docs.databricks.com/aws/en/oltp/projects/logical-replication-subscription.md).
* **Compare methods**: Choose the right way to move data in and out of Lakebase. See [Move data in and out of Lakebase](https://docs.databricks.com/aws/en/oltp/projects/move-data-in-and-out.md).
