--- title: "Ingest data from Neon" description: "How to stream data from Neon to Materialize" menu: main: parent: "postgresql" name: "Neon" identifier: "pg-neon" --- {{< tip >}} {{< guided-tour-blurb-for-ingest-data >}} {{< /tip >}} [Neon](https://neon.tech) is a fully managed serverless PostgreSQL provider. It separates compute and storage to offer features like **autoscaling**, **branching** and **bottomless storage**. This page shows you how to stream data from a Neon database to Materialize using the [PostgreSQL source](/sql/create-source/postgres/). ## Before you begin - Make sure you have [a Neon account](https://neon.tech). - Make sure you have access to your Neon instance via [`psql`](https://www.postgresql.org/docs/current/app-psql.html) or the SQL editor in the Neon Console. ## A. Configure Neon The steps in this section are specific to Neon. You can run them by connecting to your Neon database using a `psql` client or the SQL editor in the Neon Console. ### 1. Enable logical replication {{< warning >}} Enabling logical replication applies **globally** to all databases in your Neon project, and **cannot be reverted**. It also **restarts all computes**, which means that any active connections are dropped and have to reconnect. {{< /warning >}} Materialize uses PostgreSQL's [logical replication](https://www.postgresql.org/docs/current/logical-replication.html) protocol to track changes in your database and propagate them to Materialize. As a first step, you need to make sure logical replication is enabled in Neon. 1. Select your project in the Neon Console. 2. On the Neon **Dashboard**, select **Settings**. 3. Select **Logical Replication**. 4. Click **Enable** to enable logical replication. You can verify that logical replication is enabled by running: ```sql SHOW wal_level; ``` The result should be: ``` wal_level ----------- logical ``` ### 2. Create a publication and a replication user Once logical replication is enabled, the next step is to create a publication with the tables that you want to replicate to Materialize. You'll also need a user for Materialize with sufficient privileges to manage replication. 1. For each table that you want to replicate to Materialize, set the [replica identity](https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-REPLICA-IDENTITY) to `FULL`: ```postgres ALTER TABLE REPLICA IDENTITY FULL; ``` ```postgres ALTER TABLE REPLICA IDENTITY FULL; ``` `REPLICA IDENTITY FULL` ensures that the replication stream includes the previous data of changed rows, in the case of `UPDATE` and `DELETE` operations. This setting enables Materialize to ingest Neon data with minimal in-memory state. However, you should expect increased disk usage in your Neon database. 2. Create a [publication](https://www.postgresql.org/docs/current/logical-replication-publication.html) with the tables you want to replicate: _For specific tables:_ ```postgres CREATE PUBLICATION mz_source FOR TABLE , ; ``` _For all tables in the database:_ ```postgres CREATE PUBLICATION mz_source FOR ALL TABLES; ``` The `mz_source` publication will contain the set of change events generated from the specified tables, and will later be used to ingest the replication stream. Be sure to include only the tables you need. If the publication includes additional tables, Materialize will waste resources on ingesting and then immediately discarding the data. 3. Create a dedicated user for Materialize, if you don't already have one. The default user created with your Neon project and users created using the Neon CLI, Console or API are granted membership in the [`neon_superuser`](https://neon.tech/docs/manage/roles#the-neonsuperuser-role) role, which has the required `REPLICATION` privilege. While you can use the default user for replication, we recommend creating a dedicated user for security reasons. {{< tabs >}} {{< tab "Neon CLI">}} Use the [`roles create` CLI command](https://neon.tech/docs/reference/cli-roles) to create a new role. ```bash neon roles create --name materialize ``` {{< /tab >}} {{< tab "Neon Console">}} 1. Navigate to the [Neon Console](https://console.neon.tech). 2. Select a project. 3. Select **Branches**. 4. Select the branch where you want to create the role. 5. Select the **Roles & Databases** tab. 6. Click **Add Role**. 7. In the role creation dialog, specify the role name as "materialize". 8. Click **Create**. The role is created, and you are provided with the password for the role. {{< /tab >}} {{< tab "API">}} Use the [`roles` endpoint](https://api-docs.neon.tech/reference/createprojectbranchrole) to create a new role. ```bash curl 'https://console.neon.tech/api/v2/projects//branches//roles' \ -H 'Accept: application/json' \ -H "Authorization: Bearer $NEON_API_KEY" \ -H 'Content-Type: application/json' \ -d '{ "role": { "name": "materialize" } }' | jq ``` {{< /tab >}} {{< /tabs >}} 4. Grant the user the required permissions on the schema(s) you want to replicate: ```postgres GRANT USAGE ON SCHEMA public TO materialize; GRANT SELECT ON ALL TABLES IN SCHEMA public TO materialize; ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO materialize; ``` Granting `SELECT ON ALL TABLES IN SCHEMA` instead of on specific tables avoids having to add privileges later if you add tables to your publication. ## B. (Optional) Configure network security {{< note >}} If you are prototyping and your Neon instance is publicly accessible, **you can skip this step**. For production scenarios, we recommend using [**IP Allow**](https://neon.tech/docs/introduction/ip-allow) to limit the IP addresses that can connect to your Neon instance. {{}} ### Allow Materialize IPs If you use Neon's [**IP Allow**](https://neon.tech/docs/introduction/ip-allow) feature to limit the IP addresses that can connect to your Neon instance, you will need to allow inbound traffic from Materialize IP addresses. 1. In the [Materialize console's SQL Shell](https://console.materialize.com/), or your preferred SQL client connected to Materialize, run the following query to find the static egress IP addresses, for the Materialize region you are running in: ```mzsql SELECT * FROM mz_egress_ips; ``` 2. In your Neon project, add the IPs to your **IP Allow** list: 1. Select your project in the Neon Console. 2. On the Neon **Dashboard**, select **Settings**. 3. Select **IP Allow**. 4. Add each Materialize IP address to the list. ## C. Ingest data in Materialize The steps in this section are specific to Materialize. You can run them in the [Materialize console's SQL Shell](https://console.materialize.com/) or your preferred SQL client connected to Materialize. ### 1. (Optional) Create a cluster {{< note >}} If you are prototyping and already have a cluster to host your PostgreSQL source (e.g. `quickstart`), **you can skip this step**. For production scenarios, we recommend separating your workloads into multiple clusters for [resource isolation](/sql/create-cluster/#resource-isolation). {{< /note >}} {{% postgres-direct/create-a-cluster %}} ### 2. Start ingesting data Now that you've configured your database network and created an ingestion cluster, you can connect Materialize to your Neon database and start ingesting data. 1. Run the [`CREATE SECRET`](/sql/create-secret/) command to securely store the password for the `materialize` PostgreSQL user you created [earlier](#2-create-a-publication-and-a-replication-user): ```mzsql CREATE SECRET pgpass AS ''; ``` You can access the password for your Neon user from the **Connection Details** widget on the Neon **Dashboard**. 2. Use the [`CREATE CONNECTION`](/sql/create-connection/) command to create a connection object with access and authentication details for Materialize to use: ```mzsql CREATE CONNECTION pg_connection TO POSTGRES ( HOST '', PORT 5432, USER '', PASSWORD SECRET pgpass, SSL MODE 'require', DATABASE '' ); ``` You can find the connection details for your replication user in the **Connection Details** widget on the Neon **Dashboard**. A Neon connection string looks like this: ```bash postgresql://materialize:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require ``` - Replace `` with your Neon hostname (e.g., `ep-cool-darkness-123456.us-east-2.aws.neon.tech`). - Replace `` with the dedicated replication user (e.g., `materialize`). - Replace `` with the name of the database containing the tables you want to replicate to Materialize (e.g., `dbname`). 3. Use the [`CREATE SOURCE`](/sql/create-source/) command to connect Materialize to your Neon database and start ingesting data from the publication you created earlier: ```mzsql CREATE SOURCE mz_source IN CLUSTER ingest_postgres FROM POSTGRES CONNECTION pg_connection (PUBLICATION 'mz_source') FOR ALL TABLES; ``` By default, the source will be created in the active cluster; to use a different cluster, use the `IN CLUSTER` clause. To ingest data from specific schemas or tables in your publication, use `FOR SCHEMAS (,)` or `FOR TABLES (, )` instead of `FOR ALL TABLES`. 1. After source creation, you can handle upstream [schema changes](/sql/create-source/postgres/#schema-changes) for specific replicated tables using the [`ALTER SOURCE...ADD SUBSOURCE`](/sql/alter-source/#context) and [`DROP SOURCE`](/sql/alter-source/#dropping-subsources) syntax. ### 3. Monitor the ingestion status {{% postgres-direct/check-the-ingestion-status %}} ### 4. Right-size the cluster {{% postgres-direct/right-size-the-cluster %}} ## D. Explore your data {{% postgres-direct/next-steps %}} ## Considerations {{% include-md file="shared-content/postgres-considerations.md" %}}