Skip to content

feat: support PostgreSQL connection URL from environment - #850

Merged
chenyme merged 2 commits into
mainfrom
sql
Aug 4, 2026
Merged

feat: support PostgreSQL connection URL from environment#850
chenyme merged 2 commits into
mainfrom
sql

Conversation

@chenyme

@chenyme chenyme commented Aug 4, 2026

Copy link
Copy Markdown
Owner

What changed

  • adds support for injecting the PostgreSQL connection URL through GROK2API_DATABASE_URL
  • automatically selects the PostgreSQL driver when the environment variable is non-empty
  • preserves existing SQLite and PostgreSQL YAML behavior when the variable is unset or empty
  • accepts postgres:// and postgresql:// URLs
  • rejects SQLAlchemy postgresql+asyncpg:// URLs with an actionable migration message
  • redacts PostgreSQL credentials from configuration, connection, and health-check errors
  • preserves the original Go error chain through Unwrap, allowing existing errors.Is and errors.As checks to continue working
  • exposes the optional variable through the built-in Docker Compose configuration
  • documents environment-based PostgreSQL configuration in the English, Chinese, backend, and example configuration documentation

Configuration priority

The startup configuration priority is:

built-in defaults
    < config.yaml
    < GROK2API_DATABASE_URL
    < CLI overrides

The current CLI does not provide a database override, so GROK2API_DATABASE_URL is currently the highest-priority database setting.

A non-empty environment value overrides database.postgres.dsn and automatically sets:

database:
  driver: postgres

An unset, empty, or whitespace-only value does not override YAML.

Usage

Docker Compose:

GROK2API_DATABASE_URL='postgresql://user:password@host:5432/grok2api?sslmode=require' \
docker compose up -d

Kubernetes and PaaS deployments can inject the same variable through their Secret management facilities.

Platforms that only provide DATABASE_URL can map it explicitly:

environment:
  GROK2API_DATABASE_URL: "${DATABASE_URL}"

The application intentionally does not read the generic DATABASE_URL implicitly, avoiding collisions with unrelated services or sidecars.

Compatibility

  • no API changes
  • no database schema or migration changes
  • existing SQLite deployments are unchanged
  • existing YAML-based PostgreSQL deployments are unchanged
  • PostgreSQL keyword/value DSNs remain supported in YAML
  • URL-only validation applies only to GROK2API_DATABASE_URL
  • connection-pool settings continue to come from YAML or built-in defaults
  • the environment variable is read at startup and requires a restart to change

Security

  • invalid environment URLs never include the original URL or password in returned errors
  • PostgreSQL driver parse, connection, and ping errors are sanitized
  • URL and keyword/value password forms are redacted
  • sanitized errors retain their underlying error chain for programmatic classification

Validation

  • go test ./... in backend
  • targeted go test -race for configuration and relational persistence
  • go vet ./...
  • docker compose config --quiet
  • repeated environment-priority regression tests
  • malformed URL, unsupported scheme, asyncpg migration, empty override, generic DATABASE_URL, and credential-redaction tests
  • git diff --check

@chenyme chenyme linked an issue Aug 4, 2026 that may be closed by this pull request
3 tasks
@chenyme chenyme self-assigned this Aug 4, 2026
@chenyme
chenyme merged commit 4bb20b8 into main Aug 4, 2026
9 checks passed
@FakerL

FakerL commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 支持通过环境变量配置 PostgreSQL 数据库连接

2 participants