Skip to content

Configuration

Melange supports configuration via YAML files, environment variables, and command-line flags with the following precedence (highest to lowest):

  1. Command-line flags
  2. Environment variables (MELANGE_* prefix)
  3. Configuration file (melange.yaml or melange.yml)
  4. Built-in defaults

A selected environment profile overlays the base configuration before this precedence is applied, so an explicit flag still wins over the environment’s value.

Configuration File

Melange automatically discovers a configuration file by checking each directory for the following names (in order):

  1. melange.yaml
  2. melange.yml
  3. melange/config.yaml
  4. melange/config.yml
  5. melange/melange.yaml
  6. melange/melange.yml

The search starts in the current directory and walks up parent directories until a .git directory is found (repository boundary) or 25 levels are reached. The first match wins.

The melange/ directory convention is the default when you run melange init. Existing melange.yaml files at the project root continue to work. Both layouts are fully supported.

You can override auto-discovery with --config:

melange --config /path/to/custom-config.yaml migrate

Relative paths

Relative paths in a discovered config file (schema, generate.client.output, generate.migration.output, and an environment’s schema) are resolved against the directory the config was found from: the directory holding melange.yaml, or the one holding melange/ for melange/config.yaml. Commands therefore read and write the same files from any subdirectory of the project.

Paths in a config passed with --config, and values from flags or MELANGE_* environment variables, are relative to the current directory. Absolute paths are used as given.

File Format

Create a melange.yaml in your project root, or use melange init to generate one:

# Path to OpenFGA schema file (used by all commands)
schema: schemas/schema.fga

# Database connection (used by migrate, status, diff, history, doctor, functions,
# schema pull, explain, expand, studio, and generate client --check)
# See "Environments" below for per-environment overrides selected with --env.
database:
  # Option 1: Full connection URL
  url: postgres://user:password@localhost:5432/mydb?sslmode=prefer

  # Option 2: Discrete fields (used when url is empty)
  # host: localhost
  # port: 5432
  # name: mydb
  # user: melange
  # password: secret
  # sslmode: prefer

  # Optional: install melange objects in a specific PostgreSQL schema
  # schema: authz

# Code generation settings
generate:
  client:
    runtime: go
    output: internal/authz
    package: authz
    id_type: string
    # Relations to generate constants for; leave out to inherit sql.relations
    relations:
      include: ["can_*"]

  # Migration file generation settings (for external frameworks)
  migration:
    output: db/migrations
    name: melange
    format: split           # "split" or "single"

# Migration settings
migrate:
  dry_run: false
  force: false

# Doctor command settings
doctor:
  verbose: false
  skip_performance: false
  skip_client: false      # skip the Generated Client checks
  strict_client: false    # fail, not warn, on a client name the database lacks

# Generated SQL API (used by migrate, generate migration, generate client, status,
# doctor, functions, studio)
# Omit the section to generate everything. See "SQL API Settings" below.
sql:
  relations:
    include: ["document#can_*", "folder#can_view"]
    exclude: ["document#can_admin"]
  functions:
    list_objects: true
    list_subjects: true
    accessible: true
    parent_key: true
    expand: false
    explain: false

Minimal Configuration

A minimal configuration for most projects:

schema: schemas/schema.fga

database:
  url: postgres://localhost/mydb

generate:
  client:
    runtime: go
    output: internal/authz

Configuration Reference

Top-Level Settings

KeyTypeDefaultDescription
schemastringschemas/schema.fgaPath to the OpenFGA schema file

Database Settings

Configure under database::

KeyTypeDefaultDescription
urlstring-Full PostgreSQL connection URL
hoststring-Database host (used when url is empty)
portint5432Database port
namestring-Database name
userstring-Database user
passwordstring-Database password
sslmodestringpreferSSL mode for connection
schemastring-PostgreSQL schema for melange objects (see Custom Database Schema)

Connection URL format:

postgres://user:password@host:5432/dbname?sslmode=require

Required fields when using discrete configuration:

  • host
  • name
  • user

SSL Mode Values:

ValueDescription
disableNo SSL
allowTry non-SSL first, then SSL
preferTry SSL first, then non-SSL (default)
requireRequire SSL
verify-caRequire SSL and verify CA
verify-fullRequire SSL and verify CA and hostname

