PostgreSQL Reference

SchemaTongs for PostgreSQL

The full SchemaTongs reference for PostgreSQL extractions — configuration, ShouldCast flags, orphan detection, script validation, and custom-property preservation across re-extraction.

SchemaTongs — extracting a live database schema into version-controlled files

Cast your live database into version-controlled code with a single command.

Overview

SchemaTongs grips every object in your database — on SQL Server, PostgreSQL, or MySQL — and extracts them into a clean schema package: tables as JSON, programmable objects as SQL scripts, everything organized and ready to commit to source control. Point it at a database, run it, and you have a deployable package that SchemaQuench can quench onto any compatible target.

The same tool, the same package format, three engines. Whatever your team runs, SchemaTongs casts it.

Installation

GitHub Releases — Download the latest release from the SchemaSmith releases page. Unzip and place the executable on your PATH.

Running SchemaTongs

From the directory containing your SchemaTongs.settings.json:

SchemaTongs

With a custom config file:

SchemaTongs --ConfigFile:/etc/schemasmith/extract-staging.json

With a full PostgreSQL connection string override:

SchemaTongs --ConnectionString:"Host=myserver;Port=5432;Database=northwind;Username=tongs;Password=secret;"

SchemaTongs connects to the source database, reads every enabled object type, and writes the results to the configured product path. On first run it creates the full package structure; on subsequent runs it updates existing files in place and preserves any custom metadata you have attached under Extensions.

Schema-only mode

