๐Ÿ”ฌ Sync PostgreSQL data to a Pinecone vector knowledge base using Gemini embeddings

โšก 21 views ยท ๐Ÿ”ฌ Document Extraction & Analysis

Description

PostgreSQL Auto-Discovery to Pinecone Vector Knowledge Base

Turn existing PostgreSQL databases into AI-searchable Pinecone vector knowledge bases without manually defining every table and column.

This template discovers your PostgreSQL schema, selects text-rich tables, builds readable embedding documents from rows, generates Google Gemini embeddings, upserts vectors into Pinecone, and tracks per-table sync state for incremental indexing.

It also includes a companion semantic search webhook workflow that embeds a user query, queries Pinecone, and returns source row metadata for RAG and AI assistant use cases.

Features

Architecture

The main workflow runs through these stages:

  1. Manual or scheduled trigger.
  2. Configuration.
  3. PostgreSQL metadata discovery.
  4. Table analysis and filtering.
  5. Dynamic row fetching for selected tables.
  6. Row-to-document transformation.
  7. Gemini embedding batch preparation.
  8. Gemini embedding generation.
  9. Pinecone vector batch preparation.
  10. Pinecone upsert.
  11. Sync state update and summary output.

How Auto-Discovery Works

The workflow inspects PostgreSQL metadata, groups columns by table, and chooses indexable tables based on:

Security Filtering

The workflow excludes sensitive columns before documents are built. By default, it filters column names containing:

You can add more column names in the excludedColumns setting.

Important: review your database schema and compliance requirements before enabling indexing in production. This workflow provides safe defaults, but it cannot understand every business-specific sensitive field.

Supported PostgreSQL Column Types

The workflow treats these as text-rich embedding candidates:

When includeScalarContextColumns is enabled, the document also includes useful scalar context from:

PostgreSQL Setup

Create or select a PostgreSQL credential in n8n with read-only access.

Recommended permissions:

GRANT USAGE ON SCHEMA public TO your_n8n_user;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO your_n8n_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO your_n8n_user;

For production, prefer a dedicated read-only user and restrict schemas with the workflow allowedSchemas setting.

Pinecone Setup

  1. Create a Pinecone index.
  2. Use the same vector dimension as the embedding model.
  3. This workflow requests 1536 dimensions from Gemini so it can use a Pinecone index configured for 1536-dimensional vectors.
  4. Copy the index host, for example your-index-abc123.svc.us-east-1-aws.pinecone.io.
  5. Create an n8n HTTP Header Auth credential:
    • Header name: Api-Key
    • Header value: your Pinecone API key.
  6. Set pineconeIndexHost in the workflow configuration.

The workflow uses Pinecone namespaces in this format:

postgres-{schema}.{table}

Example:

postgres-public.tickets

Gemini Setup

Create an n8n HTTP Header Auth credential:

The default embedding model is:

gemini-embedding-001

This workflow requests geminiOutputDimensionality = 1536. Pinecone index dimensions must match the vectors you upsert.

Why HTTP Request Nodes Are Used

n8n has native AI/vector nodes, including embedding and Pinecone vector store nodes. This template uses HTTP Request nodes for Gemini and Pinecone because the workflow needs precise control over production indexing behavior:

The native nodes are excellent for standard document ingestion and retrieval flows. For this auto-discovery template, HTTP requests make the workflow more predictable and easier to customize for database indexing.

Main Workflow Configuration

Open the Workflow Configuration node and set:

