# How to Configure the Graph Database Directory Using Environment Variables in Code-Review-Graph

> Easily configure your Code-Review-Graph database directory using the CRG_GRAPH_DB_PATH environment variable. Store your knowledge graph anywhere you choose.

- Repository: [Tirth Kanani/code-review-graph](https://github.com/tirth8205/code-review-graph)
- Tags: how-to-guide
- Published: 2026-08-08

---

**Set the `CRG_GRAPH_DB_PATH` environment variable to any filesystem path before invoking the Code-Review-Graph library, and it will store its SQLite-backed knowledge graph at that location instead of the default `.crg/graph.db` project subdirectory.**

The Code-Review-Graph library (`tirth8205/code-review-graph`) persists code analysis data in a SQLite file-based database. To support flexible deployment across development, CI/CD, and production environments, you can configure the graph database directory using environment variables rather than relying on hard-coded paths.

## How CRG_GRAPH_DB_PATH Controls Database Location

When the library initializes a connection to its knowledge graph, it checks for the `CRG_GRAPH_DB_PATH` environment variable. If present, the library uses this value as the filesystem path for the SQLite database file. If absent, it falls back to a default location within your project root.

In [`code_review_graph/graph.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/graph.py), the `GraphStore` class constructor accepts a `db_path` argument that determines where the `.db` file resides. The CLI entry point in [`code_review_graph/cli.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/cli.py) automatically reads `CRG_GRAPH_DB_PATH` from the environment and passes it to this constructor, ensuring consistent behavior across command-line and programmatic usage.

## The Configuration Resolution Flow

### Variable Detection and Path Resolution

The configuration process follows a strict resolution hierarchy:

1. **Environment lookup** – The code evaluates `os.environ.get("CRG_GRAPH_DB_PATH")` to retrieve the custom path.
2. **Path instantiation** – The string value passes through `Path()`, which resolves relative paths against the current working directory.
3. **Directory creation** – Inside `GraphStore.__init__` (lines 88‑95 of [`code_review_graph/graph.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/graph.py)), the library calls `self.db_path.parent.mkdir(parents=True, exist_ok=True)` to ensure the parent directory exists.
4. **Connection establishment** – The resolved path opens as the SQLite database file backing the graph.

### Default Behavior

When `CRG_GRAPH_DB_PATH` is unset, the library defaults to `<project-root>/.crg/graph.db`, creating the `.crg` directory if necessary. This ensures out-of-the-box functionality while allowing explicit override for containerized or multi-tenant environments.

## Configuration Examples

### Setting the Variable in Your Shell

For Unix-like systems, export the variable before running the CLI:

```bash
export CRG_GRAPH_DB_PATH=/var/tmp/my_project/graph.db
python -m code_review_graph.cli index ./src

```

This directs all graph operations to `/var/tmp/my_project/graph.db` instead of the local project folder.

### Configuring via the Python API

When embedding the library in Python applications, set the environment variable before instantiating `GraphStore`:

```python
import os
from pathlib import Path
from code_review_graph.graph import GraphStore

os.environ["CRG_GRAPH_DB_PATH"] = str(Path("/tmp/custom_dir/graph.db"))

# The GraphStore automatically uses the path from the env var

with GraphStore(os.getenv("CRG_GRAPH_DB_PATH")) as store:
    # Perform graph operations...

    pass

```

### Overriding with Explicit Paths

To bypass the environment variable entirely, pass an explicit path directly to the constructor:

```python
from code_review_graph.graph import GraphStore

custom_path = "/opt/data/graph.sqlite"
store = GraphStore(custom_path)  # Ignores CRG_GRAPH_DB_PATH

```

This approach prioritizes the explicit argument over any environment configuration.

## Summary

- **Set `CRG_GRAPH_DB_PATH`** to control where the SQLite graph database file resides on your filesystem.
- The library **automatically creates parent directories** using `mkdir(parents=True, exist_ok=True)` if they don't exist.
- **Default location**: `.crg/graph.db` relative to the project root when the environment variable is unset.
- Both the **CLI** ([`code_review_graph/cli.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/cli.py)) and **Python API** ([`code_review_graph/graph.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/graph.py)) respect this variable when initializing `GraphStore`.
- **Explicit path arguments** passed directly to `GraphStore(db_path)` override the environment variable.

## Frequently Asked Questions

### What is the default graph database directory if I don't set the environment variable?

If `CRG_GRAPH_DB_PATH` is not set, the library stores the database at `.crg/graph.db` relative to your project root. The `GraphStore` class creates this directory automatically if it doesn't exist.

### Can I use relative paths with CRG_GRAPH_DB_PATH?

Yes. The library passes the environment variable value through `Path()`, which resolves relative paths against the current working directory. For predictable behavior in production, absolute paths are recommended.

### Does the library create the database directory automatically?

Yes. According to the implementation in [`code_review_graph/graph.py`](https://github.com/tirth8205/code-review-graph/blob/main/code_review_graph/graph.py), the `GraphStore.__init__` method calls `self.db_path.parent.mkdir(parents=True, exist_ok=True)`, ensuring the full directory hierarchy exists before opening the SQLite connection.

### How do I override the environment variable when using the Python API?

Pass an explicit `db_path` argument when constructing `GraphStore`. The constructor accepts a string or `Path` object directly, and this value takes precedence over any environment variable configuration.