If you just need to regenerate the .json-schemas/*.schema validation files for an existing product — without connecting to a database — use the --WriteSchemasOnly switch:

SchemaTongs --WriteSchemasOnly --Product:Path:./my-product

This reads the Platform from Product.json, regenerates the schema files on the fly from the current engine's C# domain types, and exits. No database connection, no extraction. Useful when you've updated SchemaSmith and want CI validation to match the new engine without re-extracting.

Configuration reference

SchemaTongs reads configuration from multiple sources, merged in precedence order (highest priority last):

  1. Settings fileSchemaTongs.settings.json in the working directory (or the file specified by --ConfigFile)
  2. User secrets (debug builds only)
  3. Environment variables with the SmithySettings_ prefix
  4. Command-line switches (highest precedence)

For the full explanation of configuration loading, environment variable mapping, and CLI switches, see Configuration.

Complete SchemaTongs.settings.json

{
  "Source": {
    "Server": "",
    "Port": "",
    "User": "",
    "Password": "",
    "Database": "",
    "Platform": "PostgreSQL",
    "ConnectionProperties": {}
  },
  "Product": {
    "Path": "",
    "Name": "",
    "CheckConstraintStyle": "ColumnLevel"
  },
  "Template": {
    "Name": ""
  },
  "ShouldCast": {
    "Tables": true,
    "Views": true,
    "Functions": true,
    "Procedures": true,
    "TableTriggers": true,
    "ObjectList": "",
    "ValidateScripts": false,
    "SaveInvalidScripts": true,
    "ScriptDynamicDependencyRemovalForFunctions": false,
    "Schemas": true,
    "DomainTypes": true,
    "EnumTypes": true,
    "CompositeTypes": true,
    "Aggregates": true,
    "Sequences": true,
    "Rules": true,
    "MaterializedViews": true
  },
  "OrphanHandling": {
    "Mode": "Detect"
  },
  "FolderMapping": {
    "Schemas": "Schemas",
    "Functions": "Functions",
    "Views": "Views",
    "Procedures": "Procedures",
    "Triggers": "Triggers"
  }
}

Source connection

KeyTypeDefaultDescription
Source:Platformstring(required)Set to PostgreSQL to select the PostgreSQL extraction adapter.
Source:Serverstring(required)Database server hostname or IP.
Source:Portstring5432TCP port.
Source:Userstring(empty)Login username.
Source:Passwordstring(empty)Login password.
Source:Databasestring(required)Name of the database to extract from.
Source:ConnectionPropertiesobject{}Arbitrary key-value pairs appended to the connection string. Platform-specific keys — see Configuration.

The --ConnectionString switch bypasses all Source settings entirely. When provided, Server, Port, User, Password, Database, and ConnectionProperties are all ignored. Source:Platform is still required so the right adapter can claim the connection.

Product output

KeyTypeDefaultDescription
Product:Pathstring(required)Directory where the schema package is created or updated.
Product:Namestring(directory name)Product name written to Product.json. If blank, defaults to the last segment of Product:Path.
Product:CheckConstraintStylestringColumnLevelControls how check constraints are written when creating a new Product.json. See CheckConstraintStyle.
Template:NamestringSource database nameTemplate name. Creates the template directory under Templates/<Name>/. Defaults to the Source:Database value when not specified.

ShouldCast flags

Control exactly what gets cast from the database. Each object type can be individually enabled or disabled.

FlagDefaultWhat it extracts
TablestrueTable definitions as JSON files
ViewstrueView definitions as SQL scripts
FunctionstrueUser-defined functions
ProcedurestrueStored procedures
TableTriggerstrueTable-level triggers
SchemastrueSchema creation scripts
DomainTypestrueCREATE DOMAIN user-defined types
EnumTypestrueCREATE TYPE ... AS ENUM types
CompositeTypestrueCREATE TYPE ... AS (...) composite types
AggregatestrueUser-defined aggregate functions
SequencestrueSequence objects
RulestrueRules attached to tables and views
MaterializedViewstrueMaterialized view definitions as JSON files
ObjectList(empty)Comma- or semicolon-separated list of specific objects to extract. See ObjectList Filtering.
ValidateScriptsfalseParses each extracted SQL script for validity. See Script Validation.
SaveInvalidScriptstrueWhen ValidateScripts is enabled, saves failing scripts as .sqlerror files.
ScriptDynamicDependencyRemovalForFunctionsfalseGenerates a dynamic dependency-removal preamble for function scripts. See ScriptDynamicDependencyRemovalForFunctions.

Folder mapping

By default, each extracted object type lands in the standard folder for the active platform (see Schema Packages — Folder Structure). The optional FolderMapping section lets you rename those default folders without breaking SchemaQuench's recognition of them.

{
  "FolderMapping": {
    "Schemas": "01_Schemas",
    "Functions": "02_Functions",
    "Views": "03_Views",
    "Procedures": "04_Procedures",
    "Triggers": "05_Triggers"
  }
}

The keys are ScriptObjectType values (e.g., Schemas, Functions, Views, Procedures, Triggers, DomainTypes, EnumTypes, CompositeTypes, Sequences, Rules, Aggregates). The values are the folder names you want to use under each template directory.

A few rules:

  • Fixed folders cannot be remapped. MaterializedViews always lives in Materialized Views/. Tables always live in Tables/.
  • Duplicate folder names are rejected. SchemaTongs validates the mapping at startup; if two object types map to the same folder, you get an error before extraction begins.
  • Unmapped types use the platform default. Anything you leave out of FolderMapping keeps its standard folder name.

This pairs naturally with the ScriptFolders array on Template.json. You can extract using your renamed folder layout and SchemaQuench will still pick up the right slot for each folder.

ObjectList filtering

The ObjectList setting restricts extraction to a specific set of objects. Provide a comma- or semicolon-separated list of object names:

"ShouldCast": {
  "ObjectList": "public.customers,public.v_active_customers,usp_get_orders"
}

Names can be specified with or without a schema prefix. Matching is case-insensitive. When ObjectList is empty (the default), all objects matching the enabled ShouldCast flags are extracted.

When ObjectList is active, orphan detection is automatically disabled — SchemaTongs can't tell which files are genuinely orphaned when only a subset of objects is being extracted.

Orphan detection

Databases change. Objects get dropped or renamed. The old script files linger in your package — unless you tell SchemaTongs what to do about them. Configure orphan behavior with OrphanHandling:Mode:

ModeBehavior
DetectOrphaned files are logged as warnings. No files are modified or deleted. Default.
DetectWithCleanupScriptsOrphaned files are logged. For each orphan in a script folder, a cleanup script containing a DROP statement is generated as _OrphanCleanup_<FolderName>.sql in the log directory. The orphaned files themselves are left in place.
DetectDeleteAndCleanupOrphaned files are deleted from the package. Cleanup DROP scripts are generated in the log directory.

When to use each mode

  • Detect — Safe default. Use during routine re-extraction when you want to review orphans manually before taking action.
  • DetectWithCleanupScripts — Use when you want SchemaTongs to prepare the cleanup work but you want to review the generated scripts and delete orphaned files yourself.
  • DetectDeleteAndCleanup — Use when you trust the extraction to be authoritative and want a fully clean package after each run. Particularly useful in automated pipelines.

How it works

SchemaTongs builds a file index for each extraction folder before extraction begins. As objects are extracted and written to disk, each file is marked as "written." After extraction completes, any indexed file that wasn't written is an orphan. Cleanup scripts from previous runs are archived into numbered SchemaTongs.NNNN backup directories before new ones are generated.

Orphan detection only runs for object types that were fully extracted (ShouldCast flag enabled and no ObjectList filter active).

The core tension

An "orphan" might be a script for an object that was genuinely removed from the database, OR it might be a new object you added to the package that has not been deployed yet. Deleting it automatically would destroy your pending work. The default (Detect) is conservative because it never destroys anything — it just tells you.

Custom property preservation

Custom metadata you've attached to your tables under Extensions is preserved across re-extractions. When SchemaTongs writes a table file, it checks whether a previous file existed for the same table and copies the previous Extensions content forward onto the freshly extracted table.

The preservation pass uses the component's Name (with quote/bracket characters stripped, case-insensitive). For columns and the table itself, matching also falls back to OldName, so renamed components keep their custom metadata as long as you set OldName correctly before the rename is deployed.

ComponentMatched by
Tableroot object
ColumnName, then OldName
IndexName
ForeignKeyName
CheckConstraintName
ExcludeConstraintName
StatisticName
MaterializedViewroot object

This is what makes Custom Properties trustworthy across the SchemaTongs / SchemaQuench round-trip: you attach metadata once, and it survives every re-extraction as long as the underlying object stays.

Script validation

Catch problems at extraction time instead of discovering them during deployment. When ShouldCast:ValidateScripts is true, SchemaTongs tests each extracted SQL script for validity immediately after writing it. The validation strategy depends on the platform and the object type, but the principle is the same: try to compile the object server-side, roll back, and flag failures.

Invalid script handling

Scripts that fail validation are handled according to ShouldCast:SaveInvalidScripts:

SaveInvalidScriptsBehavior
true (default)The script is saved with a .sqlerror extension instead of .sql. The original .sql file (if any) is removed.
falseThe script isn't written to disk at all.

.sqlerror files

.sqlerror files are SQL scripts that failed extraction validation. They serve as a record of potential problems without blocking the rest of the package.

ToolBehavior
SchemaQuenchSkips .sqlerror files — only .sql files are loaded and executed.
SchemaTongsOn re-extraction, overwrites .sqlerror files with the latest content. If the script still fails validation, it stays as .sqlerror. If it now passes, it's written as .sql and the .sqlerror is removed.

False positives

Validation failures aren't always genuine errors. Common false positives include:

  • Cross-database references — Scripts referencing objects in another database may fail when the parser can't resolve the cross-database context.
  • Temporary objects — References to temp tables or variables created elsewhere in the same batch.
  • Forward references inside the same batch — A function that calls another function created later in the same script.

To override a false positive, rename the file from .sqlerror to .sql. SchemaQuench will then include it in the next deployment.

When any scripts fail validation, SchemaTongs generates an _InvalidObjectCleanup.sql file in the log directory containing diagnostic information for all invalid objects detected during the extraction run.

Subfolder preservation

Organize your scripts however makes sense for your team — SchemaTongs respects that structure across re-extractions. Any extracted script folder supports user-created subfolders for organizing scripts. For example, you might organize stored procedures by domain:

Procedures/
  orders/
    public.get_order.sql
    public.create_order.sql
  customers/
    public.get_customer.sql

SchemaTongs preserves these subfolders across re-extraction. Before extraction begins, it builds a file index that maps every .sql, .sqlerror, and .json file to its full path, including any subfolder. When writing an extracted object, SchemaTongs checks the index:

  • If the file already exists in a subfolder, it's written back to that same subfolder.
  • If the file exists in multiple subfolders, a warning is logged and the file is written to the folder root.
  • If the file is new (not in the index), it's written to the folder root.

New objects always appear in the root of their folder. Move them into subfolders as desired — SchemaTongs will remember the location on the next run. This means you can shape your extracted package to fit your code review and team conventions, not the other way around.

Package initialization

The first extraction is where your schema package is born. When SchemaTongs runs against a path that doesn't yet contain a schema package, it creates the full structure:

  1. Creates the product directory at Product:Path.
  2. Generates Product.json with the configured product name and the Platform from Source:Platform.
  3. Creates the template directory under Templates/<TemplateName>/.
  4. Generates Template.json with a DatabaseIdentificationScript targeting the source database.
  5. Creates all standard script folders for the active platform (see Folder Structure).
  6. Creates a .json-schemas/ directory with JSON Schema validation files generated on the fly from the live engine types.

On subsequent runs against an existing package, SchemaTongs overwrites object scripts and table definitions with the current database state. It does not modify Product.json or Template.json — if you change Platform, Name, or TemplateOrder after the first extraction, those edits stick.

Helper procedures

On every run, SchemaTongs deploys (or updates) lightweight helper procedures in the source database under the SchemaSmith schema. These procedures generate the JSON representation of tables and views and are used internally by the extraction adapter. They're read-only, schema-prefixed, and excluded from extraction output.

CheckConstraintStyle

The Product:CheckConstraintStyle setting controls how check constraints are represented in extracted table JSON. It only takes effect when creating a new Product.json — on existing products, the style is read from the product file itself.

ColumnLevel (default)

Each check constraint is stored as a CheckExpression property on the column it applies to:

{
  "Name": "status",
  "DataType": "varchar(20)",
  "CheckExpression": "status IN ('Active','Inactive','Pending')"
}

TableLevel

All check constraints are promoted to named entries in the table's CheckConstraints array:

{
  "CheckConstraints": [
    {
      "Name": "ck_orders_status",
      "Expression": "status IN ('Active','Inactive','Pending')"
    }
  ]
}

TableLevel style preserves the original constraint names from the database. Use it when constraint naming matters for your deployment process or when you have multi-column check constraints.

If the config specifies a different style than what is already in Product.json, SchemaTongs logs a warning and extracts using the style from the existing product file. To change styles on an existing product, update Product.json directly.

ScriptDynamicDependencyRemovalForFunctions

When ShouldCast:ScriptDynamicDependencyRemovalForFunctions is true, SchemaTongs generates a dynamic SQL preamble before each function script. This preamble detects and temporarily removes database objects that depend on the function (computed columns, check constraints, foreign keys involving computed columns, indexes on computed columns or filter expressions referencing the function). At deployment time, SchemaQuench executes the preamble first, dropping all dependents, then creates or alters the function. The dependents are recreated by subsequent steps in the quench (table definitions restore computed columns, constraints, and indexes).

Surgical use only

This is a surgical fix, not a default. Enable it on functions where:

  1. The function is referenced by computed columns or filtered indexes, AND
  2. Changes regularly enough that manual dependency management is impractical, AND
  3. The dependency drop-and-rebuild is acceptably fast.

The risk that makes this opt-in: if a computed column is persisted or indexed on a 500-million-row table, dropping that dependency means rebuilding the persisted column or index after the function is updated — which could take a very long time and impact production availability. For functions that rarely change, the right approach is to leave this flag off and write a migration script that handles the dependencies carefully when the function truly needs to change.

Excluded objects

SchemaTongs automatically excludes the platform's system schemas and internal infrastructure:

  • System objects — Anything flagged as system-shipped by the source engine.
  • Built-in schemaspg_* (catalog, toast, temp, and any other reserved prefix) and information_schema.
  • SchemaSmith infrastructure — All objects in the SchemaSmith schema (the helper procedures SchemaTongs and SchemaQuench deploy).

Last reviewed May 2026 by the SchemaSmith Team.