CI/CD#

Deploy pgmi projects from any CI system: install a pinned pgmi binary, point it at a direct database connection, and run pgmi deploy with a pinned API version. The connection’s credential comes from a CI secret or, with OIDC, from a short-lived cloud token.

Requirements#

  • A direct PostgreSQL connection (or a session-mode pooler). Transaction-mode poolers — PgBouncer in transaction mode, AWS RDS Proxy, Azure’s built-in PgBouncer — reassign connections between statements and destroy the session-scoped temp tables pgmi relies on. See Connection Requirements .
  • Secrets from your CI secret store, never on the command line. See the Security Guide .

pgmi’s exit codes are the pipeline contract — every failure point is numbered, so CI branches on $? instead of parsing output:

The deploy sequence: validate, connect, lock, prepare the session, preprocess, then your deploy.sql runs — with exit codes at each failure point

GitHub Actions#

name: Deploy database

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    env:
      PGMI_VERSION: v0.20.1         # pin to a specific release tag
      DB_NAME: myapp
    steps:
      - uses: actions/checkout@v5

      - name: Install pgmi (pinned, checksum-verified)
        run: |
          file="pgmi_${PGMI_VERSION#v}_linux_amd64.tar.gz"
          base="https://github.com/vvka-141/pgmi/releases/download/${PGMI_VERSION}"
          curl -fsSLO "${base}/${file}"
          curl -fsSLO "${base}/checksums.txt"
          sha256sum --ignore-missing -c checksums.txt
          tar -xzf "${file}" pgmi
          sudo install pgmi /usr/local/bin/pgmi
          pgmi --version

      - name: Deploy
        env:
          PGMI_CONNECTION_STRING: ${{ secrets.DATABASE_URL }}   # direct connection
        run: pgmi deploy . -d "$DB_NAME" --compat 1 --force --param env=prod

Why these choices:

  • Pinned, checksum-verified binary — no toolchain, reproducible runs, and the download is integrity-checked against the release checksums.txt. To also check that the file was built by pgmi’s release workflow on main, run gh attestation verify "${file}" --repo vvka-141/pgmi --signer-workflow vvka-141/pgmi/.github/workflows/release-rust.yml --source-ref refs/heads/main. Never install “latest”: a new release would silently change your deploy.
  • The pinned binary pins the session API. Before pgmi 1.0 the session API may change in a minor release — a view, column or function added, renamed or removed — and every change is listed under Upgrading in that release’s notes. Read it before you bump PGMI_VERSION.
  • --compat 1 makes the deploy fail (exit 10) on a binary that no longer ships contract v1, instead of running against a different one. It does not freeze columns across pgmi releases before 1.0: v0.14 renamed pgmi_plan_view.checksum to pgmi_checksum within contract 1. See CLI Reference .
  • --force skips the interactive confirmation (there is no TTY in CI). It does not drop your database — that is --overwrite, which belongs only on throwaway/test databases, never production.
  • PGMI_CONNECTION_STRING carries host, user, and password from a single secret, for example postgresql://deployer:<password>@db.example.com:5432/postgres?sslmode=verify-full&sslrootcert=system (system trusts the runner’s CA store; for a private CA, the path of its certificate). Point it at a direct connection, not a transaction-mode pooler.

This is the smallest job. For deploys serialized per database, the deploy record kept as an artifact, the advanced template’s parameters and OIDC, use the full job ; for GitLab, the GitLab job .

Pull-request gate without a database#

pgmi metadata validate and pgmi metadata plan read the project and never connect. Run them on every pull request to catch bad <pgmi-meta> blocks, duplicate ids and an unexpected execution order before anything touches a database:

pgmi metadata validate . --json
pgmi metadata plan . --json

Both exit 10 on invalid metadata or duplicate ids. See CLI Reference for the JSON shapes.

Reading the result#

pgmi deploy --json prints a JSON envelope to stdout on success and on failure: exit code, SQLSTATE, the failing file and line, plus the pgmi, contract and PostgreSQL versions, the target host, port and database, and start and finish times. connectionSource names where the connection came from (the variable holding the connection string, pgmi.yaml, defaults…), tls whether the session was encrypted and under which sslmode, and overrides lists every pgmi.yaml value an environment variable or the connection string replaced. A password deploy belongs on sslmode=verify-full with sslrootcert: the default prefer falls back to plaintext, which an attacker on the path can force. The same lines go to stderr, so the CI log shows them too. Branch on the exit code; read the envelope for the report. See CLI Reference .

