Skip to main content
Cloud SQL is a fully managed relational database service that offers high performance, seamless integration, and impressive scalability and offers database engines such as PostgreSQL. This guide provides a quick overview of how to use Cloud SQL for PostgreSQL to store vector embeddings with the PostgresVectorStore class.

Overview

Integration details

ClassPackagePY supportVersion
PostgresVectorStore@langchain/google-cloud-sql-pgNPM - Version

Before you begin

In order to use this package, you first need to go through the following steps:
  1. Select or create a Cloud Platform project.
  2. Enable billing for your project.
  3. Enable the Cloud SQL Admin API.
  4. Setup Authentication.
  5. Create a CloudSQL instance
  6. Create a CloudSQL database
  7. Add a user to the database

Authentication

Authenticate locally to your Google Cloud account using the gcloud auth login command.

Set your Google cloud project

Set your Google Cloud project ID to leverage Google Cloud resources locally:
If you don’t know your project ID, try the following:

Setting up a PostgresVectorStore instance

To use the PostgresVectorStore library, you’ll need to install the @langchain/google-cloud-sql-pg package and then follow the steps below.
First, you’ll need to log in to your Google Cloud account and set the following environment variables based on your Google Cloud project; these will be defined based on how you want to configure (fromInstance, fromEngine, fromEngineArgs) your PostgresEngine instance:

Setting up an instance

To instantiate a PostgresVectorStore, you’ll first need to create a database connection through the PostgresEngine, then initialize the vector store table and finally call the .initialize() method to instantiate the vector store.

Manage vector store

Add documents to vector store

To add Documents to the vector store, you would be able to it by passing or not the ids

Delete documents from vector store

You can delete one or more Documents from the vector store by passing the arrays of ids to be deleted:

Search for documents

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.

Query directly

Performing a simple similarity search can be done as follows:
If you want to execute a similarity search and receive the corresponding scores you can run:
The Maximal marginal relevance optimizes for similarity to the query and diversity among selected documents.