When restoring a PostgreSQL database from a dump file, you often need to ensure the target database is completely clean before the restoration process. This prevents potential conflicts with existing schema objects or data.
The most straightforward method is to drop and recreate the database:
psql -U postgres -c "DROP DATABASE IF EXISTS db_test"
psql -U postgres -c "CREATE DATABASE db_test"
If you're using pg_restore instead of psql, consider these useful flags:
pg_restore --clean --if-exists --create -U postgres -d db_test testdb.dump
For databases with active connections, you might need to terminate them first:
psql -U postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'db_test'"
Here's a complete example for automation:
#!/bin/bash
DB_NAME="db_test"
DUMP_FILE="testdb.sql"
# Terminate active connections
psql -U postgres -c "SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '${DB_NAME}'"
# Recreate database
psql -U postgres -c "DROP DATABASE IF EXISTS ${DB_NAME}"
psql -U postgres -c "CREATE DATABASE ${DB_NAME}"
# Restore dump
psql -U postgres -d ${DB_NAME} < ${DUMP_FILE}
For very large databases, consider using parallel restore with pg_restore:
pg_restore -j 4 --clean --if-exists -U postgres -d db_test testdb.dump
- Always backup existing data before dropping databases
- Consider transaction wrappers for atomic operations
- Verify user privileges before executing destructive commands
When working with PostgreSQL database restorations, it's often necessary to ensure the target database is completely empty before applying a backup. This prevents conflicts with existing objects and ensures a clean restoration state.
The most reliable method combines DROP SCHEMA
and CREATE SCHEMA
operations:
psql -U postgres -d db_test -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
For more granular control, you can use these approaches:
1. Dropping All Objects Individually
psql -U postgres -d db_test << EOF
DROP OWNED BY current_user CASCADE;
EOF
2. Recreating the Entire Database
psql -U postgres -c "DROP DATABASE db_test; CREATE DATABASE db_test;"
Here's the full recommended sequence:
# Step 1: Clean the database
psql -U postgres -d db_test -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
# Step 2: Restore from backup
psql -U postgres db_test < testdb.sql
After restoration, you might need to reapply specific configurations:
psql -U postgres -d db_test -c "GRANT ALL ON SCHEMA public TO postgres;"
psql -U postgres -d db_test -c "GRANT ALL ON SCHEMA public TO public;"
psql -U postgres -d db_test -c "CREATE EXTENSION IF NOT EXISTS plpgsql;"
For frequent use, consider this bash script:
#!/bin/bash
DB_NAME="db_test"
BACKUP_FILE="testdb.sql"
echo "Cleaning database $DB_NAME..."
psql -U postgres -d $DB_NAME -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
echo "Restoring from $BACKUP_FILE..."
psql -U postgres $DB_NAME < $BACKUP_FILE
echo "Restoring standard permissions..."
psql -U postgres -d $DB_NAME <