Skip to main content

Configure an external PostgreSQL database

Langflow's default database is SQLite, but you can configure Langflow to use PostgreSQL instead.

This guide walks you through setting up an external database for Langflow by replacing the default SQLite connection string sqlite:///./langflow.db with PostgreSQL.

Prerequisite

Connect Langflow to PostgreSQL

To connect Langflow to PostgreSQL, follow these steps.

  1. Find your PostgreSQL database's connection string. It looks like postgresql://user:password@host:port/dbname.

The hostname in your connection string depends on how you're running PostgreSQL.

  • If you're running PostgreSQL directly on your machine, use localhost.
  • If you're running PostgreSQL in Docker Compose, use the service name, such as postgres.
  • If you're running PostgreSQL in a separate Docker container with docker run, use the container's IP address or network alias.
  1. Create a .env file for configuring Langflow.

_10
touch .env

  1. To set the database URL environment variable, add it to your .env file:

_10
LANGFLOW_DATABASE_URL="postgresql://user:password@localhost:5432/dbname"

tip

The Langflow project includes a .env.example file to help you get started. You can copy the contents of this file into your own .env file and replace the example values with your own preferred settings. Replace the value for LANGFLOW_DATABASE_URL with your PostgreSQL connection string.

  1. Run Langflow with the .env file:

_10
uv run langflow run --env-file .env

  1. In Langflow, create traffic by running a flow.
  2. Inspect your PostgreSQL deployment's tables and activity. New tables and traffic are created.

Example Langflow and PostgreSQL docker-compose.yml

The Langflow project includes a docker-compose.yml file for quick deployment with PostgreSQL.

This configuration launches Langflow and PostgreSQL containers in the same Docker network, ensuring proper connectivity between services. It also sets up persistent volumes for both Langflow and PostgreSQL data.

To start the services, navigate to the /docker_example directory, and then run docker-compose up.


_29
services:
_29
langflow:
_29
image: langflowai/langflow:latest # or another version tag on https://hub.docker.com/r/langflowai/langflow
_29
pull_policy: always # set to 'always' when using 'latest' image
_29
ports:
_29
- "7860:7860"
_29
depends_on:
_29
- postgres
_29
environment:
_29
- LANGFLOW_DATABASE_URL=postgresql://langflow:langflow@postgres:5432/langflow
_29
# This variable defines where the logs, file storage, monitor data, and secret keys are stored.
_29
- LANGFLOW_CONFIG_DIR=app/langflow
_29
volumes:
_29
- langflow-data:/app/langflow
_29
_29
postgres:
_29
image: postgres:16
_29
environment:
_29
POSTGRES_USER: langflow
_29
POSTGRES_PASSWORD: langflow
_29
POSTGRES_DB: langflow
_29
ports:
_29
- "5432:5432"
_29
volumes:
_29
- langflow-postgres:/var/lib/postgresql/data
_29
_29
volumes:
_29
langflow-postgres: # Persistent volume for PostgreSQL data
_29
langflow-data: # Persistent volume for Langflow data

note

Docker Compose creates an isolated network for all services defined in the docker-compose.yml file. This ensures that the services can communicate with each other using their service names as hostnames, for example, postgres in the database URL. If you were to run PostgreSQL separately using docker run, it would be in a different network and Langflow wouldn't be able to connect to it using the service name.

Deploy multiple Langflow instances with a shared database

To configure multiple Langflow instances that share the same PostgreSQL database, modify your docker-compose.yml file to include multiple Langflow services.

Use environment variables for more centralized configuration management:

  1. Update your .env file with values for your PostgreSQL database:

_10
POSTGRES_USER=langflow
_10
POSTGRES_PASSWORD=your_secure_password
_10
POSTGRES_DB=langflow
_10
POSTGRES_HOST=postgres
_10
POSTGRES_PORT=5432
_10
LANGFLOW_CONFIG_DIR=app/langflow
_10
LANGFLOW_PORT_1=7860
_10
LANGFLOW_PORT_2=7861
_10
LANGFLOW_HOST=0.0.0.0

  1. Reference these variables in your docker-compose.yml:

_46
services:
_46
postgres:
_46
image: postgres:16
_46
environment:
_46
- POSTGRES_USER=${POSTGRES_USER}
_46
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
_46
- POSTGRES_DB=${POSTGRES_DB}
_46
ports:
_46
- "${POSTGRES_PORT}:5432"
_46
volumes:
_46
- langflow-postgres:/var/lib/postgresql/data
_46
_46
langflow-1:
_46
image: langflowai/langflow:latest
_46
pull_policy: always
_46
ports:
_46
- "${LANGFLOW_PORT_1}:7860"
_46
depends_on:
_46
- postgres
_46
environment:
_46
- LANGFLOW_DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
_46
- LANGFLOW_CONFIG_DIR=${LANGFLOW_CONFIG_DIR}
_46
- LANGFLOW_HOST=${LANGFLOW_HOST}
_46
- PORT=7860
_46
volumes:
_46
- langflow-data-1:/app/langflow
_46
_46
langflow-2:
_46
image: langflowai/langflow:latest
_46
pull_policy: always
_46
ports:
_46
- "${LANGFLOW_PORT_2}:7860"
_46
depends_on:
_46
- postgres
_46
environment:
_46
- LANGFLOW_DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
_46
- LANGFLOW_CONFIG_DIR=${LANGFLOW_CONFIG_DIR}
_46
- LANGFLOW_HOST=${LANGFLOW_HOST}
_46
- PORT=7860
_46
volumes:
_46
- langflow-data-2:/app/langflow
_46
_46
volumes:
_46
langflow-postgres:
_46
langflow-data-1:
_46
langflow-data-2:

  1. Deploy the file with docker-compose up. You can access the first Langflow instance at http://localhost:7860, and the second Langflow instance at http://localhost:7861.

  2. To confirm both instances are using the same database, run the docker exec command to start psql in your PostgreSQL container. Your container name may vary.


_10
docker exec -it docker-test-postgres-1 psql -U langflow -d langflow

  1. Query the database for active connections:

_10
langflow=# SELECT * FROM pg_stat_activity WHERE datname = 'langflow';

  1. Examine the query results for multiple connections with different client_addr values, for example 172.21.0.3 and 172.21.0.4.

Since each Langflow instance runs in its own container on the Docker network, using different incoming IP addresses confirms that both instances are actively connected to the PostgreSQL database.

  1. To quit psql, type quit.
Search