db-dump.sh is a simple Bash script for creating 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. - Timestamped Output: Each dump file is named with the current date and time to avoid overwrites.
- Organized Storage: Saves all dumps to
dumps/db/directory. - Docker Support: Automatically detects Docker environment and replaces
localhostwith thepostgresservice name. - Prisma Compatibility: Strips Prisma-specific query parameters (e.g.
?schema=public) unsupported bypg_dump. - Improved Portability: Uses
--no-ownerand--no-privilegesflags to ensure dumps can be easily restored by different users. - Error Handling: Removes the output file if
pg_dumpfails. - Named Arguments: Supports short (
-e) and long (--env) flags.
Ensure the script is executable:
chmod +x scripts/db-dump.shbash scripts/db-dump.shbash scripts/db-dump.sh -e apps/api/.envbash scripts/db-dump.sh -e apps/api/.env.dockerOutput: dumps/db/backup-2026-04-09_14-30-00.sql
The following variable must be present in your .env file:
DATABASE_URL=postgresql://postgres:your_password@localhost:5432/your_db?schema=public
bashpg_dump(comes with PostgreSQL)
Add the following to your .gitignore to avoid committing dump files:
# Dumps (e.g. database dumps, codebase dumps, etc.)
dumps/