diff --git a/docker-compose/withPostgresPGVectorExtension/.env b/docker-compose/withPostgresPGVectorExtension/.env new file mode 100644 index 00000000..90b6726e --- /dev/null +++ b/docker-compose/withPostgresPGVectorExtension/.env @@ -0,0 +1,6 @@ +POSTGRES_USER=changeUser +POSTGRES_PASSWORD=changePassword +POSTGRES_DB=n8n + +POSTGRES_NON_ROOT_USER=changeUser +POSTGRES_NON_ROOT_PASSWORD=changePassword diff --git a/docker-compose/withPostgresPGVectorExtension/README.md b/docker-compose/withPostgresPGVectorExtension/README.md new file mode 100644 index 00000000..575c30c5 --- /dev/null +++ b/docker-compose/withPostgresPGVectorExtension/README.md @@ -0,0 +1,70 @@ +# n8n with PostgreSQL and pgvector Extension + +Starts n8n with PostgreSQL as database, including the PGvector extension for vector similarity searches. + +## Features + +- PostgreSQL database for n8n +- PGvector extension for storing and querying vector embeddings + +## What's Possible? + +This Docker Compose setup delivers a ready-to-use PostgreSQL database with **PGvector** extension, enabling powerful vector-based AI capabilities right out of the box. + +### Example: RAG Workflow with pgvector + +The diagram below shows a sample n8n workflow that demonstrates Retrieval-Augmented Generation (RAG) capabilities using the PostgreSQL/pgvector backend: + +![AI-Powered Semantic Search Workflow](./rag_pg_vector_workflow_example.png) + +This example workflow illustrates: +- Document embeddings storage using Google Gemini embeddings 001 +- Semantic similarity searches against your vector database +- AI reasoning with Google Gemini Flash 2.0 as the LLM +- A complete RAG pipeline implementation with minimal configuration + +## Start + +To start n8n with PostgreSQL and pgvector extension, simply run docker-compose by executing the following command in the current folder. + +**IMPORTANT:** But before you do that change the default users and passwords in the [`.env`](.env) file! + +``` +docker-compose up -d +``` + +To stop it execute: + +``` +docker-compose stop +``` + +## Configuration + +The default name of the database, user and password for PostgreSQL can be changed in the [`.env`](.env) file in the current directory. + +## PGVector Store Information + +This setup automatically creates: + +1. The pgvector extension in your PostgreSQL database +2. An embeddings table with the following schema: + +```sql +CREATE TABLE embeddings ( + id SERIAL PRIMARY KEY, + embedding vector, + text text, + created_at timestamptz DEFAULT now() +); +``` + +You can use this table to store vector embeddings from AI models for semantic search, similarity comparison, and other vector-based operations in your n8n workflows. + +## Accessing PostgreSQL with pgvector + +The PostgreSQL instance is exposed on port 15432 (to avoid conflicts with any local PostgreSQL installations). You can connect to it using: + +``` +psql -h localhost -p 15432 -U [POSTGRES_USER] -d [POSTGRES_DB] +``` diff --git a/docker-compose/withPostgresPGVectorExtension/docker-compose.yml b/docker-compose/withPostgresPGVectorExtension/docker-compose.yml new file mode 100644 index 00000000..a8a044e1 --- /dev/null +++ b/docker-compose/withPostgresPGVectorExtension/docker-compose.yml @@ -0,0 +1,55 @@ +version: '3.8' + +volumes: + pgvector_storage: + n8n_storage: + +networks: + n8n-network: + driver: bridge + +services: + postgres-pgvector: + image: pgvector/pgvector:pg17 + container_name: postgres-pgvector + ports: + - 15432:5432 + restart: always + environment: + - POSTGRES_USER + - POSTGRES_PASSWORD + - POSTGRES_DB + - POSTGRES_NON_ROOT_USER + - POSTGRES_NON_ROOT_PASSWORD + volumes: + - pgvector_storage:/var/lib/postgresql/data + - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh + healthcheck: + test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}'] + interval: 5s + timeout: 5s + retries: 10 + networks: + - n8n-network + + n8n: + image: docker.n8n.io/n8nio/n8n + restart: always + environment: + - DB_TYPE=postgresdb + - DB_POSTGRESDB_HOST=postgres-pgvector + - DB_POSTGRESDB_PORT=5432 + - DB_POSTGRESDB_DATABASE=${POSTGRES_DB} + - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER} + - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD} + ports: + - 5678:5678 + links: + - postgres-pgvector + volumes: + - n8n_storage:/home/node/.n8n + depends_on: + postgres-pgvector: + condition: service_healthy + networks: + - n8n-network diff --git a/docker-compose/withPostgresPGVectorExtension/init-data.sh b/docker-compose/withPostgresPGVectorExtension/init-data.sh new file mode 100755 index 00000000..c662f705 --- /dev/null +++ b/docker-compose/withPostgresPGVectorExtension/init-data.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e; + + +if [ -n "${POSTGRES_NON_ROOT_USER:-}" ] && [ -n "${POSTGRES_NON_ROOT_PASSWORD:-}" ]; then + psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE USER ${POSTGRES_NON_ROOT_USER} WITH PASSWORD '${POSTGRES_NON_ROOT_PASSWORD}'; + GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_NON_ROOT_USER}; + GRANT CREATE ON SCHEMA public TO ${POSTGRES_NON_ROOT_USER}; + EOSQL + + # Create vector extension and embeddings table + echo "Creating vector extension and embeddings table..." + if ! psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL + CREATE EXTENSION IF NOT EXISTS vector; + + CREATE TABLE IF NOT EXISTS embeddings ( + id SERIAL PRIMARY KEY, + embedding vector, + text text, + created_at timestamptz DEFAULT now() + ); + EOSQL + then + echo "ERROR: Failed to create vector extension or embeddings table" + exit 1 + fi + echo "Vector extension and embeddings table created successfully" +else + echo "SETUP INFO: No Environment variables given!" +fi diff --git a/docker-compose/withPostgresPGVectorExtension/rag_pg_vector_workflow_example.png b/docker-compose/withPostgresPGVectorExtension/rag_pg_vector_workflow_example.png new file mode 100644 index 00000000..bbfb52a6 Binary files /dev/null and b/docker-compose/withPostgresPGVectorExtension/rag_pg_vector_workflow_example.png differ