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 from Neon/Neon to Lakebase

Replicate data from Neon to Lakebase Postgres on Databricks

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

View as Markdown

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_superuser role, or a role granted the pg_create_subscription predefined 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.

  1. 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_level change.

    important

    Enabling logical replication modifies the Postgres wal_level configuration parameter, changing it from replica to logical for all databases in your Neon project. Once the wal_level setting is changed to logical, 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:

    1. Select your project in the Neon Console.
    2. Select Settings in the sidebar.
    3. Select Postgres, then Logical replication.
    4. Click Enable to enable logical replication.

    You can verify that logical replication is enabled by running the following query:

    SHOW wal_level;
     wal_level
    -----------
     logical

    Create a Postgres role for replication

    Create a dedicated Postgres role for Lakebase to connect as. The role must have the REPLICATION privilege. Roles created using the Neon CLI, Console, or API are granted membership in the neon_superuser role, which has the required REPLICATION privilege. 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 with ALTER 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_user

    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

    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.
  2. 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-only and load it into Lakebase. Neon dumps include ownership and privileges for Neon-specific roles such as neondb_owner and neon_superuser, which don't exist on Lakebase, so restore with --no-owner and --no-acl. See Restore into Lakebase for details.

    If you're using the sample playing_with_neon table, 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.

    1. 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.

    2. Create the subscription using a CREATE SUBSCRIPTION statement.

      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 -pooler suffix. See Connection pooling. sslmode=verify-full with sslrootcert=system encrypts 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.

    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 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.

  3. Test the replication

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

    1. Run some data modifying queries on the Neon 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 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_time is as expected. For example, if you just ran an insert on Neon, 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;

    To confirm that the initial copy of every table has finished, see Monitor replication in the Databricks documentation.

  4. 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 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 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.

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