SQL to pg-promise Types Generator

Convert SQL CREATE TABLE statements to TypeScript interfaces for pg-promise with proper type mapping, helper functions, and query builders.

What is SQL to pg-promise Types Generator?

This tool converts SQL CREATE TABLE statements to TypeScript interfaces for pg-promise. It generates row types, insert types, a Database schema interface, and optional helper functions for type-safe PostgreSQL access in Node.js.

How to Use

  1. Paste your SQL CREATE TABLE statements
  2. Configure type generation options
  3. Click "Generate" and copy the TypeScript types
  4. Use with pg-promise for type-safe database access

Why Use This Tool?

Generate type-safe row interfaces for each table
Map PostgreSQL types to TypeScript types correctly
Create DatabaseSchema for pg-promise type parameter
Optional insert types with auto-generated column handling
Optional update types with all-optional fields
Optional helper functions for common CRUD operations

Tips & Best Practices

  • SERIAL/BIGSERIAL columns are auto-generated and optional in Insert types
  • Columns with DEFAULT values are optional in Insert types
  • Use Date type for timestamps or string for raw text format
  • Use TypedDatabase as your database type: const db = pgp<DatabaseSchema>(connectionString)

Frequently Asked Questions

What is pg-promise?

pg-promise is a PostgreSQL client for Node.js built on top of pg (node-postgres). It provides a promise-based API with advanced query formatting, automatic connections, and extensive TypeScript support for type-safe database access.

How are SQL types mapped to TypeScript types?

SQL types are mapped to TypeScript types suitable for pg-promise. INTEGER/BIGINT map to number, VARCHAR/TEXT map to string, BOOLEAN maps to boolean, TIMESTAMP/DATE map to Date or string, JSON/JSONB map to unknown, UUID maps to string, BYTEA maps to Buffer, and nullable columns get union types like string | null.

Does it generate query helper functions?

Yes, the tool can optionally generate typed query helper functions for common CRUD operations (findById, findAll, insert, update, delete) using pg-promise query formatting syntax with $1, $2 parameter references.

What is the DatabaseSchema interface?

DatabaseSchema maps table names to their row type interfaces. You can use it as the type parameter for pg-promise: const db = pgp<DatabaseSchema>(connectionString) to get type-safe query results.

Is my SQL data sent to a server?

No, all processing happens entirely in your browser. Your SQL schema never leaves your device.

Related Tools