An implementation of LangChain vectorstore abstraction usingThe code lives in an integration package called: langchain-postgres.postgresas the backend and utilizing thepgvectorextension.
Status
This code has been ported over fromlangchain-community into a dedicated package called langchain-postgres. The following changes have been made:
langchain-postgresworks only with psycopg3. Please update your connnecion strings frompostgresql+psycopg2://...topostgresql+psycopg://langchain:langchain@...(yes, it’s the driver name ispsycopgnotpsycopg3, but it’ll usepsycopg3.- 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.
Setup
First download the partner package: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 usePGVector 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 thelangchain-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
- OpenAI
- Azure
- Google Gemini
- Gemini Enterprise Agent Platform
- AWS
- HuggingFace
- Ollama
- Cohere
- MistralAI
- Nomic
- NVIDIA
- Voyage AI
- IBM watsonx
- Fake
- Isaacus
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: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 allPGVector VectorStore features and configurations head to the PGVector API reference.
Connect these docs to your agent of choice via MCP for real-time answers.