Keep the envelope as a build artifact. A failed deploy rolls back, so an audit table written by deploy.sql records only successes. The envelope is the record of the failures. It never contains the connection’s user, password or token, and pgmi redacts a SQL PASSWORD '...' literal. Any other secret your SQL expands inside EXECUTE reaches the envelope in a runtime error’s context, so wrap such statements (see SECURITY.md ).

pgmi deploy . -d "$DB_NAME" --compat 1 --force --param env=prod --json > deploy-result.json

A parameter that was not wired. The basic and advanced templates refuse a missing or malformed parameter with exit 13 and sqlstate 22023 (invalid_parameter_value), before any change, so CI can tell “the secret was not passed” from a failed migration. A first deploy refused this way leaves an empty database behind and is safe to re-run once the parameter is set.

Concurrent deploys and timeouts#

pgmi takes a per-database lock before it runs anything. A second pgmi deploy against the same database does not wait: it exits 15 at once. Serialize deploys in the CI system, for example with a GitHub Actions concurrency: group per database, so a second run queues instead of failing. Treat exit 15 as “another deploy is running”, not as a broken deploy. Everything else pgmi guarantees in an unattended run is listed in Running unattended .

--timeout (default 3m) covers the whole run, including the autocommit tail and every retry. A transient failure (a failover, a restart, a deadlock with live traffic) runs the deploy again from the start inside that time box, so the pipeline needs no retry step of its own; a broken migration fails on the first attempt with its own exit code. Give the job a timeout-minutes above --timeout, so pgmi, not the runner, decides when to stop and reports why. What is retried and what never is: Time box and retries .

A CREATE INDEX CONCURRENTLY that outlives the time box is cancelled: pgmi exits 16, says an INVALID index may be left and how to find and drop it (also in the --json hint). Raise --timeout for large tables, and write the build so a re-run reaps the leftover (see making a concurrent index re-runnable ).

Environment reference#

Everything a pipeline can set for pgmi deploy, in one place: what each variable does, whether it is a secret, and what outranks it.

Precedence#

pgmi first picks one connection string, then resolves each connection field.

SettingHighest wins
Connection string--connection > PGMI_CONNECTION_STRING > DATABASE_URL. -h, -p or -U next to either variable exits 10: with DATABASE_URL set, adding -h db.example.com is refused, never merged and never a silent fall back to localhost. --sslmode replaces the string’s sslmode, announced on stderr and in --json overrides.
Host, port, user, database, sslmode, TLS pathsflag > the field in the connection string > its PG* variable > pgmi.yaml > built-in default. With a connection string, pgmi.yaml fills only the database, the maintenance database and the TLS paths (why ).
Parameters--param > --params-file (later file wins) > PGMI_PARAM_* > pgmi.yaml params
Timeout--timeout > pgmi.yaml timeout > 3m
Project .envA variable already set in the environment wins over the same name in <project>/.env. Only pgmi deploy reads .env; pgmi serve ignores it. A .env may set only PGMI_PARAM_*, the connection (PGMI_CONNECTION_STRING, DATABASE_URL, the PG* variables pgmi supports), the AWS region, keys and retry settings, and PGMI_MAX_FILE_SIZE, PGMI_NO_BANNER and NO_COLOR. Any other key (HOME, PATH, BASH_ENV, AWS_PROFILE, HTTPS_PROXY, PGSERVICE, an application’s NODE_ENV, …) stops the deploy with exit 10 before it connects, and the error names the key. Set what pgmi or a cloud sign-in needs in the environment pgmi runs in; keep an application’s settings in a file of another name. A .env that exists but cannot be read exits 10.

In CI, set variables from the secret store and keep .env out of the checkout. The basic and advanced templates already ignore *.env in git.

Variables#

pgmi’s own

VariableSecretEffect
PGMI_CONNECTION_STRINGyesFull connection string, URI or Key=Value; form
DATABASE_URLyesConnection string when PGMI_CONNECTION_STRING is unset
PGMI_PARAM_<NAME>when it holds oneParameter <name> (lower-cased), read as current_setting('pgmi.<name>', true). An empty value exits 10, so an unmapped CI secret never arrives as ''
PGMI_MAX_FILE_SIZEnoPer-file cap in bytes (default 10 MiB); any other form exits 10
CI (any value), PGMI_NON_INTERACTIVE=1noTurn off the interactive wizards and prompts. --overwrite then needs --force, or it exits 12
PGMI_NO_BANNER, NO_COLOR, PGMI_PAGER, PAGERnoBanner, colour and pager (PGMI_PAGER before PAGER); no effect on the deploy

libpq variables pgmi resolves itself. These take part in the precedence above, and pgmi names each one that replaces a pgmi.yaml value.

VariableSecretEffect
PGHOST, PGPORT, PGUSER, PGDATABASEnoHost, port, user, target database
PGPASSWORDyesPassword; a password in the connection string wins
PGPASSFILEno; protect the file.pgpass location (default ~/.pgpass); a passfile= in the connection string wins. As in libpq, a file other users can read is ignored with a warning
PGSSLMODEnoTLS mode
PGSSLCERT, PGSSLROOTCERTnoClient certificate and CA paths
PGSSLKEYno; protect the key fileClient private key path
PGSSLPASSWORDyesPassphrase of an encrypted client key
PGAPPNAMEnoapplication_name (default pgmi)
PGCONNECT_TIMEOUTnoConnect timeout in seconds, per host and attempt

Applied to every connection. pgmi does not report these, except PGOPTIONS, which it names on stderr because it changes every session’s settings.

None of them is a secret, and a setting in the connection string outranks each one.

VariableEffect
PGOPTIONSServer options, e.g. -c lock_timeout=5s; stderr prints session options: from PGOPTIONS
PGTZSession TimeZone
PGTARGETSESSIONATTRStarget_session_attrs, e.g. read-write with several hosts
PGREQUIREAUTH, PGCHANNELBINDINGRefuse authentication methods, require SCRAM channel binding
PGSSLNEGOTIATION, PGSSLSNI, PGSSLCERTMODETLS handshake details
PGMINPROTOCOLVERSION, PGMAXPROTOCOLVERSIONProtocol version range (a minimum of 3.2 or latest is refused, exit 10; as a maximum both are accepted, and the session speaks 3.0)
PGCLIENTENCODINGThe session’s client_encoding; without it the deploy uses the database’s encoding. auto exits 10

Read by the cloud sign-in, only with --azure, --aws, --google or --databricks. The flags --azure-tenant-id, --azure-client-id, --aws-region and --databricks-endpoint outrank their variables; among credentials, each provider’s default chain decides, in the order its own SDKs use.

VariableSecretEffect
AZURE_TENANT_ID, AZURE_CLIENT_IDnoEntra ID tenant and identity. A client ID without a secret selects that workload or managed identity only (which identity signs in )
AZURE_CLIENT_SECRETyesService principal secret, used with tenant and client ID
AZURE_FEDERATED_TOKEN_FILEno; protect the fileWorkload identity token (Kubernetes)
AWS_REGION, AWS_DEFAULT_REGIONnoRDS region; AWS_REGION first
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKENyesStatic or temporary AWS credentials
AWS_PROFILE, AWS_ROLE_ARNnoProfile and role for the AWS default credential chain
AWS_WEB_IDENTITY_TOKEN_FILEno; protect the fileWeb identity token (Kubernetes, OIDC)
GOOGLE_APPLICATION_CREDENTIALSno; protect the fileService account key for Cloud SQL IAM
PGMI_DATABRICKS_ENDPOINTnoLakebase endpoint, when --databricks-endpoint is not given
DATABRICKS_HOST, DATABRICKS_CONFIG_PROFILE, DATABRICKS_CONFIG_FILE, DATABRICKS_AUTH_TYPEnoWorkspace and sign-in method for --databricks (Lakebase )
DATABRICKS_TOKEN, DATABRICKS_CLIENT_SECRET (with DATABRICKS_CLIENT_ID)yesPersonal access token, or OAuth M2M for a service principal

Not supported. pgmi does not use libpq, so not every libpq variable works:

