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.
# .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:
{
"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
| Option | Default | Description |
|---|---|---|
format | auto | Migration format: sql, typeorm, prisma, knex, drizzle, sequelize, kysely, or auto |
output | cli | Reporter output: cli, json, github, sarif, or gitlab |
db-url | Database URL for size-aware scoring | |
stats-file | Path to a JSON stats snapshot as an alternative to db-url | |
min-pg-version | 14 | Minimum PostgreSQL version to assume for version-sensitive checks |
max-risk | high | Risk threshold for --ci |
require-lock-timeout | true | Require SET lock_timeout in every migration |
require-statement-timeout | true | Require SET statement_timeout in every migration |
max-lock-timeout | 5000 | Warn when lock_timeout exceeds this many milliseconds |
max-statement-timeout | 600000 | Warn when statement_timeout exceeds this many milliseconds |
disable-rules | Suppress specific rule IDs | |
enable-rules | Whitelist only these rule IDs | |
snapshot | Schema snapshot JSON for definitive type-change analysis | |
plugins | Plugin file paths, resolved from the current working directory | |
prisma-version | Prisma 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-transaction | false | Assume the migration runner wraps each migration in a transaction. Same as --assume-in-transaction |
exclude | Glob patterns of migration files to skip, relative to the config file's directory. See Exclude | |
rules | Per-rule severity overrides: rule ID to off or a risk level. See Per-rule severity | |
baseline | Path to a baseline file written by --write-baseline. See Baselines | |
extends | Relative path, or array of relative paths, of config files this one builds on. See Config discovery and extends | |
telemetry | true | Set 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:
# 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
rulestable 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,lowandmediummake it a warning, andhighandcriticalmake it an error. Errors are what fail a run. offremoves the rule's findings, likedisable-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.
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.
tracedoes not applyexcludeand says so.
Baselines
A baseline lets you adopt pgfence on a codebase that already has findings, and fail only on new ones.
# 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
fingerprintthat 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-baselineand--baselinecannot be combined, and--write-baselinecannot be combined with--changed-since. Write the baseline from a full run.tracedoes not applybaselineand 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:
pgfence analyze --min-pg-version 12 migrations/*.sql Version-sensitive behavior:
| Pattern | PG 11+ | PG 12+ | PG 14+ |
|---|---|---|---|
ADD COLUMN ... DEFAULT <constant> | Metadata-only (LOW) | Metadata-only (LOW) | Metadata-only (LOW) |
ALTER TYPE ... ADD VALUE | EXCLUSIVE, cannot run in a transaction (MEDIUM) | EXCLUSIVE, instant (LOW) | EXCLUSIVE, instant (LOW) |
RENAME COLUMN | ACCESS EXCLUSIVE | ACCESS EXCLUSIVE | Instant (LOW) |
DETACH PARTITION CONCURRENTLY | Not available | Not available | SHARE UPDATE EXCLUSIVE (LOW) |
REINDEX CONCURRENTLY | Not available | Available | Available |
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
-- 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
-- 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
-- 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.
-- 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:andexpires:keys are also accepted without the--. expires: YYYY-MM-DDsuppresses 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-DDdate 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[].suppressedin 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.
-- 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
| Syntax | Effect |
|---|---|
-- pgfence-ignore | Suppress 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-DD | Suppress with a documented reason and an expiry |
-- pgfence-ignore-file | Suppress 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 |
--output json to see the ruleId for any check you want to suppress.