Developer templates (F7)

A developer template is a .pgtemplate file: a parameter declaration (TOML) and a Jinja body. F7 in a query tab renders one from a generated form and inserts the SQL at the cursor. It is never executed. Templates are edited with the template editor or any text editor.

Installing the templates

Templates are read only from the templates folder. The built-in ones are embedded in the executable and are installed with pgtc --setup, pgtc --install-templates or Settings → Install templates.

Using a template

F7 (Tools → Generate SQL from developer template…) in a connected query tab: choose a template, fill in the form, OK (or Ctrl+Enter from any field, where the terminal reports it). The SQL replaces the selection or goes in at the cursor.

The picker

The template picker
Directories are sections; the line under the tree is the template's description.

The built-in templates

SectionTemplates
FunctionsCreate function, Create procedure, Drop function, Drop procedure
IndexesCreate index (incl. CONCURRENTLY, NULLS NOT DISTINCT, WHERE), Drop index
RolesCreate role, Drop role (optionally REASSIGN OWNED / DROP OWNED first — current database only)
SchemasCreate schema, Drop schema
SequencesCreate sequence, Drop sequence
TablesCreate table, Drop table
TriggersCreate trigger with function, Drop trigger
TypesCreate enum type, Create domain, Drop type, Drop domain
ViewsCreate view, Create materialized view, Drop view, Drop materialized view

Every DROP template has IF EXISTS (default off) and, except Drop role, RESTRICT (default) / CASCADE.

The form

The Create table form
Create table: parameters and the live preview.

Choice lists read from the catalogs

Drop table with its table list open
Drop table: the table list, open; typing filters it.

Defaults from the tree's selection

If the object selected in the object tree is on the same server and database as the tab, choice parameters with a built-in source start with it: its schema for schemas; a table, partition or foreign table for tables; a trigger (with its table) for triggers; a role for roles (only the server has to match).

Lists: columns, arguments, labels

Editing a row of the Columns list
The row dialog of a list parameter.

Enter on a row edits it. The row dialog validates its fields like the form. Row order is the iteration order of the body's {% for %} loop.

SQL boxes

Create function with its Body box
Create function: a sql box with fill = true.

A sql parameter is a SQL editor with the usual editor keys and autocomplete; its text reaches the body verbatim. Tab leaves the box; Esc closes the autocomplete popup first.

Writing your own templates

The template editor (Tools → Developer templates…) writes and validates the file. The rest of this page is the file's specification.

Where the files go

PlatformTemplates folder
Linux~/.local/share/pgtc/developer_templates
macOS~/Library/Application Support/pgtc/developer_templates
Windows%APPDATA%\pgtc\developer_templates
developer_templates/
    default/                          every server version
        Tables/
            table.pgtemplate
        Our team/                     your own section
            audit_table.pgtemplate
    12_plus/                          overrides for PostgreSQL 12 and newer
        Triggers/
            trigger.pgtemplate

Installing the templates again overwrites the built-in files. Save a changed built-in template under another file name (the editor warns).

The file

UTF-8. The first line is +++; a second +++ line separates the TOML header from the Jinja body.

+++
name        = "Create lookup table"
description = "A code / name table with our standard columns"

[[param]]
name     = "schema"
label    = "Schema"
type     = "choice"
source   = "schemas"
default  = "public"
required = true

[[param]]
name     = "name"
label    = "Table name"
type     = "identifier"
required = true
help     = "Singular, lower case: country, orderstatus"

[[param]]
name    = "code_length"
label   = "Length of the code"
type    = "number"
default = "10"

[[param]]
name  = "with_audit"
label = "Audit columns (modifiedby, modifieddate)"
type  = "bool"
+++
CREATE TABLE {{ schema|ident }}.{{ name|ident }} (
    code varchar({{ code_length }}) NOT NULL,
    name text NOT NULL,
{% if with_audit %}
    modifiedby text NOT NULL DEFAULT CURRENT_USER,
    modifieddate timestamp NOT NULL DEFAULT now(),
{% endif %}
    CONSTRAINT {{ (name ~ "_pkey")|ident }} PRIMARY KEY (code)
);
The form of the example template
Its form.

TOML. An unknown key is an error: the template is listed as (error).

KeyMeaning
nameLabel in the picker. Absent or blank: the file name without extension.
descriptionOne line under the picker's tree.
[[param]]One table per parameter, in form order.

Keys of [[param]]:

KeyMeaning
nameRequired. The variable in the body. Letters, digits, _; unique per level.
typeRequired. See below.
labelField label. Default: name.
requiredtrue: an empty value fails validation.
defaultInitial value: a string (a number is accepted); true for a bool.
helpHint shown while the field has the focus.

Parameter types

typeAdditional keysValue in the body
textString, as typed.
identifierThe unquoted identifier (at most 63 bytes); quote it with |ident.
numberInteger or float, also inside a list row; a value the template language would print differently (1e3, a long numeric) stays the typed text.
boolBoolean.
choiceExactly one of options = ["a", "b"], source = "tables", query = "SELECT …"String, or an object for a schema-qualified source (below).
sqlfill = true: the box takes the form's free rows (not in a list row); otherwise 5 rowsString, with its line breaks.
list[[param.fields]]: the fields of a row, declared like [[param]]; no nested listsSequence of maps: {% for r in x %}{{ r.field }}{% endfor %}.

Choice lists from the catalogs: source

sourceRows{{ value|ident }}
schemaspg_namespacesales
tablespg_class, relkind r, p, fsales.customer
viewsrelkind = 'v'sales.vsalesperson
materialized_viewsrelkind = 'm'sales.mv
sequencesrelkind = 'S'sales.customer_customerid_seq
functionspg_proc, prokind = 'f', with pg_get_function_identity_arguments()sales.customer_total(integer, integer)
proceduresprokind = 'p', likewisesales.close_month(date)
typesA fixed list of common built-in type names, then the non-array base, composite, domain, enum and range types of pg_typeinteger, double precision, sales."Order Status"
user_typespg_type: enum, stand-alone composite, range and base types — no domains, arrays, or multiranges owned by a rangesales.orderstatus
domainstyptype = 'd'public."Flag"
indexespg_index, what DROP INDEX accepts: not constraint-backing indexes, not partitions of a partitioned indexsales.ix_customer_territoryid
triggerspg_trigger, NOT tgisinternal, without clones on partitions (pg_depend.deptype = 'P')trg ON sales.customer
rolespg_roles, all rowsreporting

A custom query

The body

Jinja, rendered by minijinja. What pgtc adds:

SyntaxMeaning
{{ x|ident }}Identifier: double-quoted unless it matches [a-z_][a-z0-9_]* and is not a reserved, type-name or column-name key word. Use it for every identifier.
{{ x|qtLiteral }}String literal: O'Brien → 'O''Brien'.
{{ (name ~ "_pkey")|ident }}A derived name, quoted as one identifier.
{{ server_version }}, {{ database }}server_version_num as an integer (170002); the tab's database. No parameter may have one of these names.
{% if has("NullsNotDistinct") %}Feature test against pgtc's feature table. An unknown feature name is a rendering error.