langflow icon indicating copy to clipboard operation
langflow copied to clipboard

Langflow issue with Posgress on windows

Open yashugupta786 opened this issue 6 months ago • 3 comments

Bug Description

I have installed the langflow and posgress server on windows and langflow is also on windows . I have created a database in posgress and i have created a .env file with

LANGFLOW_DATABASE_URL=postgresql://postgres:admin123@localhost:5432/langflow_db Then running the uv run langflow run --env-file .env

My DB looks as +-----------+----------------------- langflow_db | postgres | WIN1252 | libc | English_United States.1252 | English_United States.1252 |

But it gives error

Reproduction

[05/28/25 18:04:38] ERROR 2025-05-28 18:04:38 - ERROR - service - Database error during session scope: (psycopg.InterfaceError) service.py:190 Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for
instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
(Background on this error at: https://sqlalche.me/e/20/rvf5)
ERROR 2025-05-28 18:04:38 - ERROR - utils - Error creating DB and tablesTraceback (most recent call last): utils.py:31

                           File                  

Expected behavior

[05/28/25 18:04:38] ERROR 2025-05-28 18:04:38 - ERROR - service - Database error during session scope: (psycopg.InterfaceError) service.py:190 Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for
instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'
(Background on this error at: https://sqlalche.me/e/20/rvf5)
ERROR 2025-05-28 18:04:38 - ERROR - utils - Error creating DB and tablesTraceback (most recent call last): utils.py:31

                           File                  

Who can help?

No response

Operating System

windows

Langflow Version

latest

Python Version

3.10

Screenshot

No response

Flow File

No response

yashugupta786 avatar May 28 '25 12:05 yashugupta786

The error you're seeing—psycopg.InterfaceError: Psycopg cannot use the 'ProactorEventLoop' to run in async mode. Please use a compatible event loop, for instance by setting 'asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())'—is due to a known incompatibility between the default event loop on Windows (ProactorEventLoop) and async database drivers like psycopg.

To resolve this, you need to set the event loop policy to WindowsSelectorEventLoopPolicy before any async operations start. Since Langflow does not currently set this automatically and there is no built-in configuration or documentation for this in the codebase, you will need to do this manually.

How to fix:

  1. Locate the entry point script you use to start Langflow (for example, if you are running uv run langflow run --env-file .env, you may need to create a small wrapper script).

  2. At the very top of your script—before importing anything else—add:

    import asyncio
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    
  3. Then, proceed to import and run Langflow as usual.

If you are using a command-line tool that does not allow you to modify the entry point directly, consider creating a new Python script (e.g., start_langflow.py) with the above two lines, followed by the code to launch Langflow.

This workaround is compatible with Python 3.10 and the latest Langflow version. There are no known issues with this approach for your setup, and it should resolve the psycopg event loop error on Windows.

If you need more specific guidance on where to place these lines based on your setup, let me know how you are launching Langflow (e.g., via a script, CLI, or another method).

To reply, just mention my name @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar May 28 '25 12:05 dosubot[bot]

@dosu I am not sure how to do that and where to do that . Please help

yashugupta786 avatar May 28 '25 13:05 yashugupta786

Here’s how you can fix the event loop error on Windows when running Langflow:

  1. Open a text editor and create a new file named start_langflow.py in the same folder where you would normally run your Langflow command.

  2. Copy and paste the following code into that file:

import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

import uvicorn

# This runs the same command as 'uv run langflow run --env-file .env'
uvicorn.run(
    "langflow.main:app",
    host="127.0.0.1",
    port=7860,
    reload=True,
    env_file=".env"
)
  1. Save the file.

  2. Instead of running your usual command (like uv run langflow run --env-file .env), run:

python start_langflow.py

This sets the correct event loop policy before anything else runs, which should resolve the psycopg error.

If you use a different way to start Langflow (for example, with a different command or tool), let me know how you launch it and I can adjust the instructions for your setup.

To reply, just mention my name @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Join Discord Share on X

dosubot[bot] avatar May 28 '25 13:05 dosubot[bot]

Thanks for the report! From your screenshots/logs, Postgres is reachable but Langflow is querying tables that don’t exist yet (e.g. relation "flows" does not exist). That means the database schema/migrations were not applied. On Windows this usually happens when Langflow was installed without the Postgres extra (so the driver isn’t present) and/or when the app is launched without the env var being loaded in the same process.

Here’s a clean setup that fixes it:

# 1) Install Langflow with Postgres extra (includes psycopg)
uv run pip install "langflow[postgres]"
# (optional, just to be explicit)
uv run pip install "psycopg[binary]"

# 2) Create a .env next to where you run Langflow (adjust values)
# .env
LANGFLOW_DATABASE_URL=postgresql+psycopg://USER:PASS@HOST:5432/DBNAME
LANGFLOW_DEBUG_LEVEL=debug

# 3) Apply migrations (creates the tables)
uv run langflow db upgrade --env-file .env
# (if your version doesn't have `db upgrade`, try `uv run langflow migrate --env-file .env`)

# 4) Start Langflow using the same .env
uv run langflow run --env-file .env

Let me know if this solved it for you

italojohnny avatar Sep 04 '25 15:09 italojohnny

@italojohnny uv run pip install "langflow[postgres]" command,The download is very slow. When I started the program using the source code and configured the database with the env configuration file, I still encountered a ProactorEventLoop error. Could you please tell me what I should do to resolve this issue?

Zhenshuai1 avatar Sep 11 '25 06:09 Zhenshuai1

I tested this locally on my machine:

  1. Created a PostgreSQL container
  2. Set up a Windows VM and configured the network adapter in bridge mode
  3. Installed LangFlow on Windows using the command "uv run pip install 'langflow[postgres]'"
  4. Created a .env file with database configuration pointing to my container on macOS
  5. LangFlow worked as expected, and even after stopping and restarting, the projects remained persisted
Image Image Image Image

@Empreiteiro , the Langflow code is working normally for me, and I believe this isn’t a bug but rather an environment issue on the user’s side.

italojohnny avatar Sep 16 '25 18:09 italojohnny

@italojohnny In my case, I was only able to start Langflow with Postgres on Windows by modifying the main.py file:

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

Image

Empreiteiro avatar Sep 17 '25 12:09 Empreiteiro