Skip to content
Database

Snowflake destination

Replicate Supabase Postgres changes to Snowflake.

The Snowflake destination is in private alpha and available only to approved organizations. Request access before following this guide.

Snowflake is a managed data platform. Supabase Pipelines replicates each Postgres table to Snowflake as an append-only history of changes.

To replicate data to Snowflake:

  1. Prepare a database, schema, role, service user, and key pair in Snowflake.
  2. Configure the Snowflake destination in the Dashboard.
  3. Query or materialize the replicated data in Snowflake.

Source table requirements#

The operations enabled in the Postgres publication determine the required REPLICA IDENTITY:

Published operationsRequired replica identity
INSERT onlyNo row identity is required.
DELETEA primary key, an identity index (USING INDEX), or full identity (FULL). Publish all identity columns.
UPDATEREPLICA IDENTITY FULL.

Set full replica identity before publishing updates:

alter table public.your_table replica identity full;

REPLICA IDENTITY FULL increases WAL volume but allows Pipelines to construct complete new rows when Postgres omits unchanged out-of-line TOAST values. It applies only to new WAL records. If the retained WAL already contains an incompatible update, change the setting and then restart replication for the affected table.

Prepare Snowflake resources#

Before you create a pipeline, prepare a dedicated Snowflake database and an empty schema for the replicated tables. Pipelines also needs a service user and role that can create and manage those tables. Use unquoted identifiers for the service user and role so Snowflake stores them in uppercase. Pipelines also converts the account and user names to uppercase during authentication.

Run this setup as a Snowflake administrator, changing the example names as needed:

create role if not exists PIPELINES_ROLE;
create user if not exists PIPELINES_USER
type = service;
grant role PIPELINES_ROLE to user PIPELINES_USER;
alter user PIPELINES_USER set default_role = PIPELINES_ROLE;
create database if not exists PIPELINES_DB;
create schema if not exists PIPELINES_DB.REPLICATED;
grant usage on database PIPELINES_DB to role PIPELINES_ROLE;
grant usage on schema PIPELINES_DB.REPLICATED to role PIPELINES_ROLE;
grant create table on schema PIPELINES_DB.REPLICATED to role PIPELINES_ROLE;

In this example, Pipelines creates the destination tables using PIPELINES_ROLE. Whatever name you choose, the role must retain ownership so Pipelines can alter, truncate, or drop the tables when required. Do not create the tables in advance under another role.

Snowflake automatically creates a managed default pipe named <TABLE>-STREAMING for each table. You don't need to provide a virtual warehouse, stage, or pipe for ingestion. See Snowpipe Streaming access privileges for the required permissions.

To keep ingestion resources separate from downstream workloads, use a different role and warehouse for queries and transformations.

Keep the SQL and streaming roles aligned#

Pipelines connects to Snowflake through separate interfaces for SQL requests and streaming. Both interfaces need to use the same role:

  • SQL requests use the optional Role under Advanced settings in the Dashboard. When Role is empty, they use the user's default role.
  • Snowpipe Streaming uses the user's DEFAULT_ROLE. It does not use the optional Role setting.

Grant the permissions above to a dedicated role such as PIPELINES_ROLE, then set it as the service user's DEFAULT_ROLE. In the Dashboard, either leave Role empty or enter the same role explicitly. This keeps SQL validation and streaming aligned.

Generate a key pair#

Pipelines uses an RSA key pair to authenticate with Snowflake. The key must be at least 2048 bits, and Snowflake recommends the PKCS #8 format. Run one of the following commands from the directory where you want to store the key. The command creates a private key named rsa_key.p8 in that directory.

For an unencrypted private key:

openssl genrsa 2048 | openssl pkcs8 -topk8 \
-inform PEM -out rsa_key.p8 -nocrypt

Or, for a passphrase-protected private key:

openssl genrsa 2048 | openssl pkcs8 -topk8 -v2 des3 \
-inform PEM -out rsa_key.p8

Create a public key from the private key. Snowflake uses the public key to verify connections signed with rsa_key.p8:

openssl rsa -in rsa_key.p8 -pubout -out rsa_key.pub

Open rsa_key.pub and copy the text between the BEGIN PUBLIC KEY and END PUBLIC KEY lines. In the following statement, replace the <public-key-body> placeholder with the copied text:

alter user PIPELINES_USER set rsa_public_key = '<public-key-body>';

When you configure the destination in the Dashboard, paste or upload the complete rsa_key.p8 file into Private key. Preserve its original PEM header and footer. If the key is passphrase-protected, enter the passphrase into Private key passphrase.

