Postgres Vector Store
Topics
Section titled “Topics”Overview
Section titled “Overview”You can use a Postgres database with the pgvector extension as a vector store.
Install dependencies
Section titled “Install dependencies”The Postgres vector store requires both the psycopg2 and pgvector packages:
pip install psycopg2-binary pgvectorCreating a Postgres vector store
Section titled “Creating a Postgres vector store”Use the VectorStoreFactory.for_vector_store() static factory method to create an instance of a Postgres vector store.
To create a Postgres vector store, supply a connection string in the following format:
postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]For example:
postgresql://graphrag:!zfg%dGGh@mydbcluster.cluster-123456789012.us-west-2.rds.amazonaws.com:5432/postgresConnecting to an IAM auth-enabled Postgres vector store
Section titled “Connecting to an IAM auth-enabled Postgres vector store”If your Postgres database supports AWS Identity and Access Management (IAM) database authentication, omit the password, and add enable_iam_db_auth=True to the connection string query parameters:
postgresql://graphrag@mydbcluster.cluster-123456789012.us-west-2.rds.amazonaws.com:5432/postgres?enable_iam_db_auth=TrueYou will need to create a database user, and grant the rds_iam role to use IAM authentication.
The connection uses TLS by default (sslmode=require), and fails if the server does not support it. To choose a different mode, add sslmode to the connection string query parameters, or set the PGSSLMODE environment variable. A mode in the connection string takes precedence over PGSSLMODE.
require encrypts the connection but does not check the server’s certificate, so another host can present any certificate and intercept it. If sslrootcert is set, or ~/.postgresql/root.crt exists, libpq verifies the server certificate against that CA even under require, as it does for verify-ca. For production, use sslmode=verify-full to verify the certificate and host name, and point sslrootcert at the CA certificate. For Amazon RDS and Aurora, that is the RDS certificate bundle:
postgresql://graphrag@mydbcluster.cluster-123456789012.us-west-2.rds.amazonaws.com:5432/postgres?enable_iam_db_auth=True&sslmode=verify-full&sslrootcert=/path/to/global-bundle.pemA local Postgres that has no TLS configured, such as a Docker container, needs sslmode=disable:
postgresql://postgres:password@localhost:5432/graphrag?sslmode=disable