Configuration

pgfence reads configuration from .pgfence.toml or .pgfence.json, found by walking up from the current directory to the repository root. CLI flags override the file when both are present.

Config File

Precedence, highest first: command-line flags, the config file (with any extends bases folded in), then built-in defaults. See Config discovery and extends for how the file is found.

toml
# .pgfence.toml
format = "auto"
output = "cli"
min-pg-version = 14
max-risk = "high"
require-lock-timeout = true
require-statement-timeout = true
max-lock-timeout = 5000
max-statement-timeout = 600000
disable-rules = ["rename-column"]
plugins = ["./rules/team-rules.mjs"]
telemetry = false
exclude = ["migrations/legacy/**"]
baseline = "pgfence-baseline.json"

[rules]
create-index-not-concurrent = "high"
identifier-too-long = "off"

The same config can be expressed in JSON:

json
{
  "format": "auto",
  "output": "cli",
  "min-pg-version": 14,
  "max-risk": "high",
  "require-lock-timeout": true,
  "require-statement-timeout": true,
  "max-lock-timeout": 5000,
  "max-statement-timeout": 600000,
  "disable-rules": ["rename-column"],
  "plugins": ["./rules/team-rules.mjs"],
  "telemetry": false,
  "exclude": ["migrations/legacy/**"],
  "baseline": "pgfence-baseline.json",
  "rules": {
    "create-index-not-concurrent": "high",
    "identifier-too-long": "off"
  }
}

Options

OptionDefaultDescription
formatautoMigration format: sql, typeorm, prisma, knex, drizzle, sequelize, kysely, or auto
outputcliReporter output: cli, json, github, sarif, or gitlab
db-urlDatabase URL for size-aware scoring
stats-filePath to a JSON stats snapshot as an alternative to db-url
min-pg-version14Minimum PostgreSQL version to assume for version-sensitive checks
max-riskhighRisk threshold for --ci
require-lock-timeouttrueRequire SET lock_timeout in every migration
require-statement-timeouttrueRequire SET statement_timeout in every migration
max-lock-timeout5000Warn when lock_timeout exceeds this many milliseconds
max-statement-timeout600000Warn when statement_timeout exceeds this many milliseconds
disable-rulesSuppress specific rule IDs
enable-rulesWhitelist only these rule IDs
snapshotSchema snapshot JSON for definitive type-change analysis
pluginsPlugin file paths, resolved from the current working directory
prisma-versionPrisma version of the project, such as "7.3.0" or "^7.3.0". Prisma before 7.4 runs each migration.sql in one transaction. Same as --prisma-version
assume-in-transactionfalseAssume the migration runner wraps each migration in a transaction. Same as --assume-in-transaction
excludeGlob patterns of migration files to skip, relative to the config file's directory. See Exclude
rulesPer-rule severity overrides: rule ID to off or a risk level. See Per-rule severity
baselinePath to a baseline file written by --write-baseline. See Baselines
extendsRelative path, or array of relative paths, of config files this one builds on. See Config discovery and extends
telemetrytrueSet to false to disable anonymous CLI usage counts for the repository

Config discovery and extends

pgfence uses the nearest .pgfence.toml or .pgfence.json found by walking up from the current directory to the repository root (the directory holding .git). In a directory with both files, the TOML file wins. Without a repository root only the current directory is searched, so a stray file in a parent directory such as your home directory is never picked up. Only one file is the config file: files further up the tree are not merged in.

Pass --config <path> to name a .toml or .json file explicitly. Nothing is discovered in that case, and a missing file is an error rather than a silent fall back to defaults.

To share settings between files, use extends with a relative path or an array of relative paths:

toml
# services/api/.pgfence.toml
extends = ["../../.pgfence.base.toml"]
max-risk = "medium"

[rules]
identifier-too-long = "off"
  • A scalar key set in the child replaces the base's value.
  • An array key (exclude, disable-rules, enable-rules, plugins) set in the child replaces the base's array. It is not concatenated, so a child that wants the base's entries repeats them.
  • The rules table is merged per rule ID, with the child winning, so a child can change one rule without restating the base.
  • With an array, later entries override earlier ones.
  • Targets must be relative paths inside the repository. Absolute paths, URLs and package names are rejected, as are cycles and chains deeper than 8 files.
  • Path-valued keys (snapshot, stats-file, baseline) are written relative to the file that declares them.

Per-rule severity

The rules table maps a rule ID to off or one of safe, low, medium, high, critical. In TOML it is a [rules] table, in JSON a "rules" object (see the examples above).

  • For a statement check, the level replaces the check's base risk. Size-aware scoring still applies on top of the overridden level.
  • For a policy check, safe, low and medium make it a warning, and high and critical make it an error. Errors are what fail a run.
  • off removes the rule's findings, like disable-rules.
  • Rule IDs must match exactly. There are no wildcards or prefixes.
  • An unknown rule ID is an error, with a suggestion for the closest known ID. Plugin rules are written as plugin:<id> and checked once the plugins are loaded.
  • Every active override is echoed in the report, so a lowered or disabled rule is never silent. In SARIF the overrides are recorded in the run's properties.governance.

Rule IDs are listed with each rule on the lock safety, data types and policy pages, and appear as ruleId in JSON output.

Exclude

exclude is a list of glob patterns for migration files to skip, matched against the files you pass on the command line and relative to the directory of the config file.