VariableWhat happens
PGSERVICERefused with exit 10; give host, port and database instead
PGSERVICEFILEIgnored (no service files)
PGHOSTADDRIgnored; put the address in PGHOST, or hostaddr= in the connection string
PGSSLCRL, PGSSLCRLDIRNot checked: a WARNING on stderr and in --json warnings, then the deploy goes on. Under sslmode=verify-ca or verify-full, and under require with a root certificate, they are refused, exit 10, unless the server is verified against the system trust store (sslrootcert=system), where libpq reads no list either
PGREQUIREPEERNot checked: a WARNING on stderr and in --json warnings, then the deploy goes on
PGSSLMINPROTOCOLVERSION, PGSSLMAXPROTOCOLVERSION, PGSSLCOMPRESSION, PGGSSENCMODE, PGKRBSRVNAME, PGGSSLIB, PGLOADBALANCEHOSTSIgnored; their connection-string keys work where pgmi supports them (Connections )
PGDATESTYLE, PGGEQOIgnored; set them through PGOPTIONS, e.g. -c datestyle=ISO

What the log shows#

pgmi never applies a connection source in silence. One example of each line it can print on stderr; --json carries the same facts on stdout:

connection: from PGMI_CONNECTION_STRING (db.example.com:5432/myapp)
./.env applied: PGPASSWORD, PGUSER
sslmode: from PGSSLMODE (disable), not pgmi.yaml (verify-full)
session options: from PGOPTIONS
Line--json field
connection: from <source> (host:port/database), or connection: defaults (...), with (set by ./.env) after the name when .env set it; not printed for --connection or -hconnectionSource
<path> applied: <keys>: the keys .env set, never their valuesdotenvKeys
<field>: from <VAR> (<value>), not pgmi.yaml (<value>)overrides
session options: from PGOPTIONSnone; stderr only

In CI, .env applied should never appear: it means a .env reached the checkout.

GitHub Actions: full job#

A static connection-string secret, deploys serialized per database, the advanced template’s parameters from secrets and variables, and the --json record kept as an artifact even when the deploy fails.

Before the first run:

  • Secrets: DATABASE_URL (a direct connection with sslmode=verify-full, as in the smallest job ) and DATABASE_ADMIN_PASSWORD. Variables: PLATFORM_OPERATORS and OIDC_ISSUERS, as JSON arrays (see pg_temp.parameter_declaration() in the template’s deploy/parameters.sql).
  • The job redeploys an existing database. PGMI_PARAM_UPGRADE: "true" refuses to build a fresh one by mistake, so leave it out of the one deploy that creates the database.
  • platform_operators and oidc_issuers are synced on every deploy. An operator left out of the list loses operator status, so pass both every time.
  • A secret or variable that is not defined arrives empty. pgmi stops before connecting, exit 10, and names the setting: a PGMI_PARAM_* parameter, or a connection setting such as PGHOST, PGPORT or DATABASE_URL (the host from PGHOST is empty; write the host, or remove it so the next source applies). An empty connection setting is never read as unset. Leave a variable out of the environments that do not need it rather than set it to ''.
  • The basic template needs only PGMI_PARAM_ENV; drop the other PGMI_PARAM_* lines.
name: Deploy database

on:
  push:
    branches: [main]

concurrency:
  group: deploy-myapp-prod
  cancel-in-progress: false

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    env:
      PGMI_VERSION: v0.20.1
      DB_NAME: myapp
    steps:
      - uses: actions/checkout@v5

      - name: Install pgmi (pinned, checksum-verified)
        run: |
          file="pgmi_${PGMI_VERSION#v}_linux_amd64.tar.gz"
          base="https://github.com/vvka-141/pgmi/releases/download/${PGMI_VERSION}"
          curl -fsSLO "${base}/${file}"
          curl -fsSLO "${base}/checksums.txt"
          sha256sum --ignore-missing -c checksums.txt
          tar -xzf "${file}" pgmi
          sudo install pgmi /usr/local/bin/pgmi

      - name: Deploy
        env:
          PGMI_CONNECTION_STRING: ${{ secrets.DATABASE_URL }}
          PGMI_PARAM_ENV: prod
          PGMI_PARAM_UPGRADE: "true"
          PGMI_PARAM_DATABASE_ADMIN_PASSWORD: ${{ secrets.DATABASE_ADMIN_PASSWORD }}
          PGMI_PARAM_PLATFORM_OPERATORS: ${{ vars.PLATFORM_OPERATORS }}
          PGMI_PARAM_OIDC_ISSUERS: ${{ vars.OIDC_ISSUERS }}
        run: pgmi deploy . -d "$DB_NAME" --compat 1 --force --json > deploy-result.json

      - name: Keep the deploy record
        if: always()
        uses: actions/upload-artifact@v7
        with:
          name: deploy-result
          path: deploy-result.json
  • concurrency: queues a second run for the same database instead of letting it fail with exit 15. cancel-in-progress: false never cancels a deploy halfway.
  • The human lines go to stderr and stay in the job log; stdout holds only the JSON record.

OIDC instead of a stored password. Set up the database role for token sign-in first (Azure , AWS ). Then, in the job above: add id-token: write to permissions:, insert the login step, drop PGMI_CONNECTION_STRING, and change the deploy command as shown. Keep the other PGMI_PARAM_* lines and the artifact step; the fragments below leave them out.

Azure (azure/login signs in the Azure CLI, which pgmi’s default credential chain uses):

permissions:
  contents: read
  id-token: write
# ...
      - uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          allow-no-subscriptions: true

      - name: Deploy
        env:
          PGMI_PARAM_ENV: prod
          PGMI_PARAM_UPGRADE: "true"
        run: >
          pgmi deploy . --azure -h "${{ vars.DB_HOST }}" -U "${{ vars.DB_ROLE }}"
          -d "$DB_NAME" --compat 1 --force --json > deploy-result.json

Do not also export AZURE_CLIENT_ID to the deploy step: a client ID without a secret makes pgmi use only workload or managed identity, not the Azure CLI login.

AWS (configure-aws-credentials sets AWS_REGION and temporary keys, which the AWS SDK reads):

permissions:
  contents: read
  id-token: write
# ...
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
          aws-region: eu-west-1

      - name: Deploy
        env:
          PGMI_PARAM_ENV: prod
          PGMI_PARAM_UPGRADE: "true"
        run: |
          curl -fsSLO https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
          pgmi deploy . --aws -h "${{ vars.DB_HOST }}" -U iam_deploy -d "$DB_NAME" \
            --sslrootcert global-bundle.pem --compat 1 --force --json > deploy-result.json

GitLab CI#

The full job’s static-secret deploy for GitLab, with the same prerequisites. Define PGMI_CONNECTION_STRING and PGMI_PARAM_DATABASE_ADMIN_PASSWORD as masked, protected CI/CD variables, and PGMI_PARAM_PLATFORM_OPERATORS and PGMI_PARAM_OIDC_ISSUERS as protected ones, in the project settings. GitLab passes them to the job as environment variables under those names, so the job file never mentions them.

deploy-db:
  stage: deploy
  image: debian:bookworm-slim
  environment: production
  resource_group: myapp-prod
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  variables:
    PGMI_VERSION: v0.20.1
    DB_NAME: myapp
    PGMI_PARAM_ENV: prod
    PGMI_PARAM_UPGRADE: "true"
  before_script:
    - apt-get update && apt-get install -y --no-install-recommends curl ca-certificates
    - |
      file="pgmi_${PGMI_VERSION#v}_linux_amd64.tar.gz"
      base="https://github.com/vvka-141/pgmi/releases/download/${PGMI_VERSION}"
      curl -fsSLO "${base}/${file}"
      curl -fsSLO "${base}/checksums.txt"
      sha256sum --ignore-missing -c checksums.txt
      tar -xzf "${file}" pgmi
      install pgmi /usr/local/bin/pgmi
  script:
    - pgmi deploy . -d "$DB_NAME" --compat 1 --force --json > deploy-result.json
  artifacts:
    when: always
    paths:
      - deploy-result.json

resource_group is GitLab’s concurrency:: one job per group at a time, the rest wait. artifacts: when: always keeps the record of a failed deploy.

Other CI systems#

The three steps — install, connect via secret, pgmi deploy … --compat 1 — apply anywhere. Alternatives for the install step:

  • Install script, pinned: curl -sSL https://raw.githubusercontent.com/vvka-141/pgmi/main/scripts/install.sh | PGMI_VERSION=v0.20.1 bash (verifies the download against checksums.txt). The PGMI_VERSION prefix must sit on bash, not curl, or the script falls back to the latest release.
  • Debian/Ubuntu runners (.deb from the release): gh release download v0.20.1 -R vvka-141/pgmi -p 'pgmi_*_linux_amd64.deb' -p checksums.txt && sha256sum --ignore-missing -c checksums.txt && sudo apt install -y ./pgmi_*_linux_amd64.deb.

For a complete GitLab CI job, see GitLab CI .