Skip to main content
Build your agent on Neon and provision backends you fully control. See how Ideavo does it
/Integrations & tooling/Integrations (3rd party)/Replicate to Neon/Lakebase to Neon

Replicate data from Lakebase Postgres on Databricks to Neon

Use logical replication to migrate or stream data from Lakebase Postgres on Databricks to a Neon database

View as Markdown

Neon's logical replication feature lets you replicate data from Lakebase Postgres on Databricks to a Neon database. Neon subscribes to a publication on your Lakebase database, copies the existing data, and then applies each insert, update, and delete as it happens. Use it to:

  • Migrate from Databricks to Neon with minimal downtime: Keep the Neon database current while your application still runs on Lakebase, then cut over when you're ready.
  • Keep a Neon copy of Lakebase data: Replicate selected tables to Neon for use with apps and agents built on Neon.

To replicate in the other direction, see Replicate data from Neon to Lakebase Postgres on Databricks.

Prerequisites

  • A Lakebase project on Databricks with a database containing the data you want to replicate, in a workspace on AWS or Azure. 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 Postgres role that is a member of the databricks_superuser role, to create the publication. See Create Postgres roles.

  • A destination Neon project. For information about creating a Neon project, see Create a project.

  • 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 usage on Lakebase

While Neon is subscribed, the Lakebase compute that publishes changes stays active and doesn't scale to zero. See Logical replication and scale to zero in the Databricks documentation.

  1. Prepare your source Lakebase database

    This section describes how to prepare your source Lakebase database (the publisher) for replicating data to your Neon database (the subscriber).

    Lakebase runs at wal_level = logical by default, so you don't need to enable logical replication on the Lakebase project. You also don't need to enable it on the Neon project, because Neon is only the subscriber.

    Enable password connections

    Neon connects to Lakebase with a Postgres username and password. Password connections are disabled by default for new Lakebase projects. Enable them for your project. See Manage password connections.

    Create a Postgres role for replication

    Create a dedicated role for Neon to connect as. Use a native Postgres password role, not an OAuth role. OAuth roles authenticate with short-lived Databricks tokens, which a subscription connection string can't use.

    The role must have the REPLICATION attribute. Roles you create in the Lakebase App, CLI, or API have this attribute automatically. See Create a native Postgres password role.

    To set the password yourself, create the role with SQL instead. Include the REPLICATION attribute, because roles created with SQL don't receive it automatically:

    CREATE ROLE replication_user WITH LOGIN REPLICATION PASSWORD 'AbC123dEf';

    Grant 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 public schema to the replication_user role:

    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 SCHEMA instead 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

    Connect to the Lakebase database as a member of the databricks_superuser role, then create a publication. 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 connections from Neon

    Neon connects to Lakebase over the public internet. Private Link isn't supported for logical replication on Lakebase.

    If your Databricks workspace uses an IP access list, add the NAT gateway IP addresses for your Neon project's region to it. Neon uses several NAT gateway IP addresses per region for outbound communication, and the list changes over time, so add all of the addresses listed for your region. See NAT Gateway IP addresses for the list, and Manage the IP access list in the Databricks documentation for instructions.

  2. Prepare your Neon destination database

    This section describes how to prepare your destination Neon 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 Neon 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 use pg_dump and pg_restore to dump the schema from Lakebase and load it into Neon. See Import a database schema for instructions. Lakebase dumps can include ownership and privileges for Databricks-specific roles that don't exist on Neon, so restore with --no-owner and --no-acl. See Database object ownership considerations.

    If you're using the sample playing_with_neon table, you can create the same table on the Neon 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 Neon database.

    1. Use the Neon SQL Editor, psql, or another SQL client to connect to your Neon database. Roles created using the Neon CLI, Console, or API, including the default role created with your project, can create subscriptions.

    2. Create the subscription using a CREATE SUBSCRIPTION statement.

      CREATE SUBSCRIPTION my_subscription
      CONNECTION 'host=<lakebase-host> port=5432 dbname=databricks_postgres 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 Lakebase database where you defined the publication. Replace <lakebase-host> with your Lakebase endpoint hostname, and databricks_postgres with your database name if it's different. You can find both in the Connect dialog for your Lakebase project. Use the direct hostname, not the -pooler connection pooler hostname, because logical replication requires a persistent connection. sslmode=verify-full with sslrootcert=system encrypts the connection and verifies Lakebase's publicly trusted certificate. sslrootcert=system requires Postgres 16 or later on your Neon database. If your Neon project runs Postgres 14 or 15, use sslmode=require instead.
      • publication_name: The name of the publication you created on the source Lakebase database.

      Creating the subscription creates a replication slot on your Lakebase database, copies the existing rows, and then streams ongoing changes.

    3. 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 Neon subscription stops consuming changes, for example because it's disabled or failing, the replication slot on Lakebase becomes inactive. Lakebase removes inactive replication slots after about 40 hours, and the subscription must then be re-created. See Automatic removal of inactive slots in the Databricks documentation.

  3. Test the replication

    Testing your logical replication setup ensures that data is being replicated correctly from Lakebase to Neon.

    1. Run some data modifying queries on the Lakebase database (inserts, updates, or deletes). If you're using the playing_with_neon table, 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);
    2. Perform a row count on the Lakebase and Neon 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 Neon to make sure the last_msg_receipt_time is as expected. For example, if you just ran an insert on Lakebase, the last_msg_receipt_time should reflect the time of that operation.

    SELECT subname, received_lsn, latest_end_lsn, last_msg_receipt_time FROM pg_catalog.pg_stat_subscription;
  4. Switch over your application

    When the Neon database has caught up, stop writes on Lakebase and confirm that Neon is fully current.

    Logical replication doesn't copy sequence values. Before you resume writes on Neon, reset each sequence to match the replicated data. Otherwise, new inserts into SERIAL or 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 Neon connection details. You can find them by clicking the Connect button in the Console nav to open the Connect to your branch modal. See Connect from any application.

    After your application is running against Neon, drop the subscription on Neon to stop replicating. Dropping the subscription also removes the replication slot on Lakebase.

    DROP SUBSCRIPTION my_subscription;

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.

Last updated on

Was this page helpful?
Edit on GitHub