FieldDescription
allowedSchemasComma-separated schemas to include. Default: public. Leave empty to allow all non-system schemas.
allowedTablesOptional comma-separated table names or schema.table names to include.
excludedTablesTables to skip. Supports table names or schema.table.
excludedColumnsAdditional columns to exclude.
maxRowsPerTableMaximum rows fetched per table per run. Default: 10 for safe first runs.
maxDocumentsPerRunHard cap on documents sent to Gemini in one execution. Default: 10 so first tests finish quickly.
fullSyncTablesWithoutTimestampIf true, tables without timestamp columns receive limited full syncs.
skipTablesWithoutPrimaryKeyIf true, tables without primary keys are skipped.
includeScalarContextColumnsAdds scalar values such as status, price, priority, dates, and UUIDs to documents.
maxDocumentCharactersTruncates very large documents before embedding. Default: 2000.
geminiEmbeddingModelGemini embedding model. Default: gemini-embedding-001.
geminiOutputDimensionalityOutput vector size requested from Gemini. Default: 1536.
geminiBatchSizeNumber of documents sent to Gemini per embeddings request. Default: 1 for safe first runs.
geminiSmokeTestModeSends only one document to Gemini. Default: true. Disable after one-document embedding works.
pineconeIndexHostYour Pinecone index host.
pineconeNamespacePrefixNamespace prefix. Default: postgres-.
pineconeBatchSizeNumber of vectors per Pinecone upsert request.

Document Format

Each PostgreSQL row becomes a readable document for embedding:

Database: postgres
Schema: public
Table: tickets
Id: 123
Updated At: 2026-05-17T12:00:00.000Z
Title: Payment failed for user
Description: Customer payment failed after webhook timeout
Status: open
Priority: high

The goal is to preserve business meaning instead of embedding raw JSON alone.

Pinecone Vector Structure

Each vector is upserted like this:

{
  "id": "postgres::public::tickets::123",
  "values": [0.0123, -0.0456],
  "metadata": {
    "source": "postgres",
    "schema": "public",
    "table": "tickets",
    "primary_key": "id",
    "primary_key_value": "123",
    "updated_at": "2026-05-17T12:00:00.000Z",
    "text_columns": "title,description",
    "document_columns": "id,updated_at,title,description,status,priority"
  }
}

Vector ID format:

postgres::{schema}::{table}::{primary_key_value}

Workflow Setup Steps

  1. Import workflow.json into n8n.
  2. Add your PostgreSQL credential to both PostgreSQL nodes.
  3. Add your Gemini HTTP Header Auth credential to Generate Gemini Embeddings.
  4. Add your Pinecone HTTP Header Auth credential to Upsert Vectors to Pinecone.
  5. Open Workflow Configuration.
  6. Set allowedSchemas, pineconeIndexHost, and any table or column filters.
  7. Run manually with a small maxRowsPerTable for the first test.
  8. Inspect the Return Sync Summary output.
  9. Confirm vectors appear in Pinecone.
  10. Enable the optional Cron trigger when ready.

Then import semantic-search-workflow.json and configure its Gemini and Pinecone credentials.

Troubleshooting

No tables are indexed

Check:

Gemini embedding request fails

Check:

If you receive status 429, Gemini is rate limiting the workflow or the Google AI Studio project does not have enough available quota. n8n may show a generic batching/rate-limit message for both cases.

First run with smoke-test settings:

The Generate Gemini Embeddings HTTP Request node should also have Options > Batching enabled with:

This spaces out the HTTP requests after the documents have already been grouped into embedding payloads.

If the workflow still returns 429 with smoke-test mode enabled, check your Google AI Studio project:

Gemini embeddings are documented here: https://ai.google.dev/api/embeddings.

Gemini embedding node keeps loading

This usually means the workflow is processing more documents than expected while also spacing requests out to avoid rate limits. For example, 100 documents with a 10 second batch interval can take more than 16 minutes before retries.

For a quick smoke test, use:

After the smoke test succeeds, increase the limits gradually.

Pinecone upsert fails

Check:

Incremental sync does not pick up rows

Check:

Too many rows are embedded

Lower maxRowsPerTable, restrict allowedTables, or keep fullSyncTablesWithoutTimestamp disabled.

Performance Considerations

Cost Considerations

Costs come primarily from:

To control costs:

Customization Guide

Common customizations:

Use Cases

Assumptions

๐Ÿ”— Nodes Used

HTTP Request, Postgres, Schedule Trigger

๐Ÿ“ฅ Import

Download workflow.json and import into n8n: Workflow menu โ†’ Import from File

๐Ÿ“– Importing guide ยท ๐Ÿ”‘ Credential setup