sql-faker
v1.1.1
Published
Generate realistic fake SQL data for 15 databases — PostgreSQL, MySQL, Snowflake, BigQuery, ClickHouse, and more. Dialect-aware INSERT syntax, 8 dataset templates, and 4 output formats.
Downloads
142
Maintainers
Readme
sql-faker
Generate realistic fake SQL data for 15 databases with dialect-aware syntax — SERIAL for PostgreSQL, AUTO_INCREMENT for MySQL, IDENTITY(1,1) for SQL Server, TIMESTAMP_NTZ for Snowflake. Seed a new project, stress-test queries, or build demos without touching production.
# Generate to a file
npx sql-faker --db postgresql --template users --rows 1000 --output seed.sql
# Push directly to a live database
npx sql-faker --db postgresql --template users --rows 1000 --push "postgresql://user:pass@localhost:5432/mydb"Features
- 15 databases — PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, Snowflake, BigQuery, Amazon Redshift, ClickHouse, DuckDB, libSQL (Turso), CockroachDB, TiDB, PlanetScale, SingleStore
- 8 dataset templates — users, orders, products, transactions, blog posts, companies, social profiles, locations
- 4 output formats — SQL INSERT, PostgreSQL COPY, CSV, JSON
- Dialect-correct DDL — each database gets its own
CREATE TABLEwith the right types, auto-increment style, and quoting - Write to file —
--output seed.sqlsaves directly instead of printing to stdout - Push to live DB —
--push <url>connects and executes against PostgreSQL, MySQL, or SQLite - Reproducible output —
--seedflag for deterministic data across runs - Library + CLI — use it programmatically or from the command line
Install
npm install sql-faker
# or
npm install -g sql-fakerCLI
sql-faker [options]
Options:
-d, --db <database> target database (default: "postgresql")
-t, --template <template> dataset template (default: "users")
-r, --rows <number> number of rows (default: 10)
-f, --format <format> sql | copy | csv | json (default: "sql")
-n, --table <name> override table name
-o, --output <file> write output to a file instead of stdout
--push <url> execute directly against a live database
--seed <number> random seed for reproducible output
--schema <file> JSON file with custom columns (use with --template custom)
--schema-example print an example schema.json and exit
--list-db list all supported databases
--list-templates list all dataset templates
-V, --version show version
-h, --help show help--output: write to a file
# Save SQL to a file
sql-faker --db postgresql --template users --rows 500 --output seed.sql
# ✓ Written to seed.sql (42.1 KB)
# Save CSV
sql-faker --template products --rows 1000 --format csv --output products.csv
# ✓ Written to products.csv (98.3 KB)
# Save JSON
sql-faker --template companies --rows 100 --format json --output companies.json
# ✓ Written to companies.json (24.7 KB)--push: execute against a live database
Connects to your database and runs the generated SQL directly — no pipe needed.
# PostgreSQL
sql-faker --db postgresql --template users --rows 1000 \
--push "postgresql://user:pass@localhost:5432/mydb"
# Connecting to localhost:5432/mydb ...
# ✓ Inserted 1000 rows into postgresql in 312ms
# MySQL
sql-faker --db mysql --template orders --rows 500 \
--push "mysql://user:pass@localhost:3306/mydb"
# SQLite (pass a file path)
sql-faker --db sqlite --template products --rows 200 \
--push ./local.db
# With a custom table name
sql-faker --db postgresql --template users --rows 50 \
--table app_users \
--push "postgresql://localhost/mydb"Supported databases for --push:
| Database | Connection string format |
|---|---|
| PostgreSQL | postgresql://user:pass@host:5432/db |
| CockroachDB | postgresql://user:pass@host:26257/db |
| Amazon Redshift | postgresql://user:[email protected]:5439/db |
| MySQL | mysql://user:pass@host:3306/db |
| MariaDB | mysql://user:pass@host:3306/db |
| TiDB | mysql://user:pass@host:4000/db |
| PlanetScale | mysql://user:pass@host/db?ssl={"rejectUnauthorized":true} |
| SingleStore | mysql://user:pass@host:3306/db |
| SQLite | /path/to/database.db |
| libSQL (Turso) | /path/to/database.db |
Other examples
# Snowflake transactions — dialect-correct TIMESTAMP_NTZ and VARIANT
sql-faker --db snowflake --template transactions --rows 1000 --output txn.sql
# ClickHouse blog posts with MergeTree engine
sql-faker --db clickhouse --template blog --rows 50 --output blog.sql
# PostgreSQL COPY for ultra-fast bulk loading
sql-faker --db postgresql --template locations --rows 10000 --format copy | psql -d mydb
# Reproducible — same seed always produces same data
sql-faker --db mysql --template users --rows 100 --seed 42 --output seed.sqlLibrary API
import { generate } from 'sql-faker'
// SQL INSERT statements
const sql = generate({
database: 'postgresql',
template: 'users',
rows: 100,
format: 'sql',
})
// CSV
const csv = generate({
database: 'mysql',
template: 'orders',
rows: 500,
format: 'csv',
})
// JSON
const json = generate({
database: 'snowflake',
template: 'transactions',
rows: 50,
format: 'json',
})
// Reproducible output
const seeded = generate({
database: 'postgresql',
template: 'users',
rows: 10,
seed: 42,
})
// Custom table name
const renamed = generate({
database: 'mysql',
template: 'users',
rows: 20,
tableName: 'app_users',
})Custom Schema
Define your own table structure with a JSON file.
Step 1 — generate a starter schema.json:
sql-faker --schema-example > schema.jsonThis writes a ready-to-edit file:
[
{ "name": "id", "type": "integer", "primaryKey": true, "autoIncrement": true },
{ "name": "name", "type": "string", "length": 150, "nullable": false },
{ "name": "email", "type": "string", "length": 255, "nullable": false, "unique": true },
{ "name": "score", "type": "decimal", "precision": 5, "scale": 2, "nullable": true },
{ "name": "level", "type": "integer", "nullable": false },
{ "name": "is_active", "type": "boolean", "nullable": false },
{ "name": "bio", "type": "text", "nullable": true },
{ "name": "metadata", "type": "json", "nullable": true },
{ "name": "created_at", "type": "datetime", "nullable": false }
]Step 2 — edit it, then generate:
# SQL for any database
sql-faker --template custom --schema schema.json --db postgresql --rows 100 --table leaderboard
# Save to file
sql-faker --template custom --schema schema.json --db mysql --rows 500 --output seed.sql
# Push directly to your database
sql-faker --template custom --schema schema.json --db postgresql --rows 1000 \
--push "postgresql://user:pass@localhost:5432/mydb"
# CSV / JSON export
sql-faker --template custom --schema schema.json --format csv --rows 200 --output data.csvAvailable column types:
| type | Maps to (PostgreSQL) | Example options |
|---|---|---|
| integer | INTEGER | nullable, autoIncrement, primaryKey |
| bigint | BIGINT | nullable |
| float | DOUBLE PRECISION | nullable |
| decimal | NUMERIC(p,s) | precision, scale, nullable |
| string | VARCHAR(n) | length, nullable, unique |
| text | TEXT | nullable |
| boolean | BOOLEAN | nullable |
| date | DATE | nullable |
| datetime | TIMESTAMP | nullable |
| json | JSONB | nullable |
| uuid | UUID | nullable |
Library API:
import { generate } from 'sql-faker'
import type { ColumnDef } from 'sql-faker'
const schema: ColumnDef[] = [
{ name: 'id', type: 'integer', primaryKey: true, autoIncrement: true },
{ name: 'name', type: 'string', length: 100, nullable: false },
{ name: 'score', type: 'decimal', precision: 5, scale: 2 },
{ name: 'metadata', type: 'json', nullable: true },
{ name: 'created_at', type: 'datetime', nullable: false },
]
const sql = generate({
database: 'postgresql',
template: 'custom',
schema,
tableName: 'leaderboard',
rows: 50,
})Supported Databases
| Database | Category | Auto-Increment | JSON type | Timestamp type |
|---|---|---|---|---|
| PostgreSQL | OLTP | SERIAL | JSONB | TIMESTAMP |
| MySQL | OLTP | AUTO_INCREMENT | JSON | DATETIME |
| MariaDB | OLTP | AUTO_INCREMENT | LONGTEXT | DATETIME(3) |
| SQLite | Embedded | AUTOINCREMENT | TEXT | TEXT |
| SQL Server | OLTP | IDENTITY(1,1) | NVARCHAR(MAX) | DATETIME2 |
| Snowflake | OLAP | AUTOINCREMENT | VARIANT | TIMESTAMP_NTZ |
| BigQuery | OLAP | (explicit ids) | JSON | TIMESTAMP |
| Amazon Redshift | OLAP | IDENTITY(1,1) | SUPER | TIMESTAMP |
| ClickHouse | OLAP | (explicit ids) | String | DateTime |
| DuckDB | Embedded | (explicit ids) | JSON | TIMESTAMP |
| libSQL (Turso) | Embedded | AUTOINCREMENT | TEXT | TEXT |
| CockroachDB | Distributed | unique_rowid() | JSONB | TIMESTAMP |
| TiDB | Distributed | AUTO_INCREMENT | JSON | DATETIME |
| PlanetScale | Distributed | AUTO_INCREMENT | JSON | DATETIME |
| SingleStore | Distributed | AUTO_INCREMENT | JSON | DATETIME(6) |
Dataset Templates
users
id, first_name, last_name, email, username, password_hash,
phone, address, city, state, country, zip_code,
is_active, created_at, updated_atorders
id, user_id, order_number, status, total_amount, currency,
shipping_address, billing_address, payment_method, notes,
created_at, shipped_at, delivered_atStatus values: pending processing shipped delivered cancelled refunded
products
id, name, description, price, compare_price, sku, category, brand,
stock_quantity, weight, tags (JSON array), is_available, created_attransactions
id, user_id, transaction_id, amount, currency, type, status,
description, reference_id, gateway, fee, metadata (JSON), created_atTypes: credit debit refund transfer withdrawal deposit fee adjustment
blog
id, title, slug, excerpt, content, author_id, category, tags (JSON),
cover_image_url, view_count, like_count, comment_count,
status, published_at, created_at, updated_atcompanies
id, name, legal_name, domain, industry, company_size,
country, city, address, phone, email, website,
founded_year, employee_count, annual_revenue, description, created_atsocial
id, user_id, platform, handle, display_name, bio,
profile_url, avatar_url, followers_count, following_count,
posts_count, is_verified, is_private, joined_at, created_atPlatforms: twitter instagram facebook linkedin tiktok youtube github twitch
locations
id, city, state, country, country_code, continent,
latitude, longitude, timezone, population, elevation_m,
is_capital, created_atOutput Formats
sql — Dialect-aware CREATE TABLE + INSERT
-- PostgreSQL
CREATE TABLE IF NOT EXISTS "users" (
"id" SERIAL PRIMARY KEY,
"email" VARCHAR(255) NOT NULL UNIQUE,
"is_active" BOOLEAN NOT NULL,
"created_at" TIMESTAMP NOT NULL
);
INSERT INTO "users" ("email", "is_active", "created_at") VALUES
('[email protected]', true, '2023-04-12 09:22:11.000'),
('[email protected]', false, '2023-07-05 14:01:33.000');-- MySQL
CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`email` VARCHAR(255) NOT NULL UNIQUE,
`is_active` TINYINT(1) NOT NULL,
`created_at` DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;-- Snowflake
CREATE TABLE IF NOT EXISTS USERS (
ID NUMBER(38,0) AUTOINCREMENT PRIMARY KEY,
EMAIL VARCHAR(255) NOT NULL UNIQUE,
IS_ACTIVE BOOLEAN NOT NULL,
CREATED_AT TIMESTAMP_NTZ NOT NULL
);
INSERT INTO USERS (EMAIL, IS_ACTIVE, CREATED_AT) VALUES
('[email protected]', TRUE, '2023-04-12 09:22:11.000'::TIMESTAMP_NTZ);copy — PostgreSQL COPY (fastest bulk load)
COPY users ("first_name", "last_name", "email", "is_active", "created_at") FROM stdin;
Alice Smith [email protected] t 2023-04-12 09:22:11.000
Bob Jones [email protected] f 2023-07-05 14:01:33.000
\.csv — RFC-4180 with header row
id,first_name,last_name,email,is_active,created_at
1,Alice,Smith,[email protected],true,2023-04-12 09:22:11.000
2,Bob,Jones,[email protected],false,2023-07-05 14:01:33.000json — Array of objects with ISO 8601 dates
[
{
"id": 1,
"first_name": "Alice",
"email": "[email protected]",
"is_active": true,
"created_at": "2023-04-12T09:22:11.000Z"
}
]Type Reference
type DatabaseId =
| 'postgresql' | 'mysql' | 'mariadb' | 'sqlite' | 'mssql'
| 'snowflake' | 'bigquery' | 'redshift' | 'clickhouse'
| 'duckdb' | 'libsql'
| 'cockroachdb' | 'tidb' | 'planetscale' | 'singlestore'
type TemplateId =
| 'users' | 'orders' | 'products' | 'transactions'
| 'blog' | 'companies' | 'social' | 'locations' | 'custom'
type OutputFormat = 'sql' | 'copy' | 'csv' | 'json'
interface GenerateOptions {
database: DatabaseId
template: TemplateId
rows?: number // default: 10
format?: OutputFormat // default: 'sql'
tableName?: string // override default table name
schema?: ColumnDef[] // required for template: 'custom'
seed?: number // for reproducible output
}License
MIT © Rajat Thakur
