Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DB Restore

db-restore.sh is a simple Bash script for restoring PostgreSQL database dumps. It automatically reads connection details from your .env file — no manual input required.

Features

  • Auto Configuration: Reads DATABASE_URL from .env.
  • Safe .env Parsing: Correctly handles inline comments and quoted values.
  • Flexible ENV Path: Accepts a custom path to .env file via -e / --env argument.
  • Auto Latest: Automatically uses the most recent dump if no file is specified.
  • Docker Support: Automatically detects Docker environment and replaces localhost with the postgres service name.
  • Prisma Compatibility: Strips Prisma-specific query parameters (e.g. ?schema=public) unsupported by psql.
  • Error Handling: Exits with a non-zero code if the connection or restore fails — no false success messages.
  • Named Arguments: Supports short (-f, -e, -r, -c) and long (--file, --env, --reset, --clean) flags.
  • Reset Mode: Optionally clears the public schema before restoring.
  • Clean Mode: Use -c / --clean to only clear the schema without performing a restore.

Usage

Ensure the script is executable:

chmod +x scripts/db-restore.sh

Restore latest dump with default .env

bash scripts/db-restore.sh

Restore latest dump with custom .env

bash scripts/db-restore.sh -e apps/api/.env

Restore specific dump with default .env

bash scripts/db-restore.sh -f dumps/db/backup-2026-04-09_14-30-00.sql

Restore specific dump with custom .env

bash scripts/db-restore.sh -f dumps/db/backup-2026-04-09_14-30-00.sql -e apps/api/.env

Restore with full database reset

bash scripts/db-restore.sh -e apps/api/.env --reset

Schema-only cleanup (no restore)

bash scripts/db-restore.sh -c

Run inside Docker (via runner container)

bash scripts/db-restore.sh -e apps/api/.env.docker

.env configuration

The following variable must be present in your .env file:

DATABASE_URL=postgresql://postgres:your_password@localhost:5432/your_db?schema=public

Requirements

  • bash
  • psql (comes with PostgreSQL)

.gitignore recommendation

Add the following to your .gitignore to avoid committing dump files:

# Dumps (e.g. database dumps, codebase dumps, etc.)
dumps/

About

Bash script for restoring PostgreSQL dumps with auto-latest, --reset and .env support

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages