---
name: "pg_tapgen"
section: "1p"
platform: "linux"
package: "libtap-parser-sourcehandler-pgtap-perl"
version: "3.37-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/pg_tapgen"
---

# pg\_tapgen(1p)

Generate schema TAP tests from an existing database

## Name

pg\_tapgen - Generate schema TAP tests from an existing database

## Usage

```
  pg_tapgen -d template1 > schema_test.sql
```

## Description

`"pg_tapgen"` is a command-line utility to generate pgTAP tests to validate a database schema by reading an existing database and generating the tests to match. Its use requires the installation of the DBI and DBD::Pg from CPAN or via a package distribution.

**Warning:** These prerequisites are not validated by the pgTAP `"Makefile"`, so you'll need to install them yourself. As a result, inclusion of this script in the pgTAP distribution is experimental. It may be moved to its own distribution in the future.

## Options

```
  -d --dbname DBNAME        Database to which to connect.
  -U --username USERNAME    Username with which to connect.
  -h --host HOST            Host to which to connect.
  -p --port PORT            Port to which to connect.
  -v --verbose              Display output of test scripts while running them.
  -N --exclude-schema       Exclude a schema from the generated tests.
  -c --create-extension     Include CREATE EXTENSION pgtap statement.
     --no-create-extension  Excluce CREATE EXTENSION pgtap statement.
     --directory DIRECTORY  Directory to which to write the test files.
  -H --help                 Print a usage statement and exit.
  -m --man                  Print the complete documentation and exit.
  -V --version              Print the version number and exit.
```

## Options Details

- "-d"

  - "--dbname"

  ```
    pg_tapgen --dbname try
    pg_tapgen -d postgres
  ```

  The name of database to which to connect. Defaults to the value of the `$PGDATABASE` environment variable or to the system username.

- "-U"

  - "--username"

  ```
    pg_tapgen --username foo
    pg_tapgen -U postgres
  ```

  PostgreSQL user name to connect as. Defaults to the value of the `$PGUSER` environment variable or to the operating system name of the user running the application. Password can be specified with `$PGPASSWORD`.

- "-h"

  - "--host"

  ```
    pg_tapgen --host pg.example.com
    pg_tapgen -h dev.local
  ```

  Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for the Unix-domain socket. Defaults to the value of the `$PGHOST` environment variable or localhost.

- "-p"

  - "--port"

  ```
    pg_tapgen --port 1234
    pg_tapgen -p 666
  ```

  Specifies the TCP port or the local Unix-domain socket file extension on which the server is listening for connections. Defaults to the value of the `$PGPORT` environment variable or, if not set, to the port specified at compile time, usually 5432.

- "--dir"

  - "--directory"

  Directory to which to write test files. Defaults to the current directory.

- "-c"

  - "--create-extension"

  - "--no-create-extension"

  ```
    pg_tapgen --create-exension
    pg_tapgen -c
    pg_tapgen --no-create-exension
  ```

  Enable or disable the inclusion of `"CREATE EXTENSION pgtap;"` statements in the generated test files. Enabled by default.

- "-v"

  - "--verbose"

  ```
    pg_tapgen --verbose
    pg_tapgen -v
  ```

  Display standard output of test scripts while running them. This behavior can also be triggered by setting the `$TEST_VERBOSE` environment variable to a true value.

- "-N"

  - "--exclude-schema"

  ```
    pg_tapgen --exclude-schema contrib
    pg_tapgen -N testing -N temporary
  ```

  Exclude a schema from the test generation. `"pg_tapgen"` always ignores `"information_schema"`, as it is also ignored by pgTAP. But if there are other schemas in the database that you don't need or want to test for in the database (because you run the tests on another database without those schemas, for example), use `"--exclude-schema"` to omit them. May be used more than once to exclude more than one schema.

- "-H"

  - "--help"

  ```
    pg_tapgen --help
    pg_tapgen -H
  ```

  Outputs a brief description of the options supported by `"pg_tapgen"` and exits.

- "-m"

  - "--man"

  ```
    pg_tapgen --man
    pg_tapgen -m
  ```

  Outputs this documentation and exits.

- "-V"

  - "--version"

  ```
    pg_tapgen --version
    pg_tapgen -V
  ```

  Outputs the program name and version and exits.

## Author

David E. Wheeler \<dwheeler@cpan.org\>

## Copyright

Copyright (c) 2009-2025 David E. Wheeler. Some Rights Reserved.
