Skip to content

kristiandupont/extract-pg-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

9d9e916 · Jan 4, 2025
Oct 8, 2024
Feb 7, 2024
Dec 28, 2024
Dec 28, 2024
Jan 4, 2025
Oct 20, 2020
Jul 6, 2022
Dec 2, 2023
Dec 2, 2023
Oct 20, 2020
Jun 28, 2019
Feb 7, 2024
Jul 4, 2022
Dec 28, 2024
Feb 7, 2024
Jan 4, 2025
Jan 4, 2025
Nov 11, 2020
Mar 21, 2023
Apr 1, 2024

Repository files navigation

Extract Schema from Postgres Database

Reads various metadata from your postgres database and return a Javascript object. This package is used by Kanel to generate Typescript types and Schemalint to provide linting of database schemas.

View the documentation here

Installation

npm i extract-pg-schema

Library Usage

You give it a postgres connection config object and some options and it will connect to your database and generate

const { extractSchemas } = require('extract-pg-schema');

async function run() {
  const connection = {
    host: 'localhost',
    database: 'postgres',
    user: 'postgres',
    password: 'postgres',
  };

  const result = await extractSchemas(connection);

  console.log(result);
}

run();

For an example of a generated object, take a look at dvdrental.json file which is generated from the sample Database from PostgreSQLTutorial.com.

CLI Usage

You can also use the CLI to extract the schemas from a database and write it to the console or a file in JSON format.

npx extract-pg-schema -h localhost -p 5432 -U postgres -d postgres > schemas.json

The CLI takes a small subset of the options that pg_dump takes. You can also use the environment variables starting with PG to set the connection parameters.

Usage: extract-pg-schema [options] [DBNAME]

Extract all schemas from a PostgreSQL database and print them as JSON.

Options:
    --help                      show this help
    -h, --host=HOSTNAME         database server host or socket directory
    -p, --port=PORT             database server port
    -U, --username=USERNAME     database user name
    -d, --dbname=DBNAME         database name to connect to
    -n, --schema=SCHEMA         include schema regular expression (may be given multiple times)
    -N, --exclude-schema=SCHEMA exclude schema regular expression (may be given multiple times)

Contributors

Made with contrib.rocks.