Keep rsa_key.p8 and its passphrase secret. The Dashboard accepts unencrypted PKCS #1 and PKCS #8 keys. It also accepts encrypted PKCS #8 keys with a passphrase, but not encrypted PKCS #1 keys.

See Snowflake key-pair authentication to verify the public-key fingerprint and rotate keys with RSA_PUBLIC_KEY_2.

Find the account identifier#

Run this query in Snowflake:

select current_organization_name() || '-' || current_account_name();

Use the result as the Account ID, for example MYORG-MYACCOUNT. The field also accepts legacy one-part account locators, but not full URLs or dotted locator-and-region hostnames. Account IDs can contain up to 63 characters. See Snowflake account identifiers for details.

Configure Snowflake as a destination#

Follow the steps in Set up Pipelines. When prompted to choose a destination, select Snowflake and enter the following settings:

FieldValue
Account IDMYORG-MYACCOUNT, for example; use an organization-account identifier
UserPIPELINES_USER, or your unquoted service user
DatabasePIPELINES_DB, or your destination database
SchemaREPLICATED, or your dedicated destination schema
RoleLeave empty to use the service user's default role, or enter that same role explicitly
Private keyPaste or upload the complete rsa_key.p8 private-key PEM file
Private key passphraseOnly for an encrypted PKCS #8 key

Enter the database and schema identifiers exactly as stored in Snowflake. Unquoted identifiers are stored in uppercase. Choose an account near the managed pipeline region.

Click Start pipeline, then complete the validation and cost confirmations.

How it works#

Pipelines uses Snowflake's SQL REST API to validate the database and schema. It also uses the API to create, update, and recreate destination tables and to apply source TRUNCATE operations. Pipelines sends initial and ongoing row data through Snowpipe Streaming.

Validation checks authentication, database and schema visibility, and that QUOTED_IDENTIFIERS_IGNORE_CASE is FALSE. It does not verify that the role can create or own tables or write through Snowpipe Streaming.

Destination table names#

Pipelines combines each Postgres schema and table name into one Snowflake table name. Existing underscores are doubled, the two names are joined with a single underscore, and the result is converted to uppercase:

Postgres tableSnowflake table
public.ordersPUBLIC_ORDERS
sales_eu.order_itemsSALES__EU_ORDER__ITEMS

Postgres schema and table names cannot start or end with _ or contain " or ;. Names that differ only in case map to the same Snowflake name. Use lowercase Postgres names to avoid collisions. Source column names are preserved as quoted identifiers, except for the reserved metadata names.

Append-only change history#

Each destination table contains the replicated source columns plus two VARCHAR NOT NULL metadata columns:

ColumnMeaning
_cdc_operationLowercase operation: insert, update, or delete.
_cdc_sequence_numberFixed-width hexadecimal commit LSN and transaction ordinal, such as 00000000016b3740/0000000000000002.

The metadata names are reserved and can't be used by source columns. Initial-sync rows use insert and the shared sequence number 0000000000000000/0000000000000000.

Snowflake tables contain an event history rather than a current-state replica:

  • An insert appends the new row.
  • An update appends the complete new row. It does not append a before image.
  • A delete appends the complete old row for REPLICA IDENTITY FULL. For a primary-key or USING INDEX identity, it sends only the identity columns. Other columns can contain destination defaults or NULL; do not treat them as the deleted row's original values.
  • Truncating the source table also truncates the Snowflake table and resets its streaming state. It does not append a truncate event.

The sequence number orders changes but is not a globally unique event ID. Snowpipe committed offsets suppress routine replay, but consumers must still tolerate duplicate processing.

Restarting a table drops the Snowflake table and its managed streaming state, which erases the replicated history. A restart cannot recover past events. By contrast, removing a table from the publication leaves its destination history in place.

Query replicated data #

Use the replicated change history to build current-state datasets for reporting and analytics. Pipelines maintains the history table, while you maintain the queries, views, or dynamic tables that read from it.

ApproachWhen to use itTradeoff
Query or viewRead current state from the changes already in Snowflake.Computes the result when queried, so query cost can grow with the history.
Dynamic tableStore current state for repeated analytics queries.Uses compute and storage to maintain the result, with a configurable freshness target.
Streams and tasksControl how and when a separate table is updated.Requires your own merge, initialization, and recovery logic.

Before you start#

The examples use a source table named public.orders with the columns id and status. Pipelines replicates it to PIPELINES_DB.REPLICATED.PUBLIC_ORDERS. Replace these names with your own, and wait for the initial sync to finish before treating the results as a complete replica.

