Skip to main content
An implementation of LangChain vectorstore abstraction using postgres as the backend and utilizing the pgvector extension.
The code lives in an integration package called: langchain-postgres.

Status

This code has been ported over from langchain-community into a dedicated package called langchain-postgres. The following changes have been made:
  • langchain-postgres works only with psycopg3. Please update your connnecion strings from postgresql+psycopg2://... to postgresql+psycopg://langchain:langchain@... (yes, it’s the driver name is psycopg not psycopg3, but it’ll use psycopg3.
  • The schema of the embedding store and collection have been changed to make add_documents work correctly with user specified ids.
  • One has to pass an explicit connection object now.
Currently, there is no mechanism that supports easy data migration on schema changes. Any schema changes in the vectorstore will require the user to recreate the tables and re-add the documents. If this is a concern, please use a different vectorstore. If not, this implementation should be fine for your use case.

Setup

First download the partner package:
You can run the following command to spin up a postgres container with the pgvector extension:

Use Azure Database for PostgreSQL

Azure Database for PostgreSQL - Flexible Server is a relational database service based on the open-source Postgres database engine. It is a fully managed database-as-a-service that can handle mission-critical workloads with predictable performance, security, high availability, and dynamic scalability. To use PGVector with Azure Database for PostgreSQL, set up a server and enable the pgvector extension. Use the connection string from your Azure portal when instantiating the vector store.

Credentials

There are no credentials needed to run this notebook, just make sure you downloaded the langchain-postgres package and correctly started the postgres container. If you want to get best in-class automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Instantiation

Manage vector store

Add items to vector store

Note that adding documents by ID will over-write any existing documents that match that ID.

Delete items from vector store

Query vector store

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

Filtering support

The vectorstore supports a set of filters that can be applied against the metadata fields of the documents.

Query directly

Performing a simple similarity search can be done as follows:
If you provide a dict with multiple fields, but no operators, the top level will be interpreted as a logical AND filter
If you want to execute a similarity search and receive the corresponding scores you can run:
For a full list of the different searches you can execute on a PGVector vector store, see the PGVector API reference.

Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains.

Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

API reference

For detailed documentation of all PGVector VectorStore features and configurations head to the PGVector API reference.