A powerful, interactive command-line tool for managing PostgreSQL databases with confidence. Built for developers who need reliable, production-ready database operations with automatic dependency resolution and transactional safety.
Managing PostgreSQL databases often involves tedious manual work: remembering foreign key dependencies, handling circular references, ensuring data integrity during imports, and maintaining consistent backup practices. This CLI automates all of that.
Key Benefits:
- Zero Configuration: Works out of the box with any PostgreSQL database
- Smart Dependency Resolution: Automatically handles foreign key relationships and circular dependencies
- Transactional Safety: All operations use transactions with automatic rollback on failure
- Multi-Database Support: Easily switch between multiple database connections
- Production-Ready: Used in production environments with robust error handling
| Feature | Description |
|---|---|
| π¦ Smart Dumps | Export databases with automatic foreign key ordering and optional schema inclusion |
| π₯ Safe Imports | Import SQL dumps with transactional rollback on any error |
| π§Ή Truncate Tables | Empty all tables while respecting foreign key constraints |
| π£ Drop Everything | Remove all tables and custom types in the correct order |
| π Table Inspector | List all tables with their sizes at a glance |
| π§ Interactive CLI | User-friendly prompts guide you through every operation |
| π Sequence Management | Automatically reset PostgreSQL sequences after imports |
npm install -g database-handler-cliCreate a .env file in your project root:
# Production database
DATABASE_URL=postgresql://user:password@localhost:5432/prod_db
# Development database
DATABASE_URL=postgresql://user:password@localhost:5432/dev_dbNote: Comments above
DATABASE_URLentries are used as database identifiers in the interactive menu.
Simply run:
database-handlerThen follow the interactive prompts to select your database and desired operation.
The tool analyzes your database schema to determine the correct order for exporting tables based on foreign key relationships. This ensures that imports will work without constraint violations.
-- Example generated dump structure
SET session_replication_role = 'replica'; -- Temporarily disable constraints
INSERT INTO "users" (...) VALUES (...); -- Dependencies first
INSERT INTO "posts" (...) VALUES (...); -- Then dependent tables
SET session_replication_role = 'origin'; -- Re-enable constraints
SELECT setval('public."users_id_seq"', ...); -- Reset sequencesDump Options:
- Data only: Fast exports without schema definitions (default)
- With schema: Include
CREATE TABLEstatements for full database recreation
Dump files are stored in ./backups/{database-name}/dump-DD-MM-YYYY-{timestamp}.sql
Imports run within a single transaction, ensuring atomicity:
- If any statement fails, the entire import is rolled back
- Your database remains in a consistent state
- Detailed error messages help diagnose issues
The tool uses topological sorting to determine safe deletion order:
- Analyzes foreign key dependencies
- Detects and handles circular references
- Drops tables in correct order
- Removes custom PostgreSQL types
- Uses
CASCADEonly when necessary
src/
βββ features/
β βββ dump.ts # Smart database export with dependency resolution
β βββ import.ts # Transactional SQL import with rollback
β βββ drop.ts # Safe table/type deletion with topological sort
β βββ empty.ts # Truncate all tables
β βββ list.ts # Table size inspection
β βββ schema.ts # Schema-only export
βββ utils/
β βββ getAllTableNames.ts # Table discovery
β βββ getDbConnections.ts # .env parsing
β βββ getDumpFilePath.ts # Backup path generation
βββ database.ts # Kysely connection factory
βββ cli.ts # Interactive CLI interface
- Kysely: Type-safe SQL query builder
- Inquirer.js: Interactive CLI prompts
- Chalk: Terminal styling
- pg: PostgreSQL client
- Node.js: 18+ (or Bun runtime)
- PostgreSQL: 12+
- Permissions:
SELECT,INSERT,DELETE,DROPon target databases
- Clone the repository:
git clone https://github.com/Raumain/database-handler-cli.git
cd database-handler-cli- Install dependencies:
bun install # or npm install- Run in development mode:
bun src/cli.ts- Build for production:
bun run buildFor isolated development environments:
docker compose up -d
docker compose exec -it bun bashThis project uses Biome for linting and formatting:
bun run fmt # Format and fix issues
bun run fmt.check # Check without modifyingThe tool supports multiple database connections in a single .env file:
# Production
DATABASE_URL=postgresql://...
# Staging
DATABASE_URL=postgresql://...
# Local Development
DATABASE_URL=postgresql://...Switch between databases using the interactive menu.
While designed as a CLI, the tool can be imported programmatically:
import { database } from 'database-handler-cli/database'
import { dump } from 'database-handler-cli/features/dump'
const db = database('postgresql://...')
await dump(db, 'my-database', false)- Support for MySQL and SQLite
- Parallel dump/import for large databases
- Incremental backups
- Cloud storage integration (S3, Azure Blob)
- Schema diffing and migration generation
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Found a security vulnerability? Please report it privately to romaintibo6@gmail.com. See SECURITY.md for details.
Built with β€οΈ using TypeScript, Bun, and Kysely.
