!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

You are viewing the **preview** v0.2 docs. View the **stable** v0.1 docs [here](/v0.1/docs/get_started/introduction/). Leave feedback on the v0.2 docs [here](https://github.com/langchain-ai/langchainjs/discussions/5386).

[

![🦜️🔗 Langchain](/v0.2/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.2/img/brand/wordmark-dark.png)

](/v0.2/)[Integrations](/v0.2/docs/integrations/platforms/)[API Reference](https://v02.api.js.langchain.com)

[More](#)

*   [People](/v0.2/docs/people/)
*   [Community](/v0.2/docs/community)
*   [Tutorials](/v0.2/docs/additional_resources/tutorials)
*   [Contributing](/v0.2/docs/contributing)

[v0.2](#)

*   [v0.2](/v0.2/docs/introduction)
*   [v0.1](https://js.langchain.com/v0.1/docs/get_started/introduction)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.2/docs/integrations/platforms/)
    
    *   [Providers](/v0.2/docs/integrations/platforms/)
    *   [Anthropic](/v0.2/docs/integrations/platforms/anthropic)
    *   [AWS](/v0.2/docs/integrations/platforms/aws)
    *   [Google](/v0.2/docs/integrations/platforms/google)
    *   [Microsoft](/v0.2/docs/integrations/platforms/microsoft)
    *   [OpenAI](/v0.2/docs/integrations/platforms/openai)
*   [Components](/v0.2/docs/integrations/components)
    
    *   [Chat models](/v0.2/docs/integrations/chat/)
        
    *   [LLMs](/v0.2/docs/integrations/llms/)
        
    *   [Embedding models](/v0.2/docs/integrations/text_embedding)
        
    *   [Document loaders](/v0.2/docs/integrations/document_loaders)
        
    *   [Document transformers](/v0.2/docs/integrations/document_transformers)
        
    *   [Vector stores](/v0.2/docs/integrations/vectorstores)
        
        *   [Memory](/v0.2/docs/integrations/vectorstores/memory)
        *   [AnalyticDB](/v0.2/docs/integrations/vectorstores/analyticdb)
        *   [Astra DB](/v0.2/docs/integrations/vectorstores/astradb)
        *   [Azure AI Search](/v0.2/docs/integrations/vectorstores/azure_aisearch)
        *   [Azure Cosmos DB](/v0.2/docs/integrations/vectorstores/azure_cosmosdb)
        *   [Cassandra](/v0.2/docs/integrations/vectorstores/cassandra)
        *   [Chroma](/v0.2/docs/integrations/vectorstores/chroma)
        *   [ClickHouse](/v0.2/docs/integrations/vectorstores/clickhouse)
        *   [CloseVector](/v0.2/docs/integrations/vectorstores/closevector)
        *   [Cloudflare Vectorize](/v0.2/docs/integrations/vectorstores/cloudflare_vectorize)
        *   [Convex](/v0.2/docs/integrations/vectorstores/convex)
        *   [Couchbase](/v0.2/docs/integrations/vectorstores/couchbase)
        *   [Elasticsearch](/v0.2/docs/integrations/vectorstores/elasticsearch)
        *   [Faiss](/v0.2/docs/integrations/vectorstores/faiss)
        *   [Google Vertex AI Matching Engine](/v0.2/docs/integrations/vectorstores/googlevertexai)
        *   [SAP HANA Cloud Vector Engine](/v0.2/docs/integrations/vectorstores/hanavector)
        *   [HNSWLib](/v0.2/docs/integrations/vectorstores/hnswlib)
        *   [LanceDB](/v0.2/docs/integrations/vectorstores/lancedb)
        *   [Milvus](/v0.2/docs/integrations/vectorstores/milvus)
        *   [Momento Vector Index (MVI)](/v0.2/docs/integrations/vectorstores/momento_vector_index)
        *   [MongoDB Atlas](/v0.2/docs/integrations/vectorstores/mongodb_atlas)
        *   [MyScale](/v0.2/docs/integrations/vectorstores/myscale)
        *   [Neo4j Vector Index](/v0.2/docs/integrations/vectorstores/neo4jvector)
        *   [Neon Postgres](/v0.2/docs/integrations/vectorstores/neon)
        *   [OpenSearch](/v0.2/docs/integrations/vectorstores/opensearch)
        *   [PGVector](/v0.2/docs/integrations/vectorstores/pgvector)
        *   [Pinecone](/v0.2/docs/integrations/vectorstores/pinecone)
        *   [Prisma](/v0.2/docs/integrations/vectorstores/prisma)
        *   [Qdrant](/v0.2/docs/integrations/vectorstores/qdrant)
        *   [Redis](/v0.2/docs/integrations/vectorstores/redis)
        *   [Rockset](/v0.2/docs/integrations/vectorstores/rockset)
        *   [SingleStore](/v0.2/docs/integrations/vectorstores/singlestore)
        *   [Supabase](/v0.2/docs/integrations/vectorstores/supabase)
        *   [Tigris](/v0.2/docs/integrations/vectorstores/tigris)
        *   [Turbopuffer](/v0.2/docs/integrations/vectorstores/turbopuffer)
        *   [TypeORM](/v0.2/docs/integrations/vectorstores/typeorm)
        *   [Typesense](/v0.2/docs/integrations/vectorstores/typesense)
        *   [Upstash Vector](/v0.2/docs/integrations/vectorstores/upstash)
        *   [USearch](/v0.2/docs/integrations/vectorstores/usearch)
        *   [Vectara](/v0.2/docs/integrations/vectorstores/vectara)
        *   [Vercel Postgres](/v0.2/docs/integrations/vectorstores/vercel_postgres)
        *   [Voy](/v0.2/docs/integrations/vectorstores/voy)
        *   [Weaviate](/v0.2/docs/integrations/vectorstores/weaviate)
        *   [Xata](/v0.2/docs/integrations/vectorstores/xata)
        *   [Zep](/v0.2/docs/integrations/vectorstores/zep)
    *   [Retrievers](/v0.2/docs/integrations/retrievers)
        
    *   [Tools](/v0.2/docs/integrations/tools)
        
    *   [Toolkits](/v0.2/docs/integrations/toolkits)
        
    *   [Stores](/v0.2/docs/integrations/stores/)
        

*   [](/v0.2/)
*   [Components](/v0.2/docs/integrations/components)
*   [Vector stores](/v0.2/docs/integrations/vectorstores)
*   Vercel Postgres

On this page

Vercel Postgres
===============

LangChain.js supports using the [`@vercel/postgres`](https://www.npmjs.com/package/@vercel/postgres) package to use generic Postgres databases as vector stores, provided they support the [`pgvector`](https://github.com/pgvector/pgvector) Postgres extension.

This integration is particularly useful from web environments like Edge functions.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

To work with Vercel Postgres, you need to install the `@vercel/postgres` package:

*   npm
*   Yarn
*   pnpm

    npm install @vercel/postgres

    yarn add @vercel/postgres

    pnpm add @vercel/postgres

tip

See [this section for general instructions on installing integration packages](/v0.2/docs/how_to/installation#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install @langchain/community

    yarn add @langchain/community

    pnpm add @langchain/community

This integration automatically connects using the connection string set under `process.env.POSTGRES_URL`. You can also pass a connection string manually like this:

    const vectorstore = await VercelPostgres.initialize(new OpenAIEmbeddings(), {  postgresConnectionOptions: {    connectionString:      "postgres://<username>:<password>@<hostname>:<port>/<dbname>",  },});

### Connecting to Vercel Postgres[​](#connecting-to-vercel-postgres "Direct link to Connecting to Vercel Postgres")

A simple way to get started is to create a serverless [Vercel Postgres instance](https://vercel.com/docs/storage/vercel-postgres/quickstart). If you're deploying to a Vercel project with an associated Vercel Postgres instance, the required `POSTGRES_URL` environment variable will already be populated in hosted environments.

### Connecting to other databases[​](#connecting-to-other-databases "Direct link to Connecting to other databases")

If you prefer to host your own Postgres instance, you can use a similar flow to LangChain's [PGVector](/v0.2/docs/integrations/vectorstores/pgvector) vectorstore integration and set the connection string either as an environment variable or as shown above.

Usage[​](#usage "Direct link to Usage")
---------------------------------------

    import { CohereEmbeddings } from "@langchain/cohere";import { VercelPostgres } from "@langchain/community/vectorstores/vercel_postgres";// Config is only required if you want to override default values.const config = {  // tableName: "testvercelvectorstorelangchain",  // postgresConnectionOptions: {  //   connectionString: "postgres://<username>:<password>@<hostname>:<port>/<dbname>",  // },  // columns: {  //   idColumnName: "id",  //   vectorColumnName: "vector",  //   contentColumnName: "content",  //   metadataColumnName: "metadata",  // },};const vercelPostgresStore = await VercelPostgres.initialize(  new CohereEmbeddings(),  config);const docHello = {  pageContent: "hello",  metadata: { topic: "nonsense" },};const docHi = { pageContent: "hi", metadata: { topic: "nonsense" } };const docMitochondria = {  pageContent: "Mitochondria is the powerhouse of the cell",  metadata: { topic: "science" },};const ids = await vercelPostgresStore.addDocuments([  docHello,  docHi,  docMitochondria,]);const results = await vercelPostgresStore.similaritySearch("hello", 2);console.log(results);/*  [    Document { pageContent: 'hello', metadata: { topic: 'nonsense' } },    Document { pageContent: 'hi', metadata: { topic: 'nonsense' } }  ]*/// Metadata filteringconst results2 = await vercelPostgresStore.similaritySearch(  "Irrelevant query, metadata filtering",  2,  {    topic: "science",  });console.log(results2);/*  [    Document {      pageContent: 'Mitochondria is the powerhouse of the cell',      metadata: { topic: 'science' }    }  ]*/// Metadata filtering with IN-filters works as wellconst results3 = await vercelPostgresStore.similaritySearch(  "Irrelevant query, metadata filtering",  3,  {    topic: { in: ["science", "nonsense"] },  });console.log(results3);/*  [    Document {      pageContent: 'hello',      metadata: { topic: 'nonsense' }    },    Document {      pageContent: 'hi',      metadata: { topic: 'nonsense' }    },    Document {      pageContent: 'Mitochondria is the powerhouse of the cell',      metadata: { topic: 'science' }    }  ]*/// Upserting is supported as wellawait vercelPostgresStore.addDocuments(  [    {      pageContent: "ATP is the powerhouse of the cell",      metadata: { topic: "science" },    },  ],  { ids: [ids[2]] });const results4 = await vercelPostgresStore.similaritySearch(  "What is the powerhouse of the cell?",  1);console.log(results4);/*  [    Document {      pageContent: 'ATP is the powerhouse of the cell',      metadata: { topic: 'science' }    }  ]*/await vercelPostgresStore.delete({ ids: [ids[2]] });const results5 = await vercelPostgresStore.similaritySearch(  "No more metadata",  2,  {    topic: "science",  });console.log(results5);/*  []*/// Remember to call .end() to close the connection!await vercelPostgresStore.end();

#### API Reference:

*   [CohereEmbeddings](https://v02.api.js.langchain.com/classes/langchain_cohere.CohereEmbeddings.html) from `@langchain/cohere`
*   [VercelPostgres](https://v02.api.js.langchain.com/classes/langchain_community_vectorstores_vercel_postgres.VercelPostgres.html) from `@langchain/community/vectorstores/vercel_postgres`

* * *

#### Was this page helpful?

  

#### You can leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

Vectara

](/v0.2/docs/integrations/vectorstores/vectara)[

Next

Voy

](/v0.2/docs/integrations/vectorstores/voy)

*   [Setup](#setup)
    *   [Connecting to Vercel Postgres](#connecting-to-vercel-postgres)
    *   [Connecting to other databases](#connecting-to-other-databases)
*   [Usage](#usage)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.