# Where Is the SQLite Database Stored in TREK? File Path and Configuration Guide

> Discover where TREK stores its SQLite database at server/data/trek.db. Learn how to override the default SQLITE_PATH environment variable for custom configurations.

- Repository: [Maurice/TREK](https://github.com/mauriceboe/TREK)
- Tags: how-to-guide
- Published: 2026-07-03

---

**TREK stores its SQLite database file at `server/data/trek.db` relative to the project root, with the path hardcoded in the `DatabaseService` constructor and overridable via the `SQLITE_PATH` environment variable.**

The open-source TREK application (mauriceboe/TREK) uses a file-based SQLite database to persist all application data. Understanding exactly where this SQLite database is stored is essential for backups, migrations, and containerized deployments. By default, the NestJS-based backend creates the database file in a specific subdirectory of the server folder, though this location can be customized through environment configuration.

## Default Database Location in TREK

The standard SQLite database file resides at **`server/data/trek.db`** within the repository structure. This path is resolved dynamically in the `DatabaseService` using Node.js's `path.join(__dirname, '../../data/trek.db')`, which navigates two levels up from the compiled service location to the server root, then into the `data` directory.

Unlike the in-memory database (`':memory:'`) used in the test suite, this file-based storage ensures data persists across application restarts.

## How the Database Path Is Configured

### Hardcoded Path in DatabaseService

The connection logic lives in **[`server/src/nest/database/database.service.ts`](https://github.com/mauriceboe/TREK/blob/main/server/src/nest/database/database.service.ts)**. When the service instantiates, it initializes `better-sqlite3` with a file path rather than an in-memory database. The constructor builds the absolute path by joining the current directory with the relative segment `../../data/trek.db`.

This implementation ensures that regardless of where the compiled JavaScript runs within the `server` directory hierarchy, it always resolves to the same physical location at the project level.

### Environment Variable Override

You can override the default location by setting the **`SQLITE_PATH`** environment variable before starting the application. When this variable is present, `DatabaseService` uses its value instead of the hardcoded path, allowing you to store the database on separate volumes or outside the project directory.

## Accessing and Configuring the Database

To connect to the default database location, instantiate the `DatabaseService` without additional configuration:

```typescript
import { DatabaseService } from './database.service';

// Connects to server/data/trek.db automatically
const db = new DatabaseService();
const rows = db.prepare('SELECT * FROM trips').all();
console.log(rows);

```

To use a custom path, export the environment variable before initialization:

```bash
export SQLITE_PATH=/var/lib/trek/production.db

```

```typescript
// DatabaseService now uses the custom path
const db = new DatabaseService();   // opens /var/lib/trek/production.db

```

The **[`docker-compose.yml`](https://github.com/mauriceboe/TREK/blob/main/docker-compose.yml)** configuration in the repository root mounts a persistent volume pointing to `server/data/trek.db` to ensure data survives container restarts and recreation.

## Summary

- TREK stores its SQLite database at **`server/data/trek.db`** by default, resolved via `path.join(__dirname, '../../data/trek.db')` in the service constructor.
- The **`DatabaseService`** class in [`server/src/nest/database/database.service.ts`](https://github.com/mauriceboe/TREK/blob/main/server/src/nest/database/database.service.ts) manages the connection and path resolution.
- Set the **`SQLITE_PATH`** environment variable to override the default file location for custom deployments.
- The database file is created automatically on first run if it does not exist.
- Docker deployments should mount volumes to the `server/data` directory to persist data across container lifecycle events.

## Frequently Asked Questions

### What is the default SQLite database path in TREK?

By default, TREK creates the SQLite file at **`server/data/trek.db`** relative to the project root. This path is constructed in the `DatabaseService` constructor using `path.join(__dirname, '../../data/trek.db')`, placing the database two directory levels above the service file location inside the server directory.

### How do I change the SQLite database location in TREK?

Set the **`SQLITE_PATH`** environment variable to an absolute file path before starting the application. The `DatabaseService` checks for this variable during instantiation and uses it instead of the default `../../data/trek.db` resolution, allowing you to store data on external drives or system directories like `/var/lib/trek/`.

### Is the TREK database file created automatically?

Yes, the `better-sqlite3` driver automatically creates the database file at the resolved path if it does not already exist. This occurs during the `DatabaseService` instantiation when the constructor opens the connection, ensuring the application starts without requiring manual file creation.

### How does TREK handle database persistence in Docker?

The **[`docker-compose.yml`](https://github.com/mauriceboe/TREK/blob/main/docker-compose.yml)** file in the repository root mounts a persistent volume to the `server/data` directory. This ensures that `server/data/trek.db` survives container restarts and recreation, preventing data loss when the Docker container is stopped, removed, or updated.