Generate Client Settings

Configure under generate.client::

KeyTypeDefaultDescription
runtimestring-Target runtime: go, typescript
outputstring-Output directory for generated code; generate client --check and doctor compare the files here
packagestringauthzPackage/module name
relations.includelist[]Patterns of the relations to generate constants for, as in sql.relations
relations.excludelist[]Patterns of the relations to leave out
filterstring-Deprecated: relation name prefix applied after the patterns (e.g., can_). Use relations.include: ["can_*"]
id_typestringstringID type for constructors

The client is generated from the top-level schema, or from --schema when given.

Without a relations.include, the client inherits sql.relations (include and exclude), so its constants match the relations melange migrate exposes, and a client exclude only narrows that. A client include replaces sql.relations. include: ["*"] selects every relation. The generated accessible function names always follow what a migration with the sql: section installs: the client names the accessible functions of the exposed relations it selects, accessible_{type}_{relation} when sql.functions.accessible is on and the _by_{link} forms when sql.functions.parent_key is on. generate.client.relations cannot be set to a value such as inherit; leave it out instead. A key under it other than include and exclude stops every command when the configuration is loaded. See Generated Code.

Generate Migration Settings

Configure under generate.migration::

KeyTypeDefaultDescription
outputstring"" (stdout)Output directory for migration files
namestringmelangeMigration name suffix in filenames
formatstringsplitOutput format: split or single
Do not configure both generate.migration.output and use melange migrate against the same database. The two strategies track state differently and mixing them causes warnings. See Running Migrations for guidance.

Migrate Settings

Configure under migrate::

KeyTypeDefaultDescription
dry_runboolfalseOutput SQL without applying
forceboolfalseRe-apply even if unchanged, and drop removed functions with CASCADE together with the objects that depend on them (see Dependent objects)

Doctor Settings

Configure under doctor::

KeyTypeDefaultDescription
verboseboolfalseShow detailed output
skip_performanceboolfalseSkip performance checks (view analysis)
skip_clientboolfalseSkip the Generated Client checks
strict_clientboolfalseReport a name the generated client uses that the database does not answer for as a failure instead of a warning

The Generated Client checks run when generate.client.runtime is set. They read generate.client.* and the sql: section from the configuration only, never from command flags; see doctor.

SQL API Settings

The sql: section selects the generated SQL API: which relations the public functions (check_permission, list_accessible_objects and the others) answer for, and which function families are generated. By default every relation is exposed and every family is generated. Restricting the API keeps the installed function count down: the functions of relations that no exposed relation reaches, and of the families turned off, are not generated. A relation an exposed relation reads through keeps its functions, so that the exposed one resolves completely. See Exposure for what is installed and how unexposed relations behave.

KeyTypeDefaultDescription
relations.includelist["*"]Patterns of the relations to expose. An empty list matches every relation
relations.excludelist[]Patterns of the relations not to expose. Exclude wins over include
functions.list_objectsbooltrueEnable the list_objects family: list_accessible_objects
functions.list_subjectsbooltrueEnable the list_subjects family: list_accessible_subjects and the list_<type>_<relation>_sub functions
functions.accessiblebooltrueEnable the accessible family: accessible_objects, and the accessible_<type>_<relation> names in generated clients
functions.parent_keybooltrueEnable the parent_key family: the accessible_<type>_<relation>_by_<link> functions, and their names in generated clients
functions.expandbooltrueEnable the expand family: expand_permission and the expand_<type>_<relation> functions
functions.explainbooltrueEnable the explain family: explain_permission and the explain_<type>_<relation> functions

Check functions (check_permission, check_permission_nw, check_permission_bulk) are always generated. A key the section leaves out keeps its default. A key the section does not know, such as a misspelled functions.expnd, or functions.check, which cannot be turned off, stops every command when the configuration is loaded: ignoring it would keep the default for the key that was meant.

