Neon's logical replication feature lets you replicate data from a Neon database to Lakebase Postgres on Databricks. Lakebase subscribes to a publication on your Neon database, copies the existing data, and then applies each insert, update, and delete as it happens. Use it to:
- Migrate from Neon to Databricks with minimal downtime: Keep the Lakebase database current while your application still runs on Neon, then cut over when you're ready.
- Keep a Lakebase copy of Neon data: Replicate selected tables to Lakebase for use alongside other Databricks workloads.
If a short write outage is acceptable, a dump and restore is simpler. See Migrate Neon to Lakebase. To replicate in the other direction, see Replicate data from Lakebase Postgres on Databricks to Neon.
Prerequisites
-
A Neon project with a database containing the data you want to replicate. If you're just testing this out and need some data to play with, you can use the following statements to create a table with sample data:
CREATE TABLE IF NOT EXISTS playing_with_neon(id SERIAL PRIMARY KEY, name TEXT NOT NULL, value REAL); INSERT INTO playing_with_neon(name, value) SELECT LEFT(md5(i::TEXT), 10), random() FROM generate_series(1, 10) s(i); -
A Lakebase project on Databricks for the destination database, in a workspace on AWS or Azure. See Lakebase Postgres.
-
A Lakebase Postgres role that can create subscriptions: a member of the
databricks_superuserrole, or a role granted thepg_create_subscriptionpredefined role. See Create Postgres roles. -
Read the important notices about logical replication in Neon before you begin.
-
Review our logical replication tips, based on real-world customer data migration experiences.
Compute and billing
Replication keeps your Neon compute active (no scale to zero) while Lakebase is subscribed, which can increase your bill. See Important notices about logical replication in Neon.
Prepare your source Neon database
This section describes how to prepare your source Neon database (the publisher) for replicating data to your Lakebase database (the subscriber).
Enable logical replication in the source Neon project
In the Neon project containing your source database, enable logical replication. You don't need to change anything on Lakebase. As a subscriber, Lakebase doesn't require a
wal_levelchange.important
Enabling logical replication modifies the Postgres
wal_levelconfiguration parameter, changing it fromreplicatologicalfor all databases in your Neon project. Once thewal_levelsetting is changed tological, it cannot be reverted. Enabling logical replication restarts all computes in your Neon project, meaning that active connections will be dropped and have to reconnect.To enable logical replication:
- Select your project in the Neon Console.
- Select Settings in the sidebar.
- Select Postgres, then Logical replication.
- Click Enable to enable logical replication.
You can verify that logical replication is enabled by running the following query:
SHOW wal_level; wal_level ----------- logicalCreate a Postgres role for replication
Create a dedicated Postgres role for Lakebase to connect as. The role must have the
REPLICATIONprivilege. Roles created using the Neon CLI, Console, or API are granted membership in the neon_superuser role, which has the requiredREPLICATIONprivilege. Roles created with SQL don't have this privilege by default. A role that can grant privileges, such as the project owner, can add it withALTER ROLE replication_user REPLICATION. See Replication roles.The following CLI command creates a role. To view the CLI documentation for this command, see Neon CLI roles command.
neon roles create --name replication_userGrant schema access to your Postgres role
If your replication role doesn't own the schemas and tables you're replicating from, grant access. For example, the following commands grant access to all tables in the
publicschema to thereplication_userrole: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;Granting
SELECT ON ALL TABLES IN SCHEMAinstead of naming the specific tables avoids having to add privileges later if you add tables to your publication.Create a publication on the source database
Publications define what gets replicated. Each table in the publication must have a primary key or a replica identity so that updates and deletes can be replicated.
To create a publication for a specific table:
CREATE PUBLICATION my_publication FOR TABLE playing_with_neon;To create a publication for multiple tables, provide a comma-separated list of tables:
CREATE PUBLICATION my_publication FOR TABLE users, departments;note
Defining specific tables lets you add or remove tables from the publication later, which you cannot do when creating publications with
FOR ALL TABLES.For syntax details, see CREATE PUBLICATION, in the PostgreSQL documentation.
Allow Lakebase to connect to Neon
As the subscriber, Lakebase connects out to your Neon database over the public internet. Private Link isn't supported for logical replication on Lakebase.
- On Neon: No action is needed unless you use Neon's IP Allow feature. If you do, the Lakebase connection must come from an allowed address. See Subscriber access.
- On Databricks: If your Databricks account enforces a network policy for serverless workloads, add your Neon hostname (for example,
ep-cool-darkness-123456.us-east-2.aws.neon.tech) to the allowed internet destinations. Otherwise, Databricks rejects the subscription when you create it. See Manage network policies.
Prepare your Lakebase destination database
This section describes how to prepare your destination Lakebase database (the subscriber) to receive replicated data.
Prepare your database schema
Logical replication copies data, not schema. The tables you replicate must exist in the Lakebase database with the same names and columns before you create the subscription. 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 Neon with
pg_dump --schema-onlyand load it into Lakebase. Neon dumps include ownership and privileges for Neon-specific roles such asneondb_ownerandneon_superuser, which don't exist on Lakebase, so restore with--no-ownerand--no-acl. See Restore into Lakebase for details.If you're using the sample
playing_with_neontable, you can create the same table on the Lakebase database with the following statement:CREATE TABLE IF NOT EXISTS playing_with_neon(id SERIAL PRIMARY KEY, name TEXT NOT NULL, value REAL);Create a subscription
After creating a publication on the source database, create a subscription on the Lakebase database.
-
Connect to your Lakebase database as a role that can create subscriptions (see Prerequisites). You can use the SQL editor in the Lakebase App,
psql, or another SQL client. See Connect to your database. -
Create the subscription using a
CREATE SUBSCRIPTIONstatement.CREATE SUBSCRIPTION my_subscription CONNECTION 'host=ep-cool-darkness-123456.us-east-2.aws.neon.tech port=5432 dbname=neondb user=replication_user password=AbC123dEf sslmode=verify-full sslrootcert=system' PUBLICATION my_publication;subscription_name: A name you chose for the subscription.connection_string: The connection details for the source Neon database where you defined the publication. Use the direct hostname, not the pooled one. Logical replication requires a persistent connection and isn't compatible with connection poolers, so the hostname must not include the-poolersuffix. See Connection pooling.sslmode=verify-fullwithsslrootcert=systemencrypts the connection and verifies Neon's publicly trusted certificate.publication_name: The name of the publication you created on the source Neon database.
Creating the subscription creates a replication slot on your Neon database, copies the existing rows, and then streams ongoing changes.
-
Verify the subscription was created by running the following command:
SELECT * FROM pg_stat_subscription;The subscription (
my_subscription) should be listed, confirming that your subscription has been created successfully.
Replication slots and inactive subscriptions
If the Lakebase subscription stops consuming changes, for example because it's disabled or failing, the replication slot on Neon becomes inactive. Neon removes inactive replication slots after approximately 40 hours, and the subscription must then be re-created. See Unused replication slots.
-
Test the replication
Testing your logical replication setup ensures that data is being replicated correctly from Neon to Lakebase.
-
Run some data modifying queries on the Neon database (inserts, updates, or deletes). If you're using the
playing_with_neontable, you can use this statement to insert some rows:INSERT INTO playing_with_neon(name, value) SELECT LEFT(md5(i::TEXT), 10), random() FROM generate_series(1, 10) s(i); -
Perform a row count on the Neon and Lakebase databases to make sure the result matches.
SELECT COUNT(*) FROM playing_with_neon; count ------- 20 (1 row)
Alternatively, you can run the following query on Lakebase to make sure the
last_msg_receipt_timeis as expected. For example, if you just ran an insert on Neon, thelast_msg_receipt_timeshould reflect the time of that operation.SELECT subname, received_lsn, latest_end_lsn, last_msg_receipt_time FROM pg_catalog.pg_stat_subscription;To confirm that the initial copy of every table has finished, see Monitor replication in the Databricks documentation.
-
Switch over your application
When the Lakebase database has caught up, stop writes on Neon and confirm that Lakebase is fully current.
Logical replication doesn't copy sequence values. Before you resume writes on Lakebase, reset each sequence to match the replicated data. Otherwise, new inserts into
SERIALor identity columns can generate duplicate primary keys. For example:SELECT setval(pg_get_serial_sequence('playing_with_neon', 'id'), (SELECT MAX(id) FROM playing_with_neon));Then switch your application to the Lakebase connection details. See Connect to your database.
After your application is running against Lakebase, drop the subscription on Lakebase to stop replicating. Dropping the subscription also removes the replication slot on Neon.
DROP SUBSCRIPTION my_subscription;If you no longer need the Neon project, you can delete it. See Delete a project.
Related docs
Neon
- Logical replication in Neon
- Migrate Neon to Lakebase
- Replicate data from Lakebase Postgres on Databricks to Neon
Databricks
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.








