PHPackages                             calebdw/pg-schema-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. calebdw/pg-schema-parser

ActiveLibrary[Database &amp; ORM](/categories/database)

calebdw/pg-schema-parser
========================

Parses the schema of a PostgreSQL database out of plain-text pg\_dump output.

001PHPCI passing

Since Aug 24Pushed todayCompare

[ Source](https://github.com/calebdw/pg-schema-parser)[ Packagist](https://packagist.org/packages/calebdw/pg-schema-parser)[ RSS](/packages/calebdw-pg-schema-parser/feed)WikiDiscussions master Synced today

READMEChangelog (1)DependenciesVersions (1)Used By (1)

 **Reads a PostgreSQL schema out of pg\_dump output**

 [![Latest Version](https://camo.githubusercontent.com/77dfea993c08899711a3b42256a307a97f350918fabf2d1cbd34f0f4b95749b7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63616c656264772f70672d736368656d612d7061727365722e737667)](https://packagist.org/packages/calebdw/pg-schema-parser) [![PHP Compatibility](https://camo.githubusercontent.com/3842242e10177a46891675f8fbd7da5ecb56172bb0cd6d8a4b44601065656786/68747470733a2f2f62616467652e6c61726176656c2e636c6f75642f7068702d62616467652f63616c656264772f70672d736368656d612d706172736572)](https://packagist.org/packages/calebdw/pg-schema-parser) [![Total Downloads](https://camo.githubusercontent.com/f9f55ca37e7bec717e9837096e1101136c4bc991690e071801985a834f3e4388/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63616c656264772f70672d736368656d612d7061727365722e737667)](https://packagist.org/packages/calebdw/pg-schema-parser) [![Tests](https://github.com/calebdw/pg-schema-parser/actions/workflows/tests.yml/badge.svg)](https://github.com/calebdw/pg-schema-parser/actions/workflows/tests.yml) [![License](https://camo.githubusercontent.com/39e4bd6e04f5635e99ab988ae45a0b3a5f3eae720c1690d9f3a9813ae80ae44f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f63616c656264772f70672d736368656d612d706172736572)](LICENSE)

Reads the schema of a PostgreSQL database out of plain-text `pg_dump` output, in pure PHP.

SQL parsing in PHP is largely oriented towards MySQL, so PostgreSQL's type grammar tends to be only partially supported — the multi-word standard spellings such as `timestamp with time zone` and `double precision`, the PostgreSQL-native types, and array columns. This reads them properly.

Installation
------------

[](#installation)

```
composer require calebdw/pg-schema-parser
```

The only runtime dependency is [`sad_spirit/pg_builder`](https://github.com/sad-spirit/pg-builder), whose reimplementation of PostgreSQL's own type grammar does the hard part.

Usage
-----

[](#usage)

```
use CalebDW\PgSchemaParser\PgDumpParser;

$database = (new PgDumpParser())->parse(file_get_contents('pgsql-schema.sql'));

foreach ($database->tables as $table) {
    echo $table->qualifiedName(), "\n";

    foreach ($table->columns as $column) {
        printf(
            "  %-20s %-24s %s\n",
            $column->name,
            (string) $column->type,
            $column->nullable ? 'null' : 'not null',
        );
    }
}
```

### Canonical type names

[](#canonical-type-names)

Type names are canonicalised the way PostgreSQL itself resolves them, so each type has exactly one spelling to handle:

Declared in the dump`$column->type->qualifiedName()``integer`, `int`, `int4``pg_catalog.int4``bigint`, `int8``pg_catalog.int8``boolean``pg_catalog.bool``double precision`, `float8``pg_catalog.float8``character varying(255)``pg_catalog.varchar``character(2)``pg_catalog.bpchar``timestamp(0) without time zone``pg_catalog.timestamp``timestamp with time zone``pg_catalog.timestamptz``bit varying(5)``pg_catalog.varbit``jsonb`, `uuid`, `inet``jsonb`, `uuid`, `inet`Length and precision arrive separately from array dimensions, which is the distinction a consumer needs to tell `string` from `list`:

```
$type = $database->table('t')->column('tags')->type;   // character varying(50)[]

$type->qualifiedName();  // 'pg_catalog.varchar'
$type->modifiers;        // ['50']
$type->dimensions;       // 1
$type->isArray();        // true
```

### Enums and domains

[](#enums-and-domains)

PostgreSQL enums are a type rather than a column constraint, so the labels are attached to the database rather than the column:

```
$type = $database->table('people')->column('mood')->type;

$type->isBuiltin();               // false
$database->enumFor($type)->values; // ['sad', 'ok', 'happy']
```

Domains resolve to the type they wrap, transitively:

```
$database->resolve($type)->qualifiedName(); // 'pg_catalog.int4'
```

A type that is neither built in, nor an enum, nor a domain came from an extension — `hstore`, `ltree`, `citext` — or is a composite. `resolve()` returns it unchanged, and it is up to the consumer to decide what to do with it; treating an unrecognised type as a string is usually right.

### Keys and defaults

[](#keys-and-defaults)

`pg_dump` does not write primary keys inline, and it rewrites `serial` into a plain integer column plus a sequence. Both are reassembled:

```
$table = $database->table('users');

$table->primaryKey;                        // ['id']
$table->column('id')->isAutoIncrement();   // true, from DEFAULT nextval(...)
$table->column('id')->isRequired();        // false - the database supplies it
```

Scope
-----

[](#scope)

The accepted grammar is deliberately the subset `pg_dump` emits, not all of PostgreSQL DDL. That subset is small, regular and stable, because `pg_dump` is a code generator rather than a person. Recognised statements:

- `CREATE TABLE`, including quoted identifiers, schema qualification, table constraints, generated and identity columns
- `CREATE TYPE ... AS ENUM`
- `CREATE DOMAIN`
- `ALTER TABLE ... ADD CONSTRAINT ... PRIMARY KEY`
- `ALTER TABLE ... ALTER COLUMN ... SET DEFAULT`

Everything else — functions, triggers, indexes, views, grants, comments, extensions, `SET`, and psql meta-commands such as the `\restrict` that `pg_dump`has emitted since PostgreSQL 18 — is skipped rather than rejected. Dollar-quoted function bodies, string literals containing `--`, and comments containing SQL are all handled without being mistaken for schema.

Not currently read: foreign keys, unique constraints, indexes, check constraints, partitioning, inheritance, and views.

Contributing
------------

[](#contributing)

See [CONTRIBUTING.md](CONTRIBUTING.md).

License
-------

[](#license)

MIT.

###  Health Score

21

—

LowBetter than 17% of packages

Maintenance65

Regular maintenance activity

Popularity0

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b188ac4984d22fd742e5a2654677043a28ddd364b867f41c6aeea654b6fe74b6?d=identicon)[calebdw](/maintainers/calebdw)

---

Top Contributors

[![calebdw](https://avatars.githubusercontent.com/u/4176520?v=4)](https://github.com/calebdw "calebdw (5 commits)")

---

Tags

parserpg-dumppostgrespostgresqlschemasql

### Embed Badge

![Health badge](/badges/calebdw-pg-schema-parser/health.svg)

```
[![Health](https://phpackages.com/badges/calebdw-pg-schema-parser/health.svg)](https://phpackages.com/packages/calebdw-pg-schema-parser)
```

###  Alternatives

[jdorn/sql-formatter

a PHP SQL highlighting library

3.8k117.8M121](/packages/jdorn-sql-formatter)[backup-manager/backup-manager

A framework agnostic database backup manager with user-definable procedures and support for S3, Dropbox, FTP, SFTP, and more with drivers for popular frameworks.

1.7k1.6M11](/packages/backup-manager-backup-manager)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M88](/packages/propel-propel1)[insolita/yii2-migration-generator

Set of gii tools for generating files for migration by schema of table , phpdoc or table data

108508.0k5](/packages/insolita-yii2-migration-generator)[ichikaway/cakephp-mongodb

MongoDB Datasource for CakePHP

3388.2k](/packages/ichikaway-cakephp-mongodb)[xpdo/xpdo

A PDO-based Object/Relational Bridge Library

7088.4k4](/packages/xpdo-xpdo)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
