Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docker-compose/withPostgresPGVectorExtension/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
POSTGRES_USER=changeUser
POSTGRES_PASSWORD=changePassword
POSTGRES_DB=n8n

POSTGRES_NON_ROOT_USER=changeUser
POSTGRES_NON_ROOT_PASSWORD=changePassword
70 changes: 70 additions & 0 deletions docker-compose/withPostgresPGVectorExtension/README.md
Original file line number Diff line number Diff line change
@@ -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]
```
55 changes: 55 additions & 0 deletions docker-compose/withPostgresPGVectorExtension/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions docker-compose/withPostgresPGVectorExtension/init-data.sh
Original file line number Diff line number Diff line change
@@ -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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.