Choose a unique, non-null identity that does not change when a row is updated. The examples use id. If you use a composite key, include every key column in partition by, such as partition by "tenant_id", "id". The publication and delete events must include the same columns. REPLICA IDENTITY FULL alone does not make rows unique.

Create derived objects in a schema outside the Pipelines-managed REPLICATED schema, using a separate analytics role and warehouse. The examples use ANALYTICS_ROLE, ANALYTICS_WH, and PIPELINES_DB.ANALYTICS. Ask your Snowflake administrator to prepare these resources and grant the analytics role:

  • USAGE on the warehouse, database, and both schemas.
  • SELECT on the replicated table.
  • CREATE VIEW on the analytics schema to create a view, or CREATE DYNAMIC TABLE to create a dynamic table.

The role must be available to the Snowflake user running the examples. Keep ownership of the replicated table with PIPELINES_ROLE. See Snowflake's dynamic table access control for the full privilege requirements.

Query current state#

Run these statements in a Snowflake SQL worksheet with your analytics role:

use role ANALYTICS_ROLE;
use warehouse ANALYTICS_WH;
select "id", "status"
from PIPELINES_DB.REPLICATED.PUBLIC_ORDERS
qualify row_number() over (
partition by "id" order by "_cdc_sequence_number" desc
) = 1
and "_cdc_operation" != 'delete';

The query returns one row for each identity whose latest operation is not delete. The fixed-width sequence string determines which change is the latest, and repeated copies of the same event collapse into one result row. Keep the double quotes around source and metadata column names because Pipelines creates them as case-sensitive identifiers.

Keep the delete condition inside qualify. Moving it to where "_cdc_operation" != 'delete' would remove delete events before the rows are ranked, which could bring back an older version of a deleted row. Snowflake's QUALIFY reference explains this evaluation order.

To reuse the query from an analytics tool, save it as a view:

create view PIPELINES_DB.ANALYTICS.ORDERS_CURRENT_VIEW as
select "id", "status"
from PIPELINES_DB.REPLICATED.PUBLIC_ORDERS
qualify row_number() over (
partition by "id" order by "_cdc_sequence_number" desc
) = 1
and "_cdc_operation" != 'delete';

A regular view stores the query definition rather than a separate copy of the results. Each read derives the current state from the history available at query time. See Snowflake's comparison of views and dynamic tables.

Materialize with a dynamic table#

A dynamic table stores the query results and refreshes them as the replicated history changes. Use one when you need a maintained current-state dataset without maintaining a scheduled merge task.

  1. Ask the owner of the replicated table to enable change tracking in Snowflake. This is a table setting, not a change to the replicated columns or data. Run as PIPELINES_ROLE, or another role that inherits ownership:

    alter table PIPELINES_DB.REPLICATED.PUBLIC_ORDERS
    set change_tracking = true;

    The analytics role does not own the replicated table, so it cannot enable change tracking automatically when creating the dynamic table. See Snowflake's change tracking requirements.

  2. Switch to the analytics role and create the dynamic table:

    use role ANALYTICS_ROLE;
    use warehouse ANALYTICS_WH;
    create dynamic table PIPELINES_DB.ANALYTICS.ORDERS_CURRENT
    target_lag = '5 minutes'
    warehouse = ANALYTICS_WH
    refresh_mode = incremental
    initialize = on_create
    as
    select "id", "status"
    from PIPELINES_DB.REPLICATED.PUBLIC_ORDERS
    qualify row_number() over (
    partition by "id" order by "_cdc_sequence_number" desc
    ) = 1
    and "_cdc_operation" != 'delete';

    initialize = on_create populates the dynamic table before creation finishes. Explicit refresh_mode = incremental makes creation fail if your adapted query cannot refresh incrementally, instead of choosing a full refresh through AUTO. See Snowflake's refresh modes and CREATE DYNAMIC TABLE reference.

  3. Check the refresh mode and read the materialized rows:

    show dynamic tables like 'ORDERS_CURRENT'
    in schema PIPELINES_DB.ANALYTICS;
    select "id", "status"
    from PIPELINES_DB.ANALYTICS.ORDERS_CURRENT;

    Confirm that refresh_mode is INCREMENTAL and scheduling is running. Use Snowflake's refresh monitoring to check the last successful refresh and any errors. After an insert, update, or delete reaches the replicated table, the next successful refresh reflects it in ORDERS_CURRENT.

