mohit4bug/greenseed README

Greenseed

Greenseed is a small CLI tool to seed a PostgreSQL database from JSON files.

Installation

pnpm add greenseed

Or install it globally:

pnpm add -g greenseed

Install via npm:

npm install greenseed
npm install -g greenseed

Install via bun:

bun install greenseed
bun install -g greenseed

Or install the standalone binary (no runtime required):

curl -fsSL https://raw.githubusercontent.com/mohit4bug/greenseed/main/install.sh | bash

Quick Start

Generate a seed configuration file:

greenseed init

Then run the seed:

greenseed push

Configuration

Greenseed reads a seed.config.json file in the current directory. You can specify a custom path with the -c flag.

seedFileExtensions

Allowed seed file extensions. Currently only .json is supported.

[".json"]

databaseUrlEnvVar

The name of the environment variable containing the PostgreSQL connection string.

"DATABASE_URL"

onMissingFile

Behaviour when a source data file does not exist.

  • "error" — throws an error and stops the seed (default)
  • "skip" — silently skips the table

useTransaction

When true (default), the entire seed runs inside a single database transaction. If any insert fails, all changes are rolled back.

true

tables

Array of table configurations. Each entry defines a table to seed.

OptionRequiredDefaultDescription
tableYesDatabase table name
schemaNo"public"Database schema the table belongs to
primaryKeysYesColumn names used for conflict detection
sourceYesPath to the JSON data file (relative to config)
updateOnConflictNonullColumns to update on conflict (upsert)
conflictTargetWhereNonullPredicate for partial unique indexes

Example

{
	"$schema": "./node_modules/greenseed/dist/schema/configuration_schema.json",
	"seedFileExtensions": [".json"],
	"databaseUrlEnvVar": "DATABASE_URL",
	"onMissingFile": "error",
	"useTransaction": true,
	"tables": [
		{
			"table": "users",
			"schema": "public",
			"primaryKeys": ["id"],
			"source": "./data/users.json"
		},
		{
			"table": "orders",
			"schema": "public",
			"primaryKeys": ["id"],
			"source": "./data/orders.json",
			"updateOnConflict": ["status", "updated_at"],
			"conflictTargetWhere": "is_active = true"
		}
	]
}

JSON Seed Files

Each source file must be a valid JSON array of objects. Extra object properties that do not exist as columns in the target table are automatically ignored.

[
	{ "id": 1, "name": "Alice", "email": "alice@example.com" },
	{ "id": 2, "name": "Bob", "email": "bob@example.com" }
]

CLI Reference

greenseed init

Generates a seed.config.json file with example configuration.

OptionDescriptionDefault
-c, --config <​file​>Config file pathseed.config.json

greenseed push

Connects to the database and seeds all configured tables.

OptionDescriptionDefault
-c, --config <​file​>Config file pathseed.config.json
-d, --dbvar <​name​>Override the database URL environment variable nameValue from config

The push command:

  1. Loads and validates the configuration
  2. Reads the database URL from the environment variable
  3. Connects to PostgreSQL and verifies all tables exist
  4. Reads each table's source JSON file and inserts data in batches
  5. Reports progress and a final summary

Environment Variables

The database connection URL must be set in the environment. By default, Greenseed reads from DATABASE_URL.

export DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
greenseed push

Override the variable name at runtime with the -d flag:

export MY_DB="postgresql://user:password@localhost:5432/mydb"
greenseed push --dbvar MY_DB

License

MIT