langchain-ai / langgraph-studio

Desktop app for prototyping and debugging LangGraph applications locally.
https://studio.langchain.com
1.8k stars 120 forks source link

How to : Specify a local postgres database while running LangGraph Studio #158

Open itsabhinavjain opened 2 weeks ago

itsabhinavjain commented 2 weeks ago

Is it possible to specify the postgres host and database to use while running LangGraph Studio. Managing named volumes between runs and updates is causing problems and confusion.

Kubikasz commented 4 days ago

I have the same issue. I want to use my local postgres to from the langGraph studio to run some queries

joaoaugustogrobe commented 3 days ago

@Kubikasz @itsabhinavjain Not local, but I was able to connect to another postgres instance running on docker (on a separated docker-compose file), but it should also work for local connections.

So basically I have two postgres instances running, one for langgraph studio, which we don't have much control, and another one for my service.

I had to connect my pg instance to the langgraph network

  1. List the networks docker network ls You will find something like:

    NETWORK ID     NAME                                                                                             DRIVER    SCOPE
    9b86ded9efda   react-agent-1-49ae538cc4549987339a1f7f3_default           bridge    local
    ...

    We need the network id of your langgraph project

  2. Get the container ID of your pg instance docker ps

  3. Connect your pg instance to the langgraph network: docker network connect <network_id> <your_pg_container_id>

  4. Now you can connect using the alias of your pg instance docker network inspect <network_id> you will find the Containers conected to this instance, just use the name instead of localhost

It's important to connect your pg instance to the langgraph network, and not the other way around. The langgraph api restarts on every change, so it disconnects from any other connected network.

Also, do not override the env vars POSTGRES_HOST, etc. Looks like langgraph api is using those env vars. I recommend adding a preffix.

RAG_POSTGRES_HOST=my-instance.postgres
RAG_POSTGRES_USERNAME=postgres
RAG_POSTGRES_PASSWORD=postgres
RAG_POSTGRES_DATABASE=postgres
RAG_POSTGRES_PORT=5432

For local connection, looks like the langgraph postgres is not exposing the PG port, so you may need to add a port binding via cli, but you may lost if on every change/restart of the container, so I would recommend using it on another container, as I described above