Logo

dev-resources.site

for different kinds of informations.

Setup PostgreSQL w/ pgvector in a docker container

Published at
11/29/2024
Categories
postgres
vectordatabase
docker
javascript
Author
ninjasoards
Author
11 person written this
ninjasoards
open
Setup PostgreSQL w/ pgvector in a docker container

This post is a follow-up to my previous post on how to setup a local MySQL instance in docker.

RAG (Retrieval Augmented Generation) is quickly becoming the "Hello World" of AI apps. If you are working or playing with Large Language Models, you will no doubt need to create a RAG pipeline at some point. An important component of RAG is a vector database, and a popular option is pgvector - an open-source vector similarity search for Postgres. Here's how to quickly setup a local instance in a Docker container.

Pull and run the image

Pull the latest image from the docker repository. ReplaceΒ 17Β with your Postgres server version of choice.

docker pull pgvector/pgvector:pg17
Enter fullscreen mode Exit fullscreen mode

Run the image, set the root user password, and expose the default Postgres port.

docker run -d --name <container_name> -e POSTGRES_PASSWORD=postgres -p 5432:5432 pgvector/pgvector:pg17
Enter fullscreen mode Exit fullscreen mode

Create a db inside the container

With the Postgres server running, create a database inside the container.

docker exec -it <container_name> createdb -U postgres <database_name>
Enter fullscreen mode Exit fullscreen mode

Connect to the database

Now we can connect to the database from our application and initialize the pgvector extension. I'll be using JavaScript. Setting up the entire application is outside the scope of this post, but you will need to install a couple dependencies:

pnpm add pg pgvector
Enter fullscreen mode Exit fullscreen mode

Set a DATABASE_URL in your environment. I use a .env file. It should follow this format:

DATABASE_URL=postgresql://<pg_user>:<pg_password>@localhost:5432/<database_name>
Enter fullscreen mode Exit fullscreen mode

For local development use @localhost, but if you are using something like docker-compose.yml and have named the service, you should use the name of the service e.g. @db.

In your application code, create the connection:

const pool = new pg.Pool({
  connectionString: process.env.DATABASE_URL,
});
Enter fullscreen mode Exit fullscreen mode

Then, initialize pgvector and create a new table:

async function createStore() {
  // Initialize pgvector extension and create table if not exists
  await pool.query('CREATE EXTENSION IF NOT EXISTS vector');

  return {
    vectorStore: await PGVectorStore.initialize(embeddings, {
      postgresConnectionOptions: {
        connectionString: process.env.DATABASE_URL,
      },
      tableName: 'documents', // Default table name
    }),
  };
}
Enter fullscreen mode Exit fullscreen mode

With the vectorStore setup, you can add content to it using vectorStore.addDocuments and query for context using vectorStore.similaritySearch.

That's it for this post. Maybe next time I will explore more specific uses of pgvector, and/or using it with Drizzle ORM! πŸ‘‹

vectordatabase Article's
30 articles in total
Favicon
Binary embedding: shrink vector storage by 95%
Favicon
Analyzing LinkedIn Company Posts with Graphs and Agents
Favicon
OpenSearchCon Europe 2025 - Amsterdam!
Favicon
The Best Embedding Models for Information Retrieval in 2025
Favicon
How to Chat with PDFs Using AI via API
Favicon
FalkorDB has integrated with cognee to improve AI-driven knowledge retrieval
Favicon
What Founders Must Do in Agentic LLM Era
Favicon
Vector Databases: Your AI's New Best Friend
Favicon
Vector Database for Modern Applications
Favicon
Introducing VecSpark
Favicon
pg_auto_embeddings β€” text embeddings directly in Postgres, without extensions
Favicon
Relational Databases Holding You Back?
Favicon
ChromaDB for the SQL Mind
Favicon
Getting started with LLM APIs
Favicon
Understanding Vector Databases: A Beginner's Guide
Favicon
Setup PostgreSQL w/ pgvector in a docker container
Favicon
Simplest markdown component for your AI apps
Favicon
Semantic search with Azure MS SQL and EF Core
Favicon
Announcing 12 Days of Codemas: The DataStax Holiday Giveaway!
Favicon
Enhancing Hybrid Search in MongoDB: Combining RRF, Thresholds, and Weights
Favicon
Serverless semantic search - AWS Lambda, AWS Bedrock, Neon
Favicon
How to integrate pgvector's Docker image with Langchain?
Favicon
Weekly Updates - Dec 20, 2024
Favicon
Generative AI: A Personal Deep Dive – My Notes and Insights
Favicon
Detecting and Analyzing Comment Quality Using Vector Search
Favicon
Choosing a Vector Store for LangChain
Favicon
Elasticsearch Was Great, But Vector Databases Are the Future
Favicon
Introducing Milvus 2.5: Built-in Full-Text Search, Advanced Query Optimization, and More πŸš€
Favicon
Migrating Vector Data from Milvus to TiDB
Favicon
How to Create Your Own RAG with Free LLM Models and a Knowledge Base

Featured ones: