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 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.
Make sure you have a Neon account.
Make sure you have access to your Neon instance via psql
or the SQL editor in the Neon Console.
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.
{{< 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 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.
Select your project in the Neon Console.
On the Neon Dashboard, select Settings.
Select Logical Replication.
Click Enable to enable logical replication.
You can verify that logical replication is enabled by running:
SHOW wal_level;
The result should be:
wal_level
-----------
logical
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.
For each table that you want to replicate to Materialize, set the
replica identity
to FULL
:
ALTER TABLE <table1> REPLICA IDENTITY FULL;
ALTER TABLE <table2> 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.
For specific tables:
```postgres
CREATE PUBLICATION mz_source FOR TABLE <table1>, <table2>;
```
_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.
neon_superuser
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
to create a new role.
neon roles create --name materialize
{{< /tab >}}
{{< tab "Neon Console">}}
{{< /tab >}}
{{< tab "API">}}
Use the roles
endpoint
to create a new role.
curl 'https://console.neon.tech/api/v2/projects/<project_id>/branches/<branch_id>/roles' \
-H 'Accept: application/json' \
-H "Authorization: Bearer $NEON_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"role": {
"name": "materialize"
}
}' | jq
{{< /tab >}}
{{< /tabs >}}
Grant the user the required permissions on the schema(s) you want to replicate:
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.
{{< 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 to limit the IP addresses that can connect to your Neon instance. {{</ note >}}
If you use Neon's 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.
In the Materialize console's SQL Shell, 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:
SELECT * FROM mz_egress_ips;
In your Neon project, add the IPs to your IP Allow list:
The steps in this section are specific to Materialize. You can run them in the Materialize console's SQL Shell or your preferred SQL client connected to Materialize.
{{< 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.
{{< /note >}}
{{% postgres-direct/create-a-cluster %}}
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.
Run the CREATE SECRET
command to securely store the
password for the materialize
PostgreSQL user you created earlier:
CREATE SECRET pgpass AS '<PASSWORD>';
You can access the password for your Neon user from the Connection Details widget on the Neon Dashboard.
Use the CREATE CONNECTION
command to create a
connection object with access and authentication details for Materialize to
use:
CREATE CONNECTION pg_connection TO POSTGRES (
HOST '<host>',
PORT 5432,
USER '<user_name>',
PASSWORD SECRET pgpass,
SSL MODE 'require',
DATABASE '<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:
postgresql://materialize:AbC123dEf@ep-cool-darkness-123456.us-east-2.aws.neon.tech/dbname?sslmode=require
<host>
with your Neon hostname
(e.g., ep-cool-darkness-123456.us-east-2.aws.neon.tech
).<role_name>
with the dedicated replication user
(e.g., materialize
).<database>
with the name of the database containing the tables
you want to replicate to Materialize (e.g., dbname
).Use the CREATE SOURCE
command to connect Materialize
to your Neon database and start ingesting data from the publication
you created earlier:
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
(<schema1>,<schema2>)
or FOR TABLES (<table1>, <table2>)
instead of FOR
ALL TABLES
.
After source creation, you can handle upstream schema changes
for specific replicated tables using the ALTER SOURCE...ADD SUBSOURCE
and DROP SOURCE
syntax.
{{% postgres-direct/check-the-ingestion-status %}}
{{% postgres-direct/right-size-the-cluster %}}
{{% postgres-direct/next-steps %}}
{{% include-md file="shared-content/postgres-considerations.md" %}}