---
title: Configure PostgreSQL for ingestion into Databricks
description: Preview the source setup tasks for ingestion from PostgreSQL into Databricks using Lakeflow Connect.
canonical_url: https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-source-setup
---

# Configure PostgreSQL for ingestion into Databricks

> [!NOTE]
> **Preview**
>
> The PostgreSQL connector for Lakeflow Connect is in Public Preview. Reach out to your Databricks account team to enroll in the Public Preview.

This page describes the source setup tasks for ingestion from PostgreSQL into Databricks using Lakeflow Connect.

## <a id="credentials-used-during-setup-and-ingestion" />Credentials used during setup and ingestion

PostgreSQL ingestion uses two different sets of credentials at two different stages. Knowing which credentials to use where prevents authentication and permission errors during setup.

| Stage                             | Credentials to use                                                                                                                                                  | Why                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Source setup (this page)          | A PostgreSQL admin, superuser, or table owner, connected directly to the source database (for example, through `psql` or your cloud provider's management console). | Creating the replication user, granting privileges, and creating publications require superuser or table-owner privileges that the replication user does not have. The replication slot is created by the replication user itself, so an admin connected to the database switches to that role to create it. For the full privilege list, see [PostgreSQL database user requirements](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-privileges.md). |
| Connection and ingestion pipeline | The dedicated replication user (for example, `databricks_replication`) that you create during source setup.                                                         | The ingestion gateway authenticates to PostgreSQL as the replication user to read changes. You enter these credentials when you create the Unity Catalog connection. See [Create a PostgreSQL connection](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-connection.md).                                                                                                                                                                             |

> [!NOTE]
>
> You perform the source setup tasks as an admin, but the ingestion pipeline does not use admin credentials. Only the replication user's credentials are stored in the Unity Catalog connection.

## <a id="logical-replication" />Logical replication for change data capture

The PostgreSQL connector uses logical replication to track changes in the source tables. Logical replication allows the connector to capture data modifications (inserts, updates, and deletes) without requiring triggers or significant overhead on the source database.

Lakeflow PostgreSQL Logical replication requires the following:

1. Lakeflow Connect supports data replication from PostgreSQL version 13 and later.

2. Configure the database for logical replication:

   For AWS RDS and Aurora, set the `rds.logical_replication` parameter to `1`.

3. Create publications that include all tables you want to replicate.

4. Create replication slots for each catalog that will be replicated.

> [!NOTE]
>
> Publications must be created before creating replication slots.

For more information about logical replication, see the [Logical Replication](https://www.postgresql.org/docs/current/logical-replication.html) documentation on the PostgreSQL website.

## Overview of source setup tasks

Complete the following tasks in PostgreSQL before ingesting data into Databricks:

1. Verify PostgreSQL 13 or above

2. Configure network access (security groups, firewall rules, or VPN)

3. Configure logical replication:

   * For AWS RDS/Aurora, set `rds.logical_replication = 1`.

   - Create replication user with required privileges. See [PostgreSQL database user requirements](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-privileges.md)
   - Set replica identity for tables. See [Set replica identity for tables](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-source-setup.md#set-replica-identity-for-tables)
   - Creating publications and replication slots

4. Optional: Configure inline DDL tracking for automatic schema change detection. In case you want to opt for the inline DDL tracking, reach out to Databricks support.

> [!IMPORTANT]
>
> If you plan to replicate from multiple PostgreSQL databases, you must create a separate publication and replication slot for each database. The inline DDL tracking script (if used) must also be executed in each database.

## Configure logical replication

To enable logical replication in PostgreSQL, configure the database settings and set up the necessary objects.

#### Set the WAL level to logical

The Write-Ahead Log (WAL) must be configured for logical replication. This setting typically requires a database restart.

1. Check the current `wal_level` setting:

   ```sql
   SHOW wal_level;
   ```

2. If the value is not `logical`, set `wal_level = logical` in the server configuration and restart the PostgreSQL service.

### Create a replication user

Create a dedicated PostgreSQL user for Databricks ingestion with replication privileges:

```sql
CREATE USER databricks_replication WITH PASSWORD 'your_secure_password';
GRANT CONNECT ON DATABASE your_database TO databricks_replication;
GRANT USAGE ON SCHEMA schema_name TO databricks_replication;
GRANT SELECT ON TABLE schema_name.table_name TO databricks_replication;
ALTER USER databricks_replication WITH REPLICATION;
```

For detailed privilege requirements, see [PostgreSQL database user requirements](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-privileges.md).

### Set replica identity for tables

For each table you want to replicate, configure the replica identity. The correct setting depends on the table structure:

| Table structure                                                                                                              | Required REPLICA IDENTITY                                                                                                                                                                                      | Command                                                        |
| ---------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------- |
| Table has primary key, and does not contain TOASTable columns (for example, `TEXT`, `BYTEA`, `VARCHAR(n)` with large values) | `DEFAULT`                                                                                                                                                                                                      | `ALTER TABLE schema_name.table_name REPLICA IDENTITY DEFAULT;` |
| Table has primary key, but includes large variable-length (TOASTable) columns                                                | `FULL`, or `DEFAULT` if you enable [partial row update mode](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-integrated-pipeline.md#partial-row-update) on an integrated CDC pipeline | `ALTER TABLE schema_name.table_name REPLICA IDENTITY FULL;`    |
| Table does not have a primary key                                                                                            | `FULL`                                                                                                                                                                                                         | `ALTER TABLE schema_name.table_name REPLICA IDENTITY FULL;`    |

For more information about replica identity settings, see [Replica Identity](https://www.postgresql.org/docs/current/logical-replication-publication.html#LOGICAL-REPLICATION-PUBLICATION-REPLICA-IDENTITY) in the PostgreSQL documentation.

### <a id="create-a-publication" />Create a publication

Create a publication in each database that includes the tables you want to replicate. Run this command as the table owner or a superuser:

```sql
-- Create a publication for specific tables
CREATE PUBLICATION databricks_publication FOR TABLE schema_name.table1, schema_name.table2;

-- Or create a publication for all tables in a database
CREATE PUBLICATION databricks_publication FOR ALL TABLES;
```

> [!NOTE]
>
> * You must create a separate publication in each PostgreSQL database that you want to replicate.
> * `CREATE PUBLICATION ... FOR TABLE` requires ownership of the listed tables. `FOR ALL TABLES` requires superuser privileges. Run this command as the table owner or a database superuser, not as the replication user.
> * Avoid adding tables to the publication that are not needed for replication to reduce unnecessary network traffic.

### Configure replication slot parameters

Before creating replication slots, configure the following server parameters:

#### Limit WAL retention for replication slots

**Parameter**: `max_slot_wal_keep_size`

It is **recommended not to set** `max_slot_wal_keep_size` to `-1` (the default value), as this allows unbounded WAL bloat due to retention by lagging or inactive replication slots. Depending on your workload, set this parameter to a finite value.

Learn more about [max\_slot\_wal\_keep\_size parameter](https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-MAX-SLOT-WAL-KEEP-SIZE) in the official PostgreSQL documentation.

> [!NOTE]
>
> Some managed cloud providers do not allow modification of this parameter and instead rely on built-in slot monitoring and auto-cleanup. Review the platform behavior before setting operational alerts.
>
> For more information, see:
>
> * [Taming PostgreSQL WAL File Growth - pgDash](https://pgdash.io/blog/taming-postgresql-wal-file-growth.html)
> * [PostgreSQL 13: Don't let slots kill your primary | EDB](https://www.enterprisedb.com/blog/postgresql-13-dont-let-slots-kill-your-primary)

#### Configure replication slot capacity

**Parameter**: `max_replication_slots`

Each PostgreSQL database being replicated requires one logical replication slot. Set this parameter to at least the number of databases being replicated, plus any existing replication needs.

#### Configure WAL senders

**Parameter**: `max_wal_senders`

This parameter defines the maximum number of concurrent WAL sender processes that stream WAL data to subscribers. In most cases, you should have one WAL sender process for each replication slot to ensure efficient and consistent data replication.

Configure `max_wal_senders` to be at least equal to the number of replication slots in use, accounting for any other existing usage. It is recommended to set it slightly higher to provide operational flexibility.

### <a id="create-a-replication-slot" />Create a replication slot

Create a replication slot in each database. The ingestion gateway or the integrated CDC pipeline uses the slot to track changes. The replication slot must be created by a user with the `REPLICATION` privilege. If you are connected as a superuser or admin, switch to the replication user first:

```sql
SET ROLE databricks_replication;

-- Databricks supports only the pgoutput plugin for replication slots
SELECT pg_create_logical_replication_slot('databricks_slot', 'pgoutput');

-- Switch back to the admin or table owner role for subsequent steps
RESET ROLE;
```

> [!IMPORTANT]
>
> * Replication slots hold WAL data until consumed by the connector. Configure the `max_slot_wal_keep_size` parameter to limit WAL retention and prevent unbounded WAL growth. See [Configure replication slot parameters](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-source-setup.md#configure-replication-slot-parameters) for details.
> * When you delete an ingestion pipeline, you must manually drop the associated replication slot. See [Clean up replication slots](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-maintenance.md#cleanup-replication-slots).

## <a id="optional-configure-inline-ddl-tracking" />Optional: Configure inline DDL tracking

Inline DDL tracking is an optional feature that allows the connector to automatically detect and apply schema changes from the source database. This feature is disabled by default.

> [!WARNING]
>
> Inline DDL tracking is currently on hold for stabilization. Wait for the documentation update before reaching out to Databricks Support to enable this feature for your workspace.

For information about which schema changes are handled automatically and which require a full refresh, see [How do managed connectors handle schema evolution?](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/faq.md#how-do-managed-connectors-handle-schema-evolution) and [Schema evolution](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-limits.md#schema-evolution).

### Set up inline DDL tracking

You can complete the source-side setup below in advance so that inline DDL tracking is ready to use as soon as the feature is enabled for your workspace. Creating the audit objects has no effect on the connector until the feature is enabled, so it is safe to run these steps ahead of time.

Complete these steps **in each PostgreSQL database**:

1. Download the latest version of the script:

   [Download lakeflow\_pg\_ddl\_change\_tracking.sql](https://docs.databricks.com/aws/en/documents/lakeflow_pg_ddl_change_tracking.sql)

2. Run the script:

   ```sql
   \i lakeflow_pg_ddl_change_tracking.sql
   ```

   The script creates the following objects in the `public` schema. Object names include a version suffix (currently `_1_0`) that tracks the script version:

   * **Audit table**: `public.lakeflow_ddl_audit_table_1_0` — stores captured DDL events.
   * **Event trigger functions**: `public.lakeflow_ddl_audit_function_1_0` (for `ALTER TABLE` events) and `public.lakeflow_drop_ddl_audit_function_1_0` (for `DROP TABLE` events).
   * **Event triggers**: `lakeflow_ddl_audit_trigger_1_0` (fires on `ddl_command_end`) and `lakeflow_drop_ddl_audit_trigger_1_0` (fires on `sql_drop`).

3. Verify that the triggers and audit table were created successfully:

   ```sql
   -- Check for the DDL audit table
   SELECT * FROM pg_tables WHERE tablename LIKE 'lakeflow_ddl_audit_table%';

   -- Check for the event triggers
   SELECT * FROM pg_event_trigger WHERE evtname LIKE 'lakeflow%';
   ```

   You should see the audit table `lakeflow_ddl_audit_table_1_0` and two event triggers (`lakeflow_ddl_audit_trigger_1_0` and `lakeflow_drop_ddl_audit_trigger_1_0`).

4. Add the DDL audit table to your publication. This command must be run as the publication owner, not as the replication user:

   ```sql
   ALTER PUBLICATION databricks_publication ADD TABLE public.lakeflow_ddl_audit_table_1_0;
   ```

> [!IMPORTANT]
>
> Create the DDL support objects before you request feature enablement. The connector picks up the audit objects only when the gateway starts. After the feature is enabled for your workspace, you must restart the ingestion gateway for inline DDL tracking to take effect. See [Restart the ingestion gateway](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-maintenance.md#restart-the-ingestion-gateway).

## Cloud-specific configuration notes

### AWS RDS and Aurora

* Ensure that the `rds.logical_replication` parameter is set to `1` in the parameter group.
* Configure security groups to allow connections from the Databricks workspace.
* The replication user requires the `rds_replication` role:

  ```sql
  GRANT rds_replication TO databricks_replication;
  ```

### Azure Database for PostgreSQL

* Enable logical replication in the server parameters through the Azure portal or CLI.
* Configure firewall rules to allow connections from the Databricks workspace.
* For Flexible Server, logical replication is supported. For Single Server, ensure you're using a supported tier.

### GCP Cloud SQL for PostgreSQL

* Enable the `cloudsql.logical_decoding` flag in the instance settings.
* Configure authorized networks to allow connections from the Databricks workspace.
* Ensure that the `cloudsql.enable_pglogical` flag is set to `on` if using pglogical extensions.

## Verify the configuration

After completing the setup tasks, verify that logical replication is properly configured:

1. Check that the `wal_level` is set to `logical`:

   ```sql
   SHOW wal_level;
   ```

2. Verify that the replication user has the `replication` privilege:

   ```sql
   SELECT rolname, rolreplication FROM pg_roles WHERE rolname = 'databricks_replication';
   ```

3. Verify that the replication user has SELECT privileges on your tables. Replace `schema_name.table_name` with the schema and table you are replicating (for example, `public.my_table`):

   ```sql
   SELECT has_table_privilege('databricks_replication', 'schema_name.table_name', 'SELECT');
   ```

4. Confirm that the publication exists:

   ```sql
   SELECT * FROM pg_publication WHERE pubname = 'databricks_publication';
   ```

5. Verify that the replication slot exists:

   ```sql
   SELECT slot_name, slot_type, active, restart_lsn
   FROM pg_replication_slots
   WHERE slot_name = 'databricks_slot';
   ```

6. Check the replica identity for your tables:

   ```sql
   SELECT schemaname, tablename, relreplident
   FROM pg_tables t
   JOIN pg_class c ON t.tablename = c.relname
   WHERE schemaname = 'your_schema';
   ```

   The `relreplident` column should show `d` for DEFAULT replica identity (uses primary key) or `f` for FULL replica identity (required for tables without primary keys or with TOASTable columns).

## Next steps

After completing the source setup, you can create an ingestion gateway and pipeline to ingest data from PostgreSQL. See [Ingest data from PostgreSQL](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-pipeline.md).