With a family off, its public function raises M2003 for every relation the model defines. Explain and expand are debugging tools, so expand: false and explain: false cost the CLI and studio nothing but time: melange explain, melange expand and studio compile those functions from the recorded model into pg_temp for each call, which needs a writable primary and the TEMP privilege (see Explain and Expand on Demand). list_objects, accessible and parent_key read each other, so the list_<type>_<relation>_obj and accessible_<type>_<relation> functions stay installed while any of the three is on: turning off list_objects or accessible alone removes no per-relation functions, and only all three off removes them.

Patterns. A pattern is relation, which matches that relation on every type, or type#relation. * matches any run of characters and ? one character, within one part: neither crosses #. can_* matches every relation starting with can_; document#* matches every relation of document. A pattern may not be empty, have more than one #, an empty part, or contain whitespace, : or @. An invalid pattern stops every command when the configuration is loaded, with an error naming the key (and the environment variable, when the value came from one). A valid pattern that matches no relation of the schema is a warning from melange migrate and melange doctor, not an error.

Declarative. The section describes the API the database should have. melange migrate always applies the exposure it resolves to (config file, then MELANGE_SQL_* variables, then flags, over the defaults), and records it in melange_migrations. Removing the section, or a key of it, widens the database back to the default on the next migrate. The command-line flags (--include-relation, --exclude-relation, --no-<family>) apply to one run only and are not remembered, so keep a lasting restriction in the config file. melange generate client reads the section too, without those flags, so a restriction kept in the file also gives the client the matching names. melange status and melange doctor compare the local exposure with the recorded one and report a difference as exposure drift.

Only the CLI is declarative. A Go program that calls migrator.Migrate or migrator.MigrateWithOptions without an Exposure keeps the exposure the database last recorded, so an application that migrates on startup does not undo a restriction applied with melange migrate. Migrator.MigrateWithTypes records no migration, so it cannot change the exposure at all. See Programmatic Alternative.

The sql: section applies to every environment: the exposure describes the schema, not a connection. An sql: key inside an environment profile is rejected when the configuration is loaded.

Custom Database Schema

By default, melange installs all its objects into the connection’s current schema (usually public). The database.schema option places them in a specific PostgreSQL schema instead.

Objects affected by this setting:

  • Generated check functions (check_permission, check_document_viewer, etc.)
  • Generated list functions (list_accessible_objects, list_accessible_subjects, etc.)
  • The melange_migrations tracking table
  • The melange_tuples view (you create this yourself, but it should be in the target schema)

Your application tables are not affected. They remain wherever you have them. The tuples view in the target schema can query tables in any other schema.

Prerequisites

You must create the schema before running melange migrate:

CREATE SCHEMA IF NOT EXISTS authz;

The schema must be on your connection’s search_path for permission checks to work at runtime. Most PostgreSQL configurations include public by default, but a custom schema needs to be added:

ALTER ROLE myuser SET search_path TO authz, public;

Or set it per-connection in your application’s connection string or pool configuration.

Configuration

database:
  url: postgres://localhost/mydb
  schema: authz

Or via environment variable:

export MELANGE_DATABASE_SCHEMA=authz

Or via CLI flag:

melange migrate --db postgres://localhost/mydb --db-schema authz

When --db-schema is not passed, every database command resolves it from database.schema in the config (or the selected environment), falling back to public.

Tuples view setup

Create your melange_tuples view in the target schema. The view itself lives in the custom schema but can reference tables in any schema:

CREATE OR REPLACE VIEW authz.melange_tuples AS
SELECT
    'user' AS subject_type,
    user_id::text AS subject_id,
    role AS relation,
    'organization' AS object_type,
    organization_id::text AS object_id
FROM public.organization_members;

Runtime client configuration

When using a custom schema, configure your runtime client to match:

Go:

checker := melange.NewChecker(db, melange.WithDatabaseSchema("authz"))

TypeScript:

const checker = new Checker({ db, databaseSchema: 'authz' });

Environments

Named environment profiles let a single --env flag point a command at a specific database — local, staging, production — instead of hand-swapping --db connection strings. Each profile overlays the base configuration.

schema: melange/schema.fga

# Base connection — used when no environment is selected, and the fallback
# for any field an environment leaves unset.
database:
  url: postgres://localhost:5432/mydb_dev

default_environment: local        # optional; used when --env / MELANGE_ENV absent

environments:
  local:
    database:
      url: postgres://localhost:5432/mydb_dev
  staging:
    database:
      host: staging.db.internal
      name: app
      user: melange
      password: ${STAGING_DB_PASSWORD}   # expanded from the OS environment
      schema: public
  production:
    database:
      url: ${PROD_DATABASE_URL}

Then target an environment on any database command:

melange status --env production
melange migrate --env staging
melange schema pull --env production -o recovered.fga

Selecting an environment

The active environment is resolved with this precedence (highest first):

  1. --env <name> flag
  2. MELANGE_ENV environment variable
  3. default_environment config key
  4. none → the base database block (behaviour identical to configs without an environments: map)

An explicitly selected environment (--env or MELANGE_ENV) that isn’t defined is a hard error, so a typo can’t silently run against the wrong database. A default_environment that names a missing environment is treated leniently: a warning is printed and the base config is used, so a stale default never blocks diagnostic commands.

Overlay semantics

An environment’s database block overlays the base one — fields it sets win, fields it omits fall through. An environment can set database and schema only; the sql: section is the same for every environment, and an environment that sets it is rejected. To keep behaviour predictable, when an environment supplies a discrete connection (host/name) on top of a base that uses a url, the base URL is dropped rather than decomposed. In that case the environment must provide a complete connection (including user); melange does not split a base URL into its parts.

Secrets with ${VAR}

Environment values may reference OS environment variables with ${VAR} syntax, so production credentials stay out of the committed config file:

environments:
  production:
    database:
      url: ${PROD_DATABASE_URL}

Only the braced ${VAR} form is expanded; a literal $ (e.g. in a password) is left untouched. An unset variable referenced by a database field is reported when a command actually connects — not at startup — so diagnostic commands like melange env list, melange config show, and melange validate still run even if a secret is missing from the current shell.

Inspecting environments

melange env list prints the defined profiles and their targets (with the active one marked *); ${VAR} references are shown literally and passwords redacted:

$ melange env list
Environments:
* local            postgres://localhost:5432/mydb_dev
  production       ${PROD_DATABASE_URL}
  staging          staging.db.internal:5432/app

Default: local
Active:  local (marked with *)

melange config show --env production prints the fully-resolved configuration for a profile, with passwords masked by default (pass --reveal-secrets to see them). See the CLI reference.

Environment Variables

All configuration options can be set via environment variables with the MELANGE_ prefix. Use underscores to separate nested keys:

Environment VariableConfiguration Path
MELANGE_SCHEMAschema
MELANGE_DATABASE_URLdatabase.url
MELANGE_DATABASE_HOSTdatabase.host
MELANGE_DATABASE_PORTdatabase.port
MELANGE_DATABASE_NAMEdatabase.name
MELANGE_DATABASE_USERdatabase.user
MELANGE_DATABASE_PASSWORDdatabase.password
MELANGE_DATABASE_SSLMODEdatabase.sslmode
MELANGE_DATABASE_SCHEMAdatabase.schema
MELANGE_GENERATE_CLIENT_RUNTIMEgenerate.client.runtime
MELANGE_GENERATE_CLIENT_OUTPUTgenerate.client.output
MELANGE_GENERATE_CLIENT_PACKAGEgenerate.client.package
MELANGE_GENERATE_CLIENT_RELATIONS_INCLUDEgenerate.client.relations.include
MELANGE_GENERATE_CLIENT_RELATIONS_EXCLUDEgenerate.client.relations.exclude
MELANGE_GENERATE_CLIENT_FILTERgenerate.client.filter (deprecated)
MELANGE_GENERATE_CLIENT_ID_TYPEgenerate.client.id_type
MELANGE_GENERATE_MIGRATION_OUTPUTgenerate.migration.output
MELANGE_GENERATE_MIGRATION_NAMEgenerate.migration.name
MELANGE_GENERATE_MIGRATION_FORMATgenerate.migration.format
MELANGE_MIGRATE_DRY_RUNmigrate.dry_run
MELANGE_MIGRATE_FORCEmigrate.force
MELANGE_DOCTOR_VERBOSEdoctor.verbose
MELANGE_DOCTOR_SKIP_PERFORMANCEdoctor.skip_performance
MELANGE_DOCTOR_SKIP_CLIENTdoctor.skip_client
MELANGE_DOCTOR_STRICT_CLIENTdoctor.strict_client
MELANGE_SQL_RELATIONS_INCLUDEsql.relations.include
MELANGE_SQL_RELATIONS_EXCLUDEsql.relations.exclude
MELANGE_SQL_FUNCTIONS_LIST_OBJECTSsql.functions.list_objects
MELANGE_SQL_FUNCTIONS_LIST_SUBJECTSsql.functions.list_subjects
MELANGE_SQL_FUNCTIONS_ACCESSIBLEsql.functions.accessible
MELANGE_SQL_FUNCTIONS_PARENT_KEYsql.functions.parent_key
MELANGE_SQL_FUNCTIONS_EXPANDsql.functions.expand
MELANGE_SQL_FUNCTIONS_EXPLAINsql.functions.explain
MELANGE_ENVselects an environment profile (like --env)
CI(special)

Setting CI to any value disables the automatic update check. Most CI providers set this automatically.

Example:

export MELANGE_DATABASE_URL="postgres://prod-user:secret@prod-db:5432/myapp"
export MELANGE_GENERATE_CLIENT_RUNTIME="go"
melange migrate

Boolean values: Use true/false or 1/0 for boolean environment variables.

List values: Separate the entries of a list variable with commas; spaces around the commas are ignored. A variable replaces the whole list from the config file:

export MELANGE_SQL_RELATIONS_INCLUDE="document#can_*, folder#can_view"

An empty variable is ignored, so the config file value (or the default) applies.

Configuration Inheritance

An environment profile can override the top-level schema:

schema: schemas/schema.fga

environments:
  legacy:
    schema: schemas/legacy.fga

No other section has its own schema path: generate client, generate migration and the database commands read the top-level schema (or --schema).

Viewing Effective Configuration

Use melange config show to see the effective configuration after merging all sources (and any selected environment). Passwords are masked by default; pass --reveal-secrets to print them.

# Show effective configuration
melange config show

# Show with config file path and active environment
melange config show --source

# Show the resolved production profile
melange config show --env production

Output:

Config file: /path/to/project/melange.yaml
Environment: production

Database:
  Host: ""
  Name: ""
  Password: ""
  Port: 5432
  SSLMode: prefer
  Schema: ""
  URL: postgres://prod-user:****@prod-db:5432/app
  User: ""
...
Schema: schemas/schema.fga

The keys are the Go field names (Database, URL, SSLMode, SQL), sorted alphabetically, not the melange.yaml keys.

Security Considerations

  • Prefer environment variables for secrets in production environments — reference them from an environment profile with ${VAR} (e.g. url: ${PROD_DATABASE_URL}) so the config file stays credential-free
  • Avoid committing melange.yaml files containing database.password to version control
  • Consider using connection URLs with credentials stored in secure secret management systems
  • For local development, discrete fields in a .gitignored config file are acceptable
  • melange config show masks passwords by default; --reveal-secrets is opt-in

Example Configurations

Local Development

schema: schemas/schema.fga

database:
  url: postgres://localhost/myapp_dev

generate:
  client:
    runtime: go
    output: internal/authz
    package: authz

CI/CD Pipeline

Use environment variables for credentials:

# .github/workflows/deploy.yml
env:
  MELANGE_DATABASE_URL: ${{ secrets.DATABASE_URL }}

With a minimal config file:

schema: schemas/schema.fga

generate:
  client:
    runtime: go
    output: internal/authz

Multi-Environment

Define environment profiles in one config and select them with --env:

# melange.yaml
schema: schemas/schema.fga

database:
  url: postgres://localhost:5432/myapp_dev   # base / default

default_environment: local

environments:
  local:
    database:
      url: postgres://localhost:5432/myapp_dev
  production:
    database:
      url: ${PROD_DATABASE_URL}   # from CI secrets, never committed
melange status --env production
melange migrate --env production

Alternatively, use separate config files (melange --config melange.prod.yaml migrate) or override the connection with MELANGE_DATABASE_URL per invocation.