---
title: Column selection for PostgreSQL
description: Select or exclude PostgreSQL columns for ingestion in Lakeflow Connect, including which primary and unique key columns must stay selected in integrated CDC pipelines.
canonical_url: https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-column-selection
---

# Column selection for PostgreSQL

Use column selection to ingest only the table columns you need. This page covers the requirements that are specific to PostgreSQL.

## <a id="select-columns" />Select columns to ingest

By default, the pipeline ingests all columns in the tables you select. To ingest only some columns, set `include_columns` or `exclude_columns` in the table configuration. For a description of each property and example pipeline definitions, see [Select columns to ingest](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/column-selection.md).

How the pipeline handles your column selection depends on the pipeline type:

* **Ingestion gateway and ingestion pipeline**: The pipeline ingests the columns you include, or all columns except the ones you exclude. It doesn't check whether your selection keeps the table's key columns, so keep the columns described in [Required key columns](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-column-selection.md#key-columns) selected.
* **Integrated change data capture (CDC) pipeline**: The pipeline also checks that your selection keeps the table's key columns. If it doesn't, the table fails to ingest. See [Required key columns](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/postgresql-column-selection.md#key-columns).

## <a id="key-columns" />Required key columns

CDC keeps your destination table in sync by matching each change to a row using the source table's key. The connector picks the key in this order, and your column selection must keep all of that key's columns:

1. **Primary key**: If the table has a primary key, keep all primary key columns. Unique key columns are optional.

2. **Unique key**: If the table has no primary key, keep all columns of the first unique key whose columns are all `NOT NULL`. Unique constraints come before unique indexes created with `CREATE UNIQUE INDEX`, and within each group, unique keys are ordered alphabetically by name. For example, if unique constraints `uk_email` and `uk_phone` both have only `NOT NULL` columns, the connector uses `uk_email`.

   To see a table's unique keys, run `\d <table>` in `psql`. In the `Indexes` section of the output, unique constraints are marked `UNIQUE CONSTRAINT` and unique indexes are marked `UNIQUE`. To list the unique keys in the order the connector considers them, run the following query. The connector uses the first row where `all_not_null` is `true`.

   ```sql
   SELECT c.relname AS key_name,
          CASE WHEN con.oid IS NOT NULL THEN 'constraint' ELSE 'index' END AS kind,
          (SELECT string_agg(a.attname, ', ' ORDER BY k.ord)
             FROM unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
             JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum) AS columns,
          (SELECT bool_and(a.attnotnull)
             FROM pg_attribute a
             WHERE a.attrelid = i.indrelid AND a.attnum = ANY (i.indkey)) AS all_not_null
   FROM pg_index i
   JOIN pg_class c ON c.oid = i.indexrelid
   LEFT JOIN pg_constraint con ON con.conindid = i.indexrelid AND con.contype = 'u'
   WHERE i.indrelid = '<schema>.<table>'::regclass
     AND i.indisunique AND NOT i.indisprimary
     AND i.indexprs IS NULL AND i.indpred IS NULL AND i.indisvalid
   ORDER BY con.oid IS NULL, c.relname;
   ```

   Replace `<schema>` and `<table>` with the table's schema and name. If a name has uppercase letters or special characters, wrap it in double quotes inside the string, for example `'"MySchema"."MyTable"'::regclass`.

3. **No usable key**: If the table has neither a primary key nor a qualifying unique key, you can include or exclude any columns. These unique keys don't qualify: a unique key with a nullable column, a partial unique index, a unique index on an expression, and an invalid unique index, which `\d` marks `INVALID`.

To keep a column, list it in `include_columns`, or leave it out of `exclude_columns`.

In an integrated CDC pipeline, if your column selection drops a required key column, the table fails to ingest with the error `INGESTION_GATEWAY_KEY_COLUMN_NOT_SELECTED`. To recover, update `include_columns` or `exclude_columns` so that the key is fully selected, and then run a full refresh of the table. See [Fully refresh target tables](https://docs.databricks.com/aws/en/ingestion/lakeflow-connect/full-refresh.md).

## <a id="change-selection" />Change column selection for a table that is already ingesting

In an integrated CDC pipeline, you can edit `include_columns` or `exclude_columns` for a table at any time, and the change is saved to your pipeline. However, changing the column selection for a table that is already being ingested requires a full refresh of that table. If you change the selection and don't run a full refresh of the affected table in the same update, the pipeline update fails with the error `CANNOT_CHANGE_COLUMN_SELECTION_WITHOUT_FULL_REFRESH`.
