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.
- Auto Configuration: Reads
DATABASE_URLfrom.env. - Safe
.envParsing: Correctly handles inline comments and quoted values. - Flexible ENV Path: Accepts a custom path to
.envfile via-e/--envargument. - Auto Latest: Automatically uses the most recent dump if no file is specified.
- Docker Support: Automatically detects Docker environment and replaces
localhostwith thepostgresservice name. - Prisma Compatibility: Strips Prisma-specific query parameters (e.g.
?schema=public) unsupported bypsql. - 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/--cleanto only clear the schema without performing a restore.
Ensure the script is executable:
chmod +x scripts/db-restore.shbash scripts/db-restore.shbash scripts/db-restore.sh -e apps/api/.envbash scripts/db-restore.sh -f dumps/db/backup-2026-04-09_14-30-00.sqlbash scripts/db-restore.sh -f dumps/db/backup-2026-04-09_14-30-00.sql -e apps/api/.envbash scripts/db-restore.sh -e apps/api/.env --resetbash scripts/db-restore.sh -cbash scripts/db-restore.sh -e apps/api/.env.dockerThe following variable must be present in your .env file:
DATABASE_URL=postgresql://postgres:your_password@localhost:5432/your_db?schema=public
bashpsql(comes with PostgreSQL)
Add the following to your .gitignore to avoid committing dump files:
# Dumps (e.g. database dumps, codebase dumps, etc.)
dumps/