The five-minute target_lag is an example freshness target relative to the history already in Snowflake. It is neither a fixed refresh schedule nor an end-to-end latency guarantee from Postgres. Both pipeline replication lag and dynamic-table refresh lag affect freshness. See Snowflake's target lag guide.

Refreshing a dynamic table consumes warehouse compute, while its materialized results consume storage. These costs are additional to ingestion and querying. Start with a freshness target that meets your reporting needs, then test its cost and refresh behavior with a representative workload. A dedicated warehouse can help isolate refresh costs. See Snowflake's dynamic table cost guide.

Use streams and tasks#

Snowflake streams and tasks can maintain a separate table through scheduled MERGE statements. Use them when you need more control over the update procedure or schedule. Snowflake's SCD Type 1 examples compare this approach with dynamic tables.

Adapt the merge to the "_cdc_operation" and "_cdc_sequence_number" columns created by Pipelines. A stream on the history table sees every appended row, including rows that represent source updates and deletes. Your job must interpret those operations, load the existing history, tolerate replay, and rebuild the current state after a source truncate or pipeline table reset.

Maintain derived objects#

Pipelines maintains the replicated history table, but does not update your view or dynamic-table definitions.

ChangeWhat to do
Source TRUNCATEA direct query or view reads the truncated history. Check that the dynamic table completes a refresh before relying on its contents.
Pipeline table resetWait for the new initial sync. Reapply table-specific read grants and change tracking to the recreated history table. Check dependent objects and recreate the dynamic table if it cannot refresh.
Added, renamed, or dropped source columnReview the explicit column list. Add new columns to your definition when needed. Update or recreate derived objects that reference renamed or dropped columns.

Recreating a dynamic table initializes its contents again and uses compute. See Snowflake's dynamic table modification guide for changes that require reinitialization.

Type mapping#

Pipelines creates Snowflake columns with these mappings:

Postgres typeSnowflake type
booleanBOOLEAN
smallint, integer, bigintSMALLINT, INTEGER, BIGINT
real, double precisionFLOAT, DOUBLE
date, timeDATE, TIME
timestamp, timestamp with time zoneTIMESTAMP_NTZ, TIMESTAMP_TZ
json, jsonbVARIANT
One-dimensional arraysARRAY
oidBIGINT
Other typesVARCHAR

Pipelines maps character and text types, numeric, time with time zone, interval, uuid, bytea, bit strings, and custom or unknown types to VARCHAR. It serializes these values instead of storing them as native Snowflake types. For bytea, the serialized value is a lowercase hexadecimal string.

Additional limits apply:

  • Multi-dimensional arrays aren't supported. Non-default lower bounds on one-dimensional arrays aren't preserved.
  • Non-finite real and double precision values are rejected. Non-finite numeric values are preserved as strings in VARCHAR columns.
  • An uncompressed serialized row larger than 2 MiB is rejected.
  • Source primary-key, unique, check, length, precision, and nullability constraints aren't copied. Only the two CDC metadata columns are NOT NULL.

Schema change support#

Pipelines supports:

  • Adding, renaming, or dropping columns
  • Adding or removing published columns on tracked tables

Replicated columns remain nullable in Snowflake, and changes to existing column defaults are not propagated. When a table is first created, Pipelines can copy compatible literal defaults. It can also copy string, numeric, or boolean literal defaults for columns added later in Postgres. Other defaults are omitted, although Postgres still supplies the source values through replication.

Schema changes also affect the stored history. Renaming a column changes its name in earlier events, dropping a column removes its historical values, and adding a column with a default can populate older rows.

Previously excluded columns are added without defaults, leaving historical events NULL for those columns. Removing a published column drops its destination values; adding it again does not restore them.

For type changes, unsupported changes, and interrupted schema changes, see the shared schema-change behavior and recovery. Apart from enabling change tracking, do not alter managed destination objects manually.

Troubleshooting#

SymptomWhat to check
Authentication failsAccount identifier, user, key fingerprint and PEM, and passphrase. Omit the passphrase for an unencrypted key.
Database or schema isn't foundExact identifier case and USAGE permissions on both objects
Validation passes but table creation failsGrants and ownership, including CREATE TABLE
Tables are created but writes failSQL and streaming role alignment, table permissions, and network access to the account control and discovered Snowpipe ingest endpoints
Updates or deletes failSource replica identity and published columns
A row is rejectedType and row-size limits and reserved metadata columns
A schema change failsSupported changes; do not repair managed tables manually

Use pipeline monitoring to inspect errors. For unresolved failures, contact support with the pipeline ID and error details.

Additional resources#