Supported Database Drivers for Grok2API Persistence: SQLite and PostgreSQL Explained

Grok2API supports two database drivers for persistence: SQLite via the glebarez/sqlite driver and PostgreSQL via the official GORM driver, with driver selection enforced through configuration validation.

Grok2API's persistence layer relies on GORM (Go Object Relational Mapper) to abstract database operations. As of the latest implementation in the chenyme/grok2api repository, the system explicitly supports only SQLite and PostgreSQL drivers. This design choice balances lightweight local development with production-grade scalability.


Supported Database Drivers

Grok2API implements dedicated initialization functions for each supported driver in backend/internal/infra/persistence/relational/database.go. Configuration validation in backend/internal/infra/config/config.go ensures no unsupported drivers can be instantiated.

SQLite: File-Based Persistence

SQLite serves as the default driver for local development and single-node deployments. Grok2API uses the pure-Go glebarez/sqlite driver, which provides:

  • WAL (Write-Ahead Logging) mode for improved concurrency
  • Foreign key enforcement
  • Configurable busy timeout handling

The OpenSQLite function handles initialization:

ctx := context.Background()
db, err := relational.OpenSQLite(ctx, "./data/grok2api.db")
if err != nil {
    log.Fatalf("failed to open SQLite: %v", err)
}
defer db.Close()

YAML configuration:

database:
  driver: sqlite
  sqlite:
    path: ./data/grok2api.db

PostgreSQL: Production-Ready Connectivity

PostgreSQL provides network-accessible, enterprise-grade persistence. The implementation uses GORM's official PostgreSQL driver with configurable connection pooling.

The OpenPostgres function accepts DSN and pool parameters:

ctx := context.Background()
pgCfg := cfg.Database.Postgres
db, err := relational.OpenPostgres(ctx, pgCfg.DSN, pgCfg.MaxOpenConns, pgCfg.MaxIdleConns)
if err != nil {
    log.Fatalf("failed to open PostgreSQL: %v", err)
}
defer db.Close()

YAML configuration:

database:
  driver: postgres
  postgres:
    dsn: "host=localhost user=grok password=secret dbname=grok2api port=5432 sslmode=disable"
    maxOpenConns: 50
    maxIdleConns: 10

Configuration Validation

The system enforces driver restrictions at startup. In backend/internal/infra/config/config.go, validation logic rejects any driver value other than sqlite or postgres:

if cfg.Database.Driver != "sqlite" && cfg.Database.Driver != "postgres" {
    return errors.New("database.driver 必须是 sqlite 或 postgres")
}

This validation occurs at line 334 of the configuration file, ensuring early failure with a clear error message if an unsupported persistence backend is specified.


Driver Comparison

Driver Use Case Connection Pooling File Location
SQLite Local development, edge deployments N/A (file-based) backend/internal/infra/persistence/relational/database.go
PostgreSQL Production, multi-instance deployments Configurable (maxOpenConns, maxIdleConns) backend/internal/infra/persistence/relational/database.go

Implementation Architecture

Persistence Layer

The relational persistence package centralizes database operations. Both OpenSQLite and OpenPostgres return a GORM *gorm.DB instance, allowing the remainder of the application to remain database-agnostic.

Configuration Structure

The DatabaseConfig struct in backend/internal/infra/config/config.go defines the configuration schema:

  • driver: String enum ("sqlite" | "postgres")
  • sqlite.path: Filesystem location for SQLite database
  • postgres.dsn: PostgreSQL connection string
  • postgres.maxOpenConns: Maximum open connections
  • postgres.maxIdleConns: Maximum idle connections

Summary

  • Grok2API persistence supports exactly two database drivers: SQLite and PostgreSQL.
  • SQLite uses the glebarez/sqlite pure-Go driver with WAL mode enabled.
  • PostgreSQL uses the official GORM driver with configurable connection pools.
  • Configuration validation enforces driver selection at startup in config.go.
  • Implementation files: backend/internal/infra/persistence/relational/database.go contains driver-specific initialization logic.

Frequently Asked Questions

What databases does Grok2API support for persistence?

Grok2API supports SQLite and PostgreSQL exclusively. The configuration validation in backend/internal/infra/config/config.go rejects any other driver value, returning an error requiring either sqlite or postgres.

Can I use MySQL with Grok2API?

No. Grok2API does not implement MySQL driver support. The OpenSQLite and OpenPostgres functions in backend/internal/infra/persistence/relational/database.go are the only database initialization methods available, and the configuration validator explicitly blocks other drivers.

How do I configure SQLite for local development?

Set database.driver: sqlite in your config.yaml and specify the path field under the sqlite key. The system uses glebarez/sqlite with WAL mode and foreign keys enabled automatically.

What connection pool settings are available for PostgreSQL?

PostgreSQL configuration supports maxOpenConns and maxIdleConns parameters. These values are passed directly to the OpenPostgres function in backend/internal/infra/persistence/relational/database.go to configure GORM's underlying connection pool.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →