extract-pg-schema is a utility library designed to programmatically extract comprehensive metadata from a PostgreSQL database and return it as a structured JavaScript object. Currently stable at version 5.8.1, the package maintains an active release schedule, frequently delivering patch and minor updates to enhance features and ensure compatibility. It serves as a foundational component for other tools like Kanel, which leverages its output to generate TypeScript types, and Schemalint, used for database schema linting. A key differentiator is its ability to integrate seamlessly with standard `node-postgres` connection configurations and its provision of both a flexible programmatic API and a convenient command-line interface, making it adaptable for various use cases ranging from automated code generation to direct schema inspection.
npm install extract-pg-schemaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a PostgreSQL database using environment variables or a direct connection config, extract specific schemas, and then log basic information about the extracted schema, such as table and column counts.
Review your usage of extracted function metadata. If specific details about window or aggregate functions are crucial, consider alternative methods for obtaining this information directly from PostgreSQL's `pg_catalog` or `information_schema` views.
After upgrading, re-evaluate the extracted trigger metadata for accuracy and completeness, especially if you have complex or non-standard trigger definitions. Adjust downstream processes that consume trigger data as necessary.
If encountering issues related to `pglite`, install it explicitly: `npm install pglite` or `yarn add pglite`.
Ensure your project is configured for ESM (`"type": "module"` in `package.json`) and use `import { extractSchemas } from 'extract-pg-schema';`. If sticking with CJS, be aware of potential interoperability issues and ensure your build process handles module resolution correctly.Verify that your PostgreSQL server is running. Check your connection configuration (host, port, user, password) for accuracy. Ensure no firewall is blocking the connection to the database port.
Double-check the `database` property in your connection object or the `-d` option when using the CLI to ensure it matches an actual database name.
For ESM projects (Node.js >=16, `"type": "module"` in `package.json`), use `import { extractSchemas } from 'extract-pg-schema';`. If strictly in CommonJS, you might need to ensure your environment supports dynamic `import()` or adjust your build system if direct `require` fails.