postgresqlsupabasesupabase-databasellama-index

SupabaseVectorStore connection (FastAPI backend on Render) failing with "OperationalError... Network is unreachable"


I have a Python backend for my LlamaIndex application (using FastAPI + Render) that uses "SupabaseVectorStore" to set up the connection with my Supabase DB. It works 100% fine locally on localhost, but when I host it in Render, I suddenly get this error:

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) connection to server at "db.[USER_HERE].supabase.co", port [PORT_NUMBER] failed: Network is unreachable

Is the server running on that host and accepting TCP/IP connections?

The code where the Supabase DB connection is setup is:

import logging
import os

from llama_index.core.indices import VectorStoreIndex
from llama_index.vector_stores.pinecone import PineconeVectorStore
from llama_index.vector_stores.supabase import SupabaseVectorStore
from llama_index.core import StorageContext

logger = logging.getLogger("uvicorn")


def get_index():
    logger.info("Connecting to index from Supabase...")
    vector_store = SupabaseVectorStore(
        postgres_connection_string=f"postgresql://{os.environ['SUPABASE_DB_URL']}",
        collection_name="daily_notes",
    )
    storage_context = StorageContext.from_defaults(vector_store=vector_store)
    index = VectorStoreIndex(nodes=[], storage_context=storage_context)

    logger.info("Finished connecting to index from Supabase.")
    return index

where:

SUPABASE_DB_URL = postgresql://postgres:[YOUR-PASSWORD]@db.[USER].supabase.co:[PORT]/postgres

UPDATE: Since reading this post from Supabase, I also tried the "pooling string", where (in addition to updating the code to start with "postgres://"):

SUPABASE_DB_URL = postgres://postgres.[USER]:[YOUR-PASSWORD]@aws-0-us-west-1.pooler.supabase.com:[PORT]/postgres

But this now yields:

sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres

Anyone have any idea how to fix this? I've been grappling with this for HOURS and am completely lost... 😭


Solution

  • Note that as LLamaIndex users SQLAlchemy to connect to Supabase's PostgreSQL database, you should use postgresql:// instead of postgres:// as your dialect.