A simple automation script for Windows that streamlines PostgreSQL database backup restoration. This PowerShell script eliminates manual commands and provides an interactive, user-friendly way to restore PostgreSQL backups with automatic version detection, connection verification, and post-restore validation.
Perfect for developers and DBAs who work with multiple PostgreSQL versions and need a quick, reliable way to restore database backups without memorizing complex pg_restore or psql commands.
- Automatic Version Detection - Scans and lists all installed PostgreSQL versions on your system
- Interactive Version Selection - Choose which PostgreSQL version to use for restoration
- Connection Verification - Tests database connection and verifies version matches selected instance
- Port Mismatch Detection - Alerts if you're connecting to wrong PostgreSQL instance on different port
- Smart Database Handling - Automatically creates database if it doesn't exist, or restores to existing one
- Drag & Drop Support - Supports file paths with or without quotes (drag and drop files directly)
- Multiple Backup Formats - Handles both
.sql(plain SQL) and.backup(custom/tar/directory) formats automatically - Post-Restore Validation - Lists all restored tables after successful restoration
- Color-Coded Output - Easy-to-read success/error/info messages
- Secure Password Input - Password masking for security
- Windows operating system
- PostgreSQL installed (supports multiple versions: 9.5, 15, etc.)
- PowerShell 5.1 or higher
- PostgreSQL backup files (
.sqlor.backupformats) - Appropriate database user credentials
Instead of manually typing commands like:
pg_restore -h localhost -p 5432 -U postgres -d mydb -v mybackup.backupThis script automates the entire process:
- ✅ No need to remember complex commands
- ✅ Automatically detects all PostgreSQL versions
- ✅ Prevents restoring to wrong version/port
- ✅ Creates database if it doesn't exist
- ✅ Shows what tables were restored
- ✅ Works with both
.sqland.backupfiles
- Clone this repository or download the script:
git clone https://github.com/yourusername/postgresql-restore-script.git- Navigate to the script directory:
cd postgresql-restore-script- Set execution policy (if needed):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser- Run the script:
.\restore_pg_backup.ps1- Follow the interactive prompts:
- Select PostgreSQL version from detected installations
- Enter database connection details (host, port, user, password)
- Provide backup file path (supports
.sqland.backupfiles) - You can drag and drop the backup file directly into PowerShell
- Specify target database name
- Confirm creation/restoration
================================================
PostgreSQL Backup Restore Script
================================================
[INFO] Searching for PostgreSQL installations...
[SUCCESS] Found 2 PostgreSQL installation(s):
[1] PostgreSQL 9.5 - C:\Program Files\PostgreSQL\9.5\bin\psql.exe
[2] PostgreSQL 15 - C:\Program Files\PostgreSQL\15\bin\psql.exe
Select PostgreSQL version [1-2]: 2
[SUCCESS] Selected PostgreSQL 15
Enter database host [localhost]: localhost
Enter database port [5432]: 5432
Enter database user: postgres
Enter database password: ****
[INFO] Testing connection to PostgreSQL server...
[SUCCESS] Connected to PostgreSQL server version: 15
[SUCCESS] Version verification passed!
Enter path to backup file (or drag and drop the file here): C:\backups\mydb.backup
[SUCCESS] Backup file found: C:\backups\mydb.backup
Enter target database name: restored_db
[INFO] Checking if database 'restored_db' exists...
[INFO] Database 'restored_db' does not exist
Create new database and restore? [Y/n]: y
[SUCCESS] Database 'restored_db' created successfully
[INFO] Starting restore process...
========================================
[SUCCESS] Backup restored successfully to database 'restored_db'
[INFO] Querying restored tables...
[SUCCESS] Found 3 table(s) in database 'restored_db':
- customers
- products
- orders
[SUCCESS] Script completed successfully!
Press any key to exit...
A test backup file (test_backup.sql) is included with sample data:
- customers table (5 records)
- products table (8 records)
- orders table (10 records)
Use this file to test the script:
.\restore_pg_backup.ps1
# When prompted, use: test_backup.sql
# Database name: test_restore_dbThe script works with:
.sqlfiles - Plain SQL dump files (created withpg_dump).backupfiles - Custom format backups (created withpg_dump -Fc)- tar/directory formats - Other PostgreSQL backup formats
Solution: Ensure PostgreSQL is installed in standard locations:
C:\Program Files\PostgreSQL\*\bin\C:\PostgreSQL\*\bin\- Or add PostgreSQL to your system PATH
Solution: This means you selected one PostgreSQL version but connected to another on a different port.
- PostgreSQL 9.5 might be on port 5433
- PostgreSQL 15 might be on port 5432
- Either select the correct version or use the correct port
Solution:
- Don't use quotes when typing the path manually
- When dragging and dropping, quotes are automatically handled
- Ensure the file path is correct and file exists
Solution: Verify:
- PostgreSQL service is running (
services.msc) - Host and port are correct
- Username and password are correct
- User has appropriate permissions
- Passwords are masked during input
PGPASSWORDenvironment variable is used temporarily and cleared after use- Never hardcode passwords in the script
- Ensure backup files contain no sensitive credentials
Kakamyrat Komekov - @komekovv
- PostgreSQL community for excellent documentation
- PowerShell community for scripting best practices
Note: Always test backup restoration on a non-production database first to ensure compatibility and data integrity.