toml
exclude = ["migrations/legacy/**", "**/*.generated.sql", "baseline.sql"]
  • * matches any run of characters except /, ** matches across directories, ? matches one character, [abc] is a character class ([!abc] negates), and {a,b} is alternation.
  • A pattern with no / matches at any depth. A pattern ending in / matches a directory and everything under it.
  • Matching runs in linear time, so no pattern can stall a run. Patterns longer than 512 characters, or with more than 64 brace alternatives, are rejected.
  • Excluded files are listed in the report. If every file you pass is excluded, pgfence says so on stderr and exits 0.
  • trace does not apply exclude and says so.

Baselines

A baseline lets you adopt pgfence on a codebase that already has findings, and fail only on new ones.

bash
# 1. Record every current finding, from a full run. Exits 0, no CI gating.
pgfence analyze --write-baseline pgfence-baseline.json migrations/*.sql

# 2. Commit the file, then report only findings that are not in it.
pgfence analyze --ci --baseline pgfence-baseline.json migrations/*.sql

Set baseline = "pgfence-baseline.json" in config to apply it on every run. --baseline overrides the config key.

  • Entries are identified by the same stable fingerprint that JSON, SARIF and GitLab reports carry: a hash of the rule, the file, the normalized statement text and an occurrence index. A finding survives line moves and reformatting, but a changed statement is a new finding.
  • A baseline covers statement findings and policy violations. It never covers statements pgfence could not analyze: those stay UNKNOWN in every report and in the coverage line.
  • Suppressed findings are counted in the report, and baseline entries that no longer match anything are listed as stale so you can prune the file.
  • --write-baseline and --baseline cannot be combined, and --write-baseline cannot be combined with --changed-since. Write the baseline from a full run.
  • trace does not apply baseline and says so.

Telemetry Configuration

telemetry = false is the one configuration key that pgfence also searches for in parent directories up to the repository root. This lets a monorepo opt out once even when commands run from a nested package. See the telemetry reference for every collected field, precedence rules, and the other opt-out methods.

PostgreSQL Version

pgfence parses SQL with the PostgreSQL 18 grammar (libpg-query 18) and is version-aware for older releases. The default minimum is PG 14. Set --min-pg-version or the config-file equivalent to override if needed:

bash
pgfence analyze --min-pg-version 12 migrations/*.sql

Version-sensitive behavior:

PatternPG 11+PG 12+PG 14+
ADD COLUMN ... DEFAULT <constant>Metadata-only (LOW)Metadata-only (LOW)Metadata-only (LOW)
ALTER TYPE ... ADD VALUEEXCLUSIVE, cannot run in a transaction (MEDIUM)EXCLUSIVE, instant (LOW)EXCLUSIVE, instant (LOW)
RENAME COLUMNACCESS EXCLUSIVEACCESS EXCLUSIVEInstant (LOW)
DETACH PARTITION CONCURRENTLYNot availableNot availableSHARE UPDATE EXCLUSIVE (LOW)
REINDEX CONCURRENTLYNot availableAvailableAvailable

Inline Ignore

Add an inline comment immediately before a statement to suppress checks for it. The directive applies only to the single statement that follows. A trailing -- pgfence-ignore after a semicolon applies to the statement on its own line, not to the next one.

Suppress all checks

sql
-- pgfence-ignore
DROP TABLE old_sessions;

Use this to explicitly acknowledge a statement that pgfence cannot fully analyze, such as dynamic SQL, or when you have reviewed the risk and accepted it.

Suppress specific rule

sql
-- pgfence-ignore: drop-table
DROP TABLE old_logs;

-- pgfence-ignore: add-column-not-null-no-default
ALTER TABLE audit_log ADD COLUMN meta JSONB NOT NULL DEFAULT '{}';

Suppress multiple rules

sql
-- pgfence-ignore: drop-table, prefer-robust-drop-table
DROP TABLE old_queue;

Reason and expiry

Any ignore can end with a reason and an expiry date, so a suppression documents itself and does not outlive the problem it covers.

sql
-- pgfence-ignore: drop-column -- reason: backfilled already, expires: 2027-01-31
ALTER TABLE accounts DROP COLUMN legacy_flag;

-- pgfence-ignore: drop-table -- legacy table, going away
DROP TABLE old_logs;
  • Text after -- is the reason. reason: and expires: keys are also accepted without the --.
  • expires: YYYY-MM-DD suppresses through the end of that day (UTC) and stops the day after.
  • An expired ignore stops suppressing, and the report says which findings are reported again. An expiry that is not a real YYYY-MM-DD date also stops it from suppressing, rather than reading as "never expires".
  • An ignore that suppresses nothing is reported as unused, so a typo in a rule ID does not stay hidden. An ignore for a rule that is switched off is not reported.
  • Every suppressed finding is counted in the report, and appears under results[].suppressed in JSON with its source, reason and expiry.

Ignore a whole file

pgfence-ignore-file suppresses the named rules, or every rule, for the whole file. It works in the CLI and in the editor, and on its own line.

sql
-- pgfence-ignore-file: add-column-not-null-no-default -- reason: generated by the ORM, expires: 2027-01-31

In ORM migration source files, // pgfence-ignore-file is accepted as well. With no rule list it covers every rule.

Syntax reference

SyntaxEffect
-- pgfence-ignoreSuppress all checks for the next statement
-- pgfence-ignore: <ruleId>Suppress one specific rule
-- pgfence-ignore: <ruleId>, <ruleId>Suppress multiple rules (comma-separated)
-- pgfence-ignore: <ruleId> -- reason: <text>, expires: YYYY-MM-DDSuppress with a documented reason and an expiry
-- pgfence-ignore-fileSuppress all checks for the whole file
-- pgfence-ignore-file: <ruleId>, <ruleId>Suppress the named rules for the whole file. Also accepts a reason and expiry
-- pgfence: ignore <ruleId>Legacy syntax, still supported
Use --output json to see the ruleId for any check